Search code examples
phpmagentomagento-rest-api

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

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. 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. But when I get my cart, there is no product to add...

in return I have a code 200 OK

Do you have any suggestions to help me?

Thanks a lot for your help

regards


Solution

  • 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) :

    //$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