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'); 
}

但是当我在我的网站上执行此操作时,它只会将没有选项的产品添加到商店,是的,我确保检查我是否有选项。有谁知道为什么?

4

1 回答 1

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); 
于 2013-05-06T16:44:34.080 回答