0

I try to add simple product programmatically it's working fine but i want to add product with custom option how i achieve this?

public function addtocartAction(){
      /* Get Product id From Form Post */
        $product_id = $this->getRequest()->getPost('productids');
      /* Get Product id From Form Post */

      /* Add Product in to Cart */
        $product=new Mage_Catalog_Model_Product();
        $product->load($product_id); // Product Id
        $quote = Mage::getSingleton('checkout/session')->getQuote();
        $quote->addProduct($product); // quantity is 1
        $cart = Mage::getSingleton('checkout/cart');
        $cart->init(); // tried commenting this too!
        $cart->save();
        Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
        $quote->collectTotals()->save();
        $this->_redirectReferer();
        Mage::getSingleton('core/session')->addSuccess('Your product has been Added successfully...'); 
      /* Add Product in to Cart */
    }

Note I have multiple Custom option i need to get Is Required = Yes option in list.phtml and add it How I achieve this?

1 Answer 1

1
public function checkoutAction()
    {
        $data = $this->getRequest()->getPost();
        try
        {
            if($data)
            {
                        $productId=$data['id'];
                        $qty=$data['qty'];
                        $cart = Mage::getSingleton('checkout/cart');
                        $cart->init();
                        $product = Mage::getModel('catalog/product')->load($data['id']);
                        $formKey = Mage::getSingleton('core/session')->getFormKey();
                        $product->setSkipCheckRequiredOption(true);
                        Mage::app()->cleanCache();
                        $option=array();

                        foreach ($product ->getOptions() as $value) 
                        {
                            $values = $value->getValues();
                            switch(trim($value->getData('title')))
                            {
                                case 'color':
                                        $colorMainID=$value->getData('option_id');
                                        break;
                                case 'jug':
                                        $jugMainID=$value->getData('option_id');
                                        break;
                                case 'shield cover':
                                        $shieldMainID=$value->getData('option_id');
                                        break;
                            }
                        }


                        if($data['shieldid']!='')
                        {
                            $option[$shieldMainID]=$data['shieldid'];
                        }
                        if($data['colorid']!='')
                        {
                            $option[$colorMainID]=$data['colorid'];
                        }
                        if($data['jugid']!='')
                        {
                            $option[$jugMainID]=$data['jugid'];
                        }

                        if($product->getSku()!='')
                            {

                                        $paramater = array(
                                                        'product' => $productId,
                                                        'qty' => $qty,
                                                        'form_key' => $formKey,
                                                         'options' => $option
                                                    );       
                                $request = new Varien_Object();
                                $request->setData($paramater);
                                $cart->addProduct($product, $request);
                                $cart->save();
                        }
                    $cart->save();
                    Mage::getSingleton('checkout/session')->setCartWasUpdated(true); 
            }
        }
        catch(Exception $e)
        {
            $session->addError($e->getMessage());   
        }
    } 

i use this code to add product with custom option
you must specify custom option id to add with custom option color,jug,shiled are the custom main option id
enter image description here

10
  • Actually custom option they create manually lot of we can't restrict Tilltle value
    – Magento 2
    Commented Nov 23, 2016 at 11:16
  • if you want to add product programatically with custom option . you must specify all required custom option Commented Nov 23, 2016 at 11:18
  • in my case only 3 custom option are there so i add it with given code., all work fine Commented Nov 23, 2016 at 11:19
  • Yes but i don't want to featch bacsed on title name
    – Magento 2
    Commented Nov 23, 2016 at 11:19
  • use id instead of title Commented Nov 23, 2016 at 11:20

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.