Php Magento添加多个可配置产品变体多次添加第一个
我试图将一个可配置产品的多个变体一次添加到购物车中,我将代码放在一起,但目前它添加了正确数量的产品,但只使用了第一个变体 换句话说,如果我尝试添加2件绿色T恤和4件白色T恤,它将添加6件绿色T恤 这是我得到的代码:Php Magento添加多个可配置产品变体多次添加第一个,php,magento,cart,configurable-product,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
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/');
}
但在购物车中,我看到了第一个super_属性中的6个
在添加每件物品或其他物品后,我是否需要做些什么来“重置”购物车
谢谢 在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>
我偶然发现了这个问题,但答案毫无帮助。所以我在这里发布我的版本
$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);
你得到这个问题的答案了吗?你建议的通过parentProductId的解决方案对我也很有效。有关更多信息,请参见以下app\code\core\Mage\Checkout\Model\Cart.php公共函数addProduct($productInfo,$requestInfo=null)和受保护函数_getProduct($productInfo)
$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);
$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);