0

For my project I need to empty current quote and add new products from quotation (custom module, and all programmatically).

But, when I add some products (5000 for my tests), I have an error because I don't have stock for this product.

For my test, I need to add product with custom price and custom quantity. At this moment, I have this code :

$cart = Mage::getModel('checkout/cart');
    $quoteItems = Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection();
    foreach($quoteItems as $item)
        $cart->removeItem($item->getId());

    // Nouveau panier
    $devisItems = Mage::getModel('devis/detail')->getCollection()->addFieldToFilter('devis_id', (int)$quote_id);
    foreach($devisItems as $item)
    {
        $product = Mage::getModel('catalog/product')->load($item->getOfferId());
        $cart->addProduct($product, 1);
    }
    $cart->save();
    Mage::getSingleton('customer/session')->setCartWasUpdated(true);

Can you help me ? Thanks

0

Please try below,

$quote = Mage::getModel('sales/quote');
$quote = $quote->setStoreId($store_id);
$quote->setIsSuperMode(true);

$productId  = 1;
$iqty       = 10;

$productModel = Mage::getModel('catalog/product');
$product = $productModel->load($productId);
$quote->addProduct($product, $iqty);

$quote->collectTotals();
$quote->save();
  • Hello Nalin, I tried this code yesterday. But, it does not work : I have 400 products in stock but I want to add 5000 in cart. (error message : The quantity for "product" is not available) – th0r1eif Jun 15 '17 at 9:29
  • Please check my updated answer – Nalin Savaliya Jun 15 '17 at 9:56
  • It does'nt work when I activate cart super mode. – th0r1eif Jun 15 '17 at 10:08
  • Ohh, it's cart, you can set super mode for quote, sorry for wrong information. I have updated my answer, it will help anybody for future reference – Nalin Savaliya Jun 15 '17 at 10:21
  • Yes that's quote I want to create. Is it necessary to create cart before quote ? – th0r1eif Jun 15 '17 at 10:22
0

You made me on the good road !

Here the solution I found :

$cart = Mage::getModel('checkout/cart');
    $quoteItems = Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection();
    foreach($quoteItems as $item)
        $cart->removeItem($item->getId());

    // Nouveau panier
    $devisItems = Mage::getModel('devis/detail')->getCollection()->addFieldToFilter('devis_id', (int)$quote_id);
    foreach($devisItems as $item)
    {
        $product = Mage::getModel('catalog/product')->load($item->getOfferId());

        $stock_item = $product->getStockItem();
        $stock_item->setData('is_in_stock', true);
        $stock_item->setData('use_config_backorders', false);
        $stock_item->setData('backorders', '1');
        $stock_item->save();

        $cart->addProduct($product, 5000);
    }
    $cart->save();
    Mage::getSingleton('customer/session')->setCartWasUpdated(true);

I have to reactivate backorders for this product when I'll end my process.

Thanks

  • Please don't save product, just remove $product->save();, without it; will work – Nalin Savaliya Jun 15 '17 at 9:50
  • Please check my updated answer, you will not need to play with stock item, just set super mode for cart – Nalin Savaliya Jun 15 '17 at 9:57
  • It work without product update, I updated my answer – th0r1eif Jun 15 '17 at 10:09
  • 1
    Saving $stock_item is necessary – th0r1eif Jun 15 '17 at 10:21

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.