Magento以编程方式将可配置产品添加到购物车

时间:2015-06-09 09:03:37

标签: magento cart configurable-product

我试图将可配置的产品添加到购物车,但是虽然它没有抛出任何异常,但购物车仍然是空的。

我之前使用过这段代码没有任何问题,所以我不确定它是否与我使用的Magento版本有关。

我使用的代码是:

   $post = $this->getRequest()->getPost();
   $session = Mage::getSingleton('customer/session'); 

   $attr = array_keys($post['sa']);

   $cart = Mage::getSingleton('checkout/cart');
   $cart->init();

    foreach ($post['sa'][$attr[0]] as $optId){
        try {
            if (abs($post['qty'][$optId]) > 0){
                $product = Mage::getModel('catalog/product')->load($post['product']);
                $this->getRequest()->setParam('product',$post['product']);
                $this->getRequest()->setParam('super_attribute',array(
                        $attr[0] => $optId
                    ));
                $options = array(
                    "product"=>$post['product'], 
                    "super_attribute"=>array(
                        $attr[0] => $optId
                    ),                    
                    "qty"=>$post['qty'][$optId]
                ); 


                $opts = new Varien_Object();
                $opts->setData($options);

                var_dump($opts);

                $cart->addProduct($product, $opts);
                $cart->save();

            }
        } catch (Exception $e){
            var_dump($e);
        }


    }

    $cart->save(); // save the cart
    $cart->setCartWasUpdated(true); 

$pdts = $cart->getAllVisibleItems();

    var_dump($pdts);
    die("??");

因此,我希望购买包含6件物品的购物车(3款产品,可配置+简单),但我会变为空 - 正如您从下面所看到的,这也显示了$ opts对象我试图传递:

    object(Varien_Object)[507]
      protected '_data' => 
        array (size=3)
          'product' => string '86' (length=2)
          'super_attribute' => 
            array (size=1)
              179 => string '20' (length=2)
          'qty' => string '1' (length=1)
      protected '_hasDataChanges' => boolean true
      protected '_origData' => null
      protected '_idFieldName' => null
      protected '_isDeleted' => boolean false
      protected '_oldFieldsMap' => 
        array (size=0)
          empty
      protected '_syncFieldsMap' => 
        array (size=0)
          empty
    object(Varien_Object)[663]
      protected '_data' => 
        array (size=3)
          'product' => string '86' (length=2)
          'super_attribute' => 
            array (size=1)
              179 => string '19' (length=2)
          'qty' => string '2' (length=1)
      protected '_hasDataChanges' => boolean true
      protected '_origData' => null
      protected '_idFieldName' => null
      protected '_isDeleted' => boolean false
      protected '_oldFieldsMap' => 
        array (size=0)
          empty
      protected '_syncFieldsMap' => 
        array (size=0)
          empty
    object(Varien_Object)[678]
      protected '_data' => 
        array (size=3)
          'product' => string '86' (length=2)
          'super_attribute' => 
            array (size=1)
              179 => string '17' (length=2)
          'qty' => string '3' (length=1)
      protected '_hasDataChanges' => boolean true
      protected '_origData' => null
      protected '_idFieldName' => null
      protected '_isDeleted' => boolean false
      protected '_oldFieldsMap' => 
        array (size=0)
          empty
      protected '_syncFieldsMap' => 
        array (size=0)
          empty
    null
    ??

任何帮助都会被批准!

2 个答案:

答案 0 :(得分:1)

您可以使用以下代码添加。

try {
$product_id = '126'; 
$product = Mage::getModel('catalog/product')->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
    'product' => $product_id,
    'super_attribute' => array(
        525 => 100,
        //525 is the attribute id of size and 100 is the selected option value (small) of that attribute.  
    ),
    'qty' => 2,
);
$cart->addProduct($product, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
Mage::getSingleton('core/session')->addSuccess('Product added successfully');
//to do
}
catch (Exception $e) {
echo $e->getMessage();
}

答案 1 :(得分:0)

正如人生所愿,问题实际上与添加到购物车无关。

有一个已知问题'使用localhost作为Magento上的主机名 - 这里有很多其他帖子可以解决这个问题。我们正在努力解决这个问题,但显然它仍然没有在Chrome中保持我的会话,但它在Firefox中运行良好。