1

Below code helps me to add configurable product to current cart session

    $quote_id = '4';
    $params = array(
        'product' => 1,
        'super_attribute' => array(
            132 => 3,
            133 => 8
        ),
        'qty' => 1,
    );
    $cart = Mage::getSingleton('checkout/cart'); 
    $product = new Mage_Catalog_Model_Product();
    $product->load(1); 
    $cart->addProduct($product, $params,$quote_id);
    $cart->save();
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);    

In a similar way i want to add configurable product to my existing quote_id 4

I've tried

$cart = Mage::getSingleton('checkout/cart')->load(4);

But unfortunately it didn't worked. Any suggestion to make it work ?

2 Answers 2

2

To add the product to particular quote, you have load a quote with store id.

$storeId = Mage::app()->getStore()->getStoreId(); // get current store id
$quote = Mage::getModel('sales/quote')->setStoreId($storeId)->load(4); //4 is your quote id
$params = array(
        'product' => 1,
        'super_attribute' => array(
            132 => 3,
            133 => 8
        ),
        'qty' => 1,
    );
$product = Mage::getModel('catalog/product')->load(1); //your config product is
$quote->addProduct($product, new Varien_Object($params))->save();

Try this code it'll work.

1
  • Not exactly the way but it worked. I'll do the rest. Thanks. Sep 24, 2015 at 10:47
0
  • *Use try and catch to exception handling*
  • **Also use Use proper product model load**
  • Use Mage::getModel('catalog/product') instead of new Mage_Catalog_Model_Product()

As you want to add configuable to quote id =4 then you can try below

Code:

$params
=array('product'=>1,super_attribute=>array('132'=>3,'133'=>8,),qty=>1));
    try {     $quote = Mage::getModel('sales/quote')->load(4);
        $product =Mage::getModel('catalog/product')
            ->setStoreId(Mage::app()->getStore()->getId())
            ->load($productId);;
        $quote->addProduct($product,new Varien_Object($params));

        $quote->save();

    } catch (Mage_Core_Exception $e) {
            echo 'Cannot add the item to shopping cart.');
        Mage::logException($e);

    } catch (Exception $e) {
       echo 'Cannot add the item to shopping cart.');
        Mage::logException($e);

    }
7
  • Thanks for your suggestion. I need to add a product to existing quote of id 4. Not to current cart session. Sep 24, 2015 at 9:33
  • At question.. you mentioned that current cart session.....Then what would be point of it?
    – Amit Bera
    Sep 24, 2015 at 9:38
  • Read the question again completely. I said the mentioned piece of code is used to add product to current cart session but i need to add the same to my existing quote_id Sep 24, 2015 at 10:30
  • are want to assign the configurable to quote 4 and then want to assign that code current cart session. Am i right?
    – Amit Bera
    Sep 24, 2015 at 10:33
  • No only to existing quote 4 Sep 24, 2015 at 10:34

Your Answer

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

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