简体   繁体   English

Magento | 我如何在自定义模块中的购物车中添加产品?

[英]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.

相关问题 Magento Soap API在购物车中添加具有自定义选项值的产品 - Magento soap api add product with custom options value in shopping cart Magento - 使用购物车页面上的自定义模块添加自定义块 - Magento - Add custom block using custom module on Shopping Cart page Magento将可配置产品添加到购物车 - Magento Add Configurable Product to shopping cart 如何在自定义模块magento中集成购物车规则的条件块? - How to integrate Conditions Block of Shopping Cart Rules in Custom module magento? 向购物车中的产品添加自定义评论 - add a custom comment to product in shopping cart 我如何才能将Magento添加到购物车ajax推送正确的网址并将产品添加到购物车? - How can I get Magento add to cart ajax push the right url and add the product to cart? Magento-如何使用模块在产品详细信息页面中添加新的自定义模块 - Magento- How can i add a new custom block in product details page using module 如何动态地将产品添加到购物车? - How to dynamically add product to shopping cart? 如何在可配置产品页面上添加自定义空购物车按钮:Magento - How to add a custom empty cart button on configurable product page:Magento 如何将自定义产品添加到magento购物车-请说明这些选项 - How to add a custom product to magento cart - Please explain these options
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM