4

I am making rest api's for add product to cart for that I wrote this code:

public function _create(array $data) {

        $id = $data['id'];
        $qty = $data['qty'];

       $product = Mage::getModel('catalog/product')                 
                         // load the product object
                         ->load($data['id']);

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

       $quote->collectTotals()->save();

 return json_encode(array("customer", $quote->getId()));

}

from this the product successfully adding in shopping cart(sales_flat_quote table) and it returns quote ID but I want to add this product to particular(logged in) user's cart for that I load quote from customer like this:

$customer = Mage::getModel('customer/customer')->load(21);

// Load quote by Customer
$quote = Mage::getModel('sales/quote')
             ->loadByCustomer($customer);

But still it is not associating with customer(in table customer ID is 0). Please let me know where I am doing wrong or I have to do more for that.

3

If I understand correctly, your complete code is like this

public function _create(array $data) {

        $id = $data['id'];
        $qty = $data['qty'];

       $product = Mage::getModel('catalog/product')                 
                         // load the product object
                         ->load($data['id']);

       $customer = Mage::getModel('customer/customer')->load(21);

       // Load quote by Customer
       $quote = Mage::getModel('sales/quote')
              ->loadByCustomer($customer);

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

       $quote->collectTotals()->save();

      return json_encode(array("customer", $quote->getId()));

}

Instead of, load quote by customer object try to load this quote by customerId like this

// Load quote by Customer
$quote = Mage::getModel('sales/quote')
         ->loadByCustomer($customer->getId());

I've tested this one on Magento 1.9.0.1 just right now and it works fine. Here my code

I can access the user because it is identified by my application via token ...

$customer = Mage::getModel('customer/customer')->load($this->getApiUser()->getUserId());
$customer_ID = $customer->getId();

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

Hope it's help you

The working code is:

$store = $this->_getStore();
    Mage::app()->setCurrentStore($store->getId());

    $customer_ID = 10;

    $customer = Mage::getModel('customer/customer')->load($customer_ID);

    // load quote by customer
    $quote = Mage::getModel('sales/quote')->loadByCustomer($customer_ID);

    $quote->assignCustomer($customer);

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

    // Add Product to Quote
    $quote->addProduct($product,$data['qty']);

    // Calculate the new Cart total and Save Quote
    $quote->collectTotals()->save();

Regards

13
  • No flow still it is adding product as a guest and I am not using oauth so I do not have token.
    – Pushpendra
    May 15 '15 at 12:14
  • And if you use this method : $customer_ID = 21; $quote = Mage::getModel('sales/quote')->loadByCustomer($customer_ID); then call your addProduct method like before $quote->addProduct($product, $qty); $quote->collectTotals()->save(); To be sure you can also Assign the quote to your customer's model: $quote->assignCustomer($customer); where $customer = Mage::getModel('customer/customer')->load($customer_ID);
    – Flow
    May 15 '15 at 12:55
  • do you have an error when you call this one ? try to log $quote please and give the log
    – Flow
    May 15 '15 at 13:03
  • flow u made my day $quote->assignCustomer($customer); is the trickkkk....I am updating your answer :)))))
    – Pushpendra
    May 15 '15 at 13:09
  • happy to help you. I spend a lot of hours / days to do that ;-)
    – Flow
    May 15 '15 at 13:15
1

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();
0

Here is the working code to logged in user's cart

require_once '../app/Mage.php';
Mage::app();
umask(0);

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

$product_id = '98';
$qty = '1'; 
$product = Mage::getModel('catalog/product')->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$cart->addProduct($product, array('qty' => $qty));
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
Mage::getSingleton('core/session')->addSuccess('Product added successfully');

$cart =array(
'status' => True,
'Message' => 'Product added to cart successfully.'
);
echo json_encode($cart);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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