0

I need to forcefully add product Y to cart if product X is add to cart. Both X and Y are individual simple products. The following is what i have done to achieve this:

try{
    $quote->assignCustomer($customer);
    $quote->addProduct($product, $request);
    $quote->collectTotals()->save();
    **if($productId==25)
    {
        $productModel = Mage::getModel('catalog/product');
        $productCombo= $productModel->setStoreId($store)->loadByAttribute('sku','8903287005350');
        $param=$this->_getParam($productCombo,null,1);
        $request1 = new Varien_Object();
        $request1->setData($param);
        $quote = Mage::getModel('sales/quote')->setStoreId($store)->loadByCustomer($customer);
        $quote->assignCustomer($customer);
        $quote->addProduct($productCombo, $request1);
        $quote->collectTotals()->save();
    }**
    $cartItems  = $this->_getCartItems($store,$quote->getData("entity_id"));
    $cart_data  = $this->getCartItemsData($store,$quote->getData("entity_id"));
    $message['status']              =   'success';                
    $message['message']             =   'Product successfully added to shopping cart';
    $message["quote_id"]            =   $quote->getData("entity_id");
    $message['cart_item_count']     =   $cart_data["cart_qty"];
    $message['grand_total']         =   $cart_data["grand_total"];
    $message['items']               =   $cartItems;
}
catch(Exception $e)
{                
    // $this->_critical("Product can't be added to shopping cart",Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
    $this->_critical($e);
}

And i am getting the following exception:

enter image description here

NB: Both products X and y are in-stock, qty>10 and status=enabled

Can anyone tell me what i am doing wrong? Any help will be appreciated.

0

How to adds multiple items to shopping cart, at a time?

Normally this code is kept into controller.

public function multipleProdAddAction()
      {
        $items = array("1" => "sku1" , "2" => "sku2");
        $invalidProduct = array();
        $validProducts = array();
        foreach($items as $qty=>$sku) {
            $id = Mage::getModel('catalog/product')->getIdBySku($sku);
            //Check whether the sku exists or not.
            if(!$id) {
                $invalidProduct[$sku] = $id;
            }
            else {
                $validProducts[$sku] = $quantity;
            }

        }
        //If any of the product we are adding, is not valid, we are not adding it to cart.
        if(count($invalidProduct) < 1 && count($validProducts) > 0) {
            try{
                $cart = Mage::helper('checkout/cart')->getCart();
                $ms="";
                foreach($validProducts as $sku => $qty) {
                    $params = array('qty' => $qty);
                    $id = Mage::getModel('catalog/product')->getIdBySku($sku);
                    $product = Mage::getModel('catalog/product')->load($id);;
                    $cart->addProduct($product, $params);
                    $msg .= $product->getName(). " is successfully added into cart<br>";
                }
                 $cart->save();
                 Mage::getSingleton('core/session')->addSuccess(Mage::helper('checkout')->__($msg));
                 $this->_redirect('checkout/cart');

            }
            catch(Exception $e) {
            Mage::getSingleton('core/session')->addError(Mage::helper('checkout')->__($e->getMessage()));
            $this->_redirect('checkout/cart');
         }

        }  
      }

I hope it will help you.

0

I solved this issue by the following method.

The Problem with my code was i was passing the wrong object to add to cart. I was loading the product using loadByAttribute('sku',$_sku). The correct method is to get the productId usign SKU and load product using load($productId).

ref: Adding product to cart cause error 'The stock item for Product is not valid.'?

So instead of

    $productModel = Mage::getModel('catalog/product');
    $productCombo= $productModel->setStoreId($store)->loadByAttribute('sku','8903287005350');

I added

  $_sku = '8903287005350';
  $_catalog = Mage::getModel('catalog/product');
  $_productId = $_catalog->getIdBySku($_sku);
  $productCombo = Mage::getModel('catalog/product')->setStoreId($store)->load($_productId);

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.