选项未加载到购物车中

时间:2013-05-06 14:58:36

标签: php magento

好的,所以我有一个可行的模块。在模块中,我添加了多个产品,其中包含以下选项:

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 个答案:

答案 0 :(得分: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);