【问题标题】:Magento. How to add simple product in cart programmatically with custom optionsMagento。如何使用自定义选项以编程方式在购物车中添加简单产品
【发布时间】:2016-02-04 05:08:51
【问题描述】:

我正在尝试在购物车中添加带有自定义选项的简单产品。

    $post = $this->getRequest()->getPost();
    $_product = Mage::getModel('catalog/product')->load(8);
    $QuoteId= Mage::getModel('checkout/cart_api')->create('default');
    $storeId = Mage::app()->getStore()->getId();
    $arrProducts = array(
        array(
            "product_id" => 8,
            "qty" => 1,
            "options" => array(
                '1' => array(
                    'sku' => 'cheese'
                )
            )
        )
    );
    $cart = Mage::getSingleton('checkout/cart');
    $cart->addProduct($_product, $arrProducts);
    $cart->save();
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

但不幸的是,产品没有添加到购物车中。 我有错误

"请指定产品所需的选项"

我做错了什么?我有两种产品选择。选项 1 id 是 1 和 sku 奶酪 ...

【问题讨论】:

  • 你好! stackoverflow.com/questions/13698236/… 注意这个答案如何使用 ->addOption 到报价项目,然后将其选项添加到“additional_options”代码中?这通常是需要这样做的方式。如果这不能为你解决问题,我可以看看:)
  • Mikel,您能根据您的评论更改我的代码吗?

标签: php magento magento-1.7 magento-1.9 magento-1.8


【解决方案1】:

我认为你的数组太多了:

$arrProducts = array(
    array(
        "product_id" => 8,
        "qty" => 1,
        "options" => array(
            'option_id' => 'option_value'
        )
    )
);

应该做的工作。

【讨论】:

    【解决方案2】:

    下面的代码我已经成功了,请检查:

    $options = array('related_product'=>null,
                15=>37,
                16=>41,
                17=>45,
                18=>51,
                19=>150000); //Those are my option. 
    $cart = Mage::getSingleton('checkout/cart');
    $cart->init();   // Add a product with custom options
    
    $params = array('product' => $_product->getId(),
                    'qty' => 1,
                    'options' => $options
                    );
    $request = new Varien_Object();
    
    $request->setData($params);
    
    $quoteObj->addProduct($_product, $request); 
    

    【讨论】:

      猜你喜欢
      • 2018-06-26
      • 2016-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多