Magento添加多个可配置的产品变体,多次添加第一个

时间:2015-05-14 09:35:38

标签: php magento cart configurable-product

我试图立即将可配置产品的多种变体添加到购物车中,并且我已将代码放在一起,但目前它正在添加正确数量的产品,但仅限于使用第一个变体。

换句话说,如果我尝试添加2件绿色T恤和4件白色T恤,它会增加6件绿色T恤。

这是我得到的代码:

public function indexAction ()   {
   $post = $this->getRequest()->getPost();
   $attr = array_keys($post['super_attribute']);
   $cart = Mage::getSingleton('checkout/cart');
    $product = Mage::getModel('catalog/product')->load($post['product']);
    foreach ($post['super_attribute'][$attr[0]] as $optId){

        if (abs($post['qty'][$optId]) > 0){

            $options = array(
                //"product"=>$post['product'], 
                "super_attribute"=>array(
                    $attr[0] => $optId
                ),                    
                "qty"=>$post['qty'][$optId]
            ); 

            echo "Add To Cart:";
            print_r($options);
            echo "<br /><br />";                
            $cart->addProduct($product, $options);
        }

    }

    $cart->save(); // save the cart
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true); 

    die("??");
    $this->_redirect('checkout/cart/');    
}

从那个print_r,它确认选项是正确的:

 Add To Cart:Array ( [super_attribute] => Array ( [141] => 5 ) [qty] => 2 ) 

 Add To Cart:Array ( [super_attribute] => Array ( [141] => 4 ) [qty] => 4 ) 

但是在购物车中我看到了第一个super_attribute中的6个。

我需要做些什么来重置&#39;添加每个项目后的购物车?

谢谢!

2 个答案:

答案 0 :(得分:1)

我偶然发现了这个问题,答案并没有帮助。所以我在这里发布我的版本。

$parentProduct = Mage::getModel('catalog/product')->load($parentId)
    ->setStoreId(Mage::app()->getStore()->getId());

foreach($postData['super_attribute'] as $simpleProdId => $simpleProdConfig){

    //This cloning is important
    $product = clone $parentProduct;

    $cartParams = array();

    $cartParams = array(
        'super_attribute' => $simpleProdConfig['super_attribute'],
        'qty' => $simpleProdConfig['qty'],
    );

    $this->_getCart()->addProduct($product, $cartParams);

}

$this->_getCart()->save();

$this->_getSession()->setCartWasUpdated(true);

或者不是传递父产品对象传递其id,因为它似乎工作,虽然请求变慢。

$parentProduct = Mage::getModel('catalog/product')->load($parentId)
    ->setStoreId(Mage::app()->getStore()->getId());

foreach($postData['super_attribute'] as $simpleProdId => $simpleProdConfig){

    $cartParams = array();

    $cartParams = array(
        'super_attribute' => $simpleProdConfig['super_attribute'],
        'qty' => $simpleProdConfig['qty'],
    );

    //Passing id instead of instance of the parent product here
    $this->_getCart()->addProduct($parentProduct->getId(), $cartParams);

}

$this->_getCart()->save();

$this->_getSession()->setCartWasUpdated(true);

答案 1 :(得分:0)

在etc / config.xml add

中创建自定义模块
<?xml version="1.0"?>
<config>
    <modules>
        <Package_Mymodule>
            <version>0.1.0</version>
        </Package_Mymodule>
    </modules>
    <global>
        <models>
            <checkout>
                <rewrite>
                    <cart>Package_Mymodule_Model_Checkout_Cart</cart>
                </rewrite>
            </checkout>
        </models>
    </global>
</config>

并在下一个路径中创建文件Package / Mymodule / model / Checkout / Cart.php

class Package_Mymodule_Model_Checkout_Cart extends Mage_Checkout_Model_Cart{
    public function addProduct($productInfo, $requestInfo=null){   
        $producstChildId = $requestInfo['product_child_id'];

        foreach ($producstChildId as $key => $value){
            $requestInfo['qty'] = current($value);

            if($requestInfo['qty']){    
                //It is the size of the product     
                $requestInfo['super_attribute'][133] = key($value);

                $product = Mage::getModel('catalog/product')
                    ->setStoreId(Mage::app()->getStore()->getId())
                    ->load($requestInfo['product'])
                    ->setConfiguredAttributes(array('super_attribute'=>$requestInfo['super_attribute']));           
                parent::addProduct($product,$requestInfo);
            }
        }
    }
}