Magento - 使用循环将产品添加到购物车
我收到来自外部站点的请求,其中包含一些产品 ID。 在我的模块中,我尝试加载产品并将其添加到购物车中。我用这段代码尝试过:
public function indexAction() {
$ids = explode(',', $this->getRequest()->getParam('products'));
Mage::log('ADDING PRODUCTS');
$cart = Mage::getModel('checkout/cart');
$cart->init();
$pModel = Mage::getSingleton('catalog/product');
//$cart->addProductsByIDs($ids);
foreach ($ids as $id) {
Mage::log('Loading: ' . $id);
$product = $pModel->load($id);
Mage::log('Loaded: ' . $product->getId());
try {
$cart->addProduct($product, array('qty' => '1'));
} catch (Exception $e) {
Mage::log($e);
continue;
}
}
$cart->save();
if ($this->getRequest()->isXmlHttpRequest()) {
exit('1');
}
$this->_redirect('checkout/cart');
}
我可以在 system.log 中看到它正确加载了产品。但重定向后,我的购物车中的第二个产品出现了两次。第一个失踪了。使用 $cart->addProductsByIDs($ids) 效果很好,但我无法再影响产品的数量。
有人知道我做错了什么并给我提示吗?
谢谢
I get a request from an extrenal site containing some product ids.
In my module i try to load the products and add them to the shopping cart. I tried it with this code:
public function indexAction() {
$ids = explode(',', $this->getRequest()->getParam('products'));
Mage::log('ADDING PRODUCTS');
$cart = Mage::getModel('checkout/cart');
$cart->init();
$pModel = Mage::getSingleton('catalog/product');
//$cart->addProductsByIDs($ids);
foreach ($ids as $id) {
Mage::log('Loading: ' . $id);
$product = $pModel->load($id);
Mage::log('Loaded: ' . $product->getId());
try {
$cart->addProduct($product, array('qty' => '1'));
} catch (Exception $e) {
Mage::log($e);
continue;
}
}
$cart->save();
if ($this->getRequest()->isXmlHttpRequest()) {
exit('1');
}
$this->_redirect('checkout/cart');
}
I can see in the system.log that it loads the products correctly. But after the redirect i have the second product in my cart twice. The first one is missing. Using $cart->addProductsByIDs($ids) works great but then i cant influence the quantity of the products anymore.
Does someone know what i am doing wrong and give me a hint?
Thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,我通过在每个循环中加载产品模型来修复它:
然后我有一个“将所有商品添加到购物车”按钮,该按钮执行以下 Javascript 代码:
为了使其能够正常工作,我在数量文本框中添加了一个额外的 Product_id 属性,例如:
整个产品列表位于 ID 为 product-listing-table 的 div 中
I had the same problem and I fixed it by loading the product model inside every loop:
I then have an "Add all items to cart" button which executes the following Javascript code:
For this to be able to work, I put an extra product_id attribute on the quantity textbox, such as:
and the whole list of products is inside a div with ID product-listing-table
刚刚检查了我得到的一些自定义添加到购物车代码,并确认工作正常,我唯一的区别是:
也就是说,您的错误并不会让您听起来像您实际上在 这个区域。我无法想象会是这样,但是每次将产品添加到购物车时是否都需要保存购物车模型?值得一试。
Just checked with some custom add to cart code that I've got, and confirmed is working correctly, and the only difference I have is:
That said, your error doesn't make it sound like you're actually having trouble in this area. I can't imagine it would be this, but is it possible that the cart model needs to be
save()
d every time you add a product to the cart? It's worth a punt.