2013-06-28 117 views
0

我有一個控制器類,可以用於添加項目購物車。我的場景是
我的產品有兩個自定義選項。一個是孩子,另一個是嬰兒。
我想從這兩個添加一個自定義選項到我的購物車。但是,我嘗試了許多不同的方式。但是,我在結帳頁面中看不到任何自定義選項。我怎樣才能做到這一點。在這裏我的代碼示例。添加到購物車與自定義選項虛擬產品在magento

public function indexAction() { 
    $products = explode(',', $this->getRequest()->getParam('products')); 
    $cart = Mage::getModel('checkout/cart'); 
    $cart->init(); 
    foreach ($products as $product_id) { 
    if ($product_id == '') { 
continue; 
    } 
    $pModel = Mage::getModel('catalog/product')->load($product_id); 
    $param = array(
     'qty' => 2, 
     'options' => array(
     'option_id' => $product_id, 
     'default_title' => "Ticket for Child or Infant", 
     'title' => "Child 2 to 12" 
     ) 
    ); 
    if ($pModel->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_VIRTUAL) { 
     $cart->addProduct($pModel, new Varien_Object($param)); 
     Mage::getSingleton('checkout/session')->setCartWasUpdated(true); 
    } 
    $cart->save(); 
    $this->_redirect('checkout/cart'); 
} 

回答

0

Soooo,你必須非常手動添加一個項目到購物車。這是如何...

$cart = Mage::getSingleton("checkout/cart"); 

// $products_to_add is something I made up... 
// associative array of product_ids to an array of their custom options 
foreach ($products_to_add as $product_id => $custom_options) { 
    $product = Mage::getModel("catalog/product")->load($product_id); 
    $options = new Varien_Object(array("options" => $custom_options, 
            "qty" => 1)); 

    // some products may result in multiple products getting added to cart 
    // I believe this pulls them all and sets the custom options accordingly 
    $add_all = $product->getTypeInstance(true) 
     ->prepareForCartAdvanced($options, $product, Mage_Catalog_Model_Product_Type_Abstract::PROCESS_MODE_FULL); 

    foreach ($add_all as $add_me) { 
    $item = Mage::getModel('sales/quote_item'); 
    $item->setStoreId(Mage::app()->getStore()->getId()); 
    $item->setOptions($add_me->getCustomOptions()) 
     ->setProduct($add_me); 

    $item->setQty(1); 
    $cart->getQuote()->addItem($item); 
    } 
} 

// when done adding all the items, finally call save on the cart 
$cart->save(); 
相關問題