2014-10-28 23 views
0

我在Magento中創建了新的產品類型。但是,我無法將其所有關聯產品添加到sales_flat_quote_item表中。我只想將關聯的產品添加到表格中,並且只將主要的父級產品顯示在購物車中。將相關產品添加到報價中,但不包含購物車

我真的很接近實現這一點。現在,只有父件在購物車中可見時纔會顯示。但是,上述表格中只列出了其中一種相關產品。

這裏是我的代碼片段:

class Namespace_Module_Model_Product_Type_Custom extends Mage_Catalog_Model_Product_Type_Abstract { 
........ 

    protected function _prepareProduct(Varien_Object $buyRequest, $product, $processMode) 
{ 
    $qty = $buyRequest['qty']; 
    $associatedQty = $buyRequest['associated_qty']; 

    if($qty >= 1) { 
     $result = parent::_prepareProduct($buyRequest, $product, $processMode); 
     if (is_array($result)) { 
      $product = $this->getProduct($product); 

      foreach($buyRequest['associated'] as $associated){ 
       if($associated){ 
        $subProducts[] = Mage::getModel('catalog/product')->load($associated); 
       } 
      } 

      foreach($subProducts as $subProduct){ 
       if($subProduct){ 
        $product->setCartQty($qty); 
        $product->addCustomOption('product_qty_'.$subProduct->getId(), $associatedQty[$subProduct->getId()], $subProduct); 

        $product->addCustomOption('associated_product_' . $subProduct->getId(), $associatedQty[$subProduct->getId()]); 

       } 
      } 

        $_result = $subProduct->getTypeInstance(true)->_prepareProduct(
         $buyRequest, 
         $subProduct, 
         $processMode 
        ); 

      if (!isset($_result[0])) { 
       return Mage::helper('checkout')->__('Cannot add the item to shopping cart'); 
      } 

      $_result[0]->setParentProductId($product->getId()) 
       ->addCustomOption('parent_product_id', $product->getId()); 

      $result[] = $_result[0]; 
      return $result; 

     } else { 
      return $this->getSpecifyOptionMessage(); 
     } 
    } else { 
     return $this->getQtyMessage(); 
    } 
} 

........ 
} 

眼下只有相關聯的產品「53」被添加爲一個子產品。我仍然錯過了其他兩個。基本上,foreach($subProducts as $subProduct)循環將使用三個相關產品循環三次。我假設在Magento的某處,它只使用最後一個循環產品。

任何意見或幫助,這將是偉大的。提前致謝!

回答

0

想通了。我只需將以下內容轉換爲foreach循環而不是外部循環。

$_result[0]->setParentProductId($product->getId()) 
      ->addCustomOption('parent_product_id', $product->getId()); 

$result[] = $_result[0]; 
相關問題