Skip to main content
1 of 3
Magento 2
  • 3.8k
  • 7
  • 55
  • 107

Magento add product programatically with custom option is required field

I try to add simple product programatically 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 acheive this?

Magento 2
  • 3.8k
  • 7
  • 55
  • 107