[英]Magento | How i can add a product in shopping cart in a custom module?
I am currently working on the development of a management module Cart on Magento 1.9 我目前正在开发Magento 1.9上的管理模块Cart
I'm stuck on adding a product to my cart (I tried many different things) and that's why I solicits your help. 我一直坚持在我的购物车上添加一个产品(我尝试了很多不同的东西),这就是我寻求帮助的原因。
My module extends the rest API of magento and I have already managed update to my cart (quantity of products) but now I'll wish to add a new product via the POST method. 我的模块扩展了magento的其余API,我已经管理了对我的购物车的更新(产品数量),但现在我希望通过POST方法添加新产品。 Of course I'm logged as customer.
当然我是以客户身份登录的。 I defined the creation privileges for this role.
我定义了此角色的创建权限。 (I can do the update without problems)
(我可以毫无问题地进行更新)
Here is my code : 这是我的代码:
protected function _create(array $data){
$store = $this->_getStore();
Mage::app()->setCurrentStore($store->getId());
$cart = Mage::getModel('checkout/cart');
$cart->init();
$productCollection = Mage::getModel('catalog/product')->load(4);
// Add product to cart
$cart->addProduct($productCollection,
array(
'product_id' => $productCollection->getId(),
'qty' => '1'
)
);
// Save cart
$cart->save();
}
In this simple example, I try to add the product id 4 in quantity 1. My problem is that I have no error in the log, and everything seems to be past. 在这个简单的例子中,我尝试在数量1中添加产品ID 4.我的问题是我在日志中没有错误,一切似乎已经过去了。 But when I get my cart, there is no product to add...
但是当我拿到购物车时,没有产品可以添加...
in return I have a code 200 OK 作为回报,我有一个代码200 OK
Do you have any suggestions to help me? 你有什么建议可以帮到我吗?
Thanks a lot for your help 非常感谢你的帮助
regards 问候
I finally found the solution after traveling all internet;) 我终于在所有互联网旅行后找到了解决方案;)
In fact when you want to reach the cart before checkout, magento uses the definition "Quote" ... Not easy to understand for a beginner on Magento.... So in order to facilitate research of those who are like me had troubles, here is my code to add a new product in the cart (before checkout) : 事实上,当你想在结账前到达购物车时,magento使用定义“引用”......对于Magento的初学者来说不容易理解....所以为了方便研究那些像我一样的人有麻烦,这是我在购物车中添加新产品的代码(结账前):
//$data['entity_id'] = The id of the product you want to add to the cart
//$data['qty'] = The quantity you want to specify
protected function _create(array $data)
{
$store = $this->_getStore();
Mage::app()->setCurrentStore($store->getId());
// Get Customer's ID
$customerID = $this->getApiUser()->getUserId();
// Load quote by Customer
$quote = Mage::getModel('sales/quote')
->loadByCustomer($customerID);
$product = Mage::getModel('catalog/product')
// set the current store ID
->setStoreId(Mage::app()->getStore()->getId())
// load the product object
->load($data['entity_id']);
// Add Product to Quote
$quote->addProduct($product,$data['qty']);
try {
// Calculate the new Cart total and Save Quote
$quote->collectTotals()->save();
} catch (Mage_Core_Exception $e) {
$this->_error($e->getMessage(),Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR);
}
}
I hope this can help someone 我希望这可以帮助别人
Regards 问候
Carniflex Carniflex
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.