doujiang5211 2013-05-06 14:58
浏览 17
已采纳

选项未加载到购物车中

OK, so I have a module that works. In the module, I add multiple products with some options like this:

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

But when I execute this on my site, it only adds the product with no options to the store, and yes, I made sure to check if I'm even getting options. Anyone know why?

  • 写回答

1条回答 默认 最新

  • douye9822 2013-05-06 16:44
    关注

    I think you should check data of $copy variable. The parameters should be in format:

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

    Also, you don't need

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

    just pass $params like this:

    $cart->addProduct($product, $params); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案