2013-05-06 66 views
0

好的,所以我有一個模塊可以工作。在該模塊中添加多個產品,像這樣一些選擇:選項不能加載到購物車

public function indexAction() { 
    $prod_count = $this->getRequest()->getParam('prod_count'); 
    $cart = Mage::getModel('checkout/cart'); 
    $cart->init(); 
    for($i = 1; $i <= $prod_count ;$i++){ 
     $prod_id = $this->getRequest()->getParam('prod_'.$i.'_id'); 
     $prod_count = $this->getRequest()->getParam('product_'.$i.'_count'); 
     $product = Mage::getModel('catalog/product')->load($prod_id); 
     $options = array('options' => NULL); 
     for($u = 1; $u <= $prod_count; $u++){ 
      $op_id= $this->getRequest()->getParam('option_id_'.$u.'_'.$i); 
      $op_type_id = $this->getRequest()->getParam('option_type_id_'.$u.'_'.$i); 
      $options['options'][] = array($op_id => $op_type_id); 
     } 
     $copy = array(); 
     for($r = 0; $r < count($options['options']); $r++){ 
      array_push($copy,$options['options'][$r]); 
      var_dump($copy); 
     } 
     echo $prod_id.'<br><br>';try { 
     $params = array(
        'product' => $prod_id, // This would be $product->getId() 
        'qty' => 1, 
        'options' => $copy 
       ); 
     $request = new Varien_Object(); 
     $request->setData($params); 
     $cart->addProduct($product, $request); 
     Mage::getSingleton('checkout/session')->setCartWasUpdated(true); 
     $cart->save(); 
     }catch (Exception $ex) { 
      echo $ex->getMessage(); 
     } 
    }  
    if ($this->getRequest()->isXmlHttpRequest()) { 
     exit('1'); 
    } 
    $session= Mage::getSingleton('checkout/session'); 
    $this->_redirect('checkout/cart'); 
} 

但是,當我執行這個在我的網站,它只會增加不帶任何選項商店的產品,是的,我確信檢查如果我甚至獲得選擇。有人知道爲什麼

回答

1

我想你應該檢查$copy變量的數據。該參數應採用以下格式:

Array 
(
    [product] => 171  // product id 
    [options] => Array 
     (
      [4] => 1111 // <option_id> => <selected value> 
      [3] => 7  // <option_id> => <selected value> 
     ) 

    [qty] => 1 
) 

而且,你不需要

$request = new Varien_Object(); 
$request->setData($params); 

只是通過$params這樣的:

$cart->addProduct($product, $params); 
+0

狗屎,你的權利。我發誓我花時間確保$ copy的格式正確。謝謝! – user962028 2013-05-06 17:01:14

相關問題