1

I am adding products to cart from admin to customer account like this:

$quote = Mage::getModel('sales/quote')->loadByCustomer($customer_ID);
$quote->addProduct($product, $qty);

from this product is adding successfully in sales_flat_quote table with customer details but on frontend product is not showing in customer's cart. From little search I found that to show product in frontend side I have to add product to cart like this:

$quote = Mage::getSingleton('checkout/session')->getQuote();
$quote->addProduct($product, $qty);

But this is not working in my case as I have to use them from mobile apps.

So my queries are following:

  1. My first query is obvious how I can show products to fronted?
  2. Magento using oauth system for rest api's but that is not good(my perception) for android/ios applications. So what is best way to make api's for such of type of apps.
  3. When I am creating rest api for customer I am noticing that always in customer details it shows created from Admin and Associate to Website is Admin Even I after hardcode the website id value like:$customer->setWebsiteId(2); Really we can not change?
  4. Is it good to create customer from admin side always?

If you find some corrections, please update them.

3 Answers 3

0

If you are attempting to add an item to the cart via the Magento api, then the proper methods would be like this:

cart_product.add (SOAP V1)
shoppingCartProductAdd (SOAP V2)

HTTP Method: PUT /products/:id (REST)

PUT http://magentohost/api/rest/products/8
PUT for specific store http://magentohost/api/rest/products/8/store/3

See the Magento api documentation for more help: http://www.magentocommerce.com/api/rest/introduction.html

4
  • This(rest api) is for updating a product not for add to cart. Check here magentocommerce.com/api/rest/Resources/Products/… and as per my knowledge there is no available rest api for adding a product to cart.
    – Pushpendra
    Commented May 18, 2015 at 5:27
  • See cart_product.add, clearly a method for adding a product to the cart via the SOAP api. You can even create a new cart with cart.create Commented May 18, 2015 at 13:22
  • See here for a script gist.github.com/roberto-butti/3509401 Commented May 18, 2015 at 13:23
  • Tim I made it, Check my answer if you want to know.
    – Pushpendra
    Commented May 19, 2015 at 8:52
0

This is how I made it. To show product at frontend we have to add product at checkout/cart instead of quote/sales.

   Mage::init('default');
   Mage::getSingleton('core/session', array('name' => 'frontend'));  

   // Get customer session
   $session = Mage::getSingleton('customer/session'); 

   // Get cart instance
   $cart = Mage::getSingleton('checkout/cart');
   $cart->init();

   // Add a product with custom options
   $product = Mage::getModel('catalog/product')
                     // set the current store ID
                     ->setStoreId(Mage::app()->getStore()->getId())
                     // load the product object
                     ->load($data['id']);

   $cart->addProduct($product, $data['qty'] );             

   // update session
   $session->setCartWasUpdated(true);

   // save the cart
  $cart->save(); 
0

I spent two days to find the issue. Finally, the result is: When we load the quote by customer, if quote is not exist in the database new quote will be create but store ID is missing. We need to assign store to the quote.

Here is the working code:

$customer = Mage::getModel('customer/customer')->load($customerId);
$product = Mage::getModel('catalog/product')->load($productId);
// load quote by customer and store...
$quote = Mage::getModel('sales/quote')->setStore($storeId)->loadByCustomer($customerId);
$quote->addProduct($product, 1);
$quote->setIsActive(1);
$quote->collectTotals()->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.