1

I am trying to add configurable product programatically in cart i can add simple product to cart but while adding configurable product it showing "you must select product option to add cart" error in console here is my controller file

<?php
    namespace Multiple\Addtocart\Controller\Index;    
    use Magento\Framework\Controller\ResultFactory;
    use Magento\Framework\App\Action\Action;
    use Magento\Framework\App\Action\Context;
    use Magento\Framework\View\Result\PageFactory;
    use Magento\Checkout\Model\Sidebar;
    use Magento\Checkout\Model\Cart as CustomerCart;
class Upsell extends \Magento\Framework\App\Action\Action
{
    protected $resultPageFactory;
    protected $checkoutSession;
    protected $formKey;
    protected $request;
    protected $cart;
    protected $sidebar;

  public function __construct(
    Context $context,
    \Magento\Framework\Data\Form\FormKey $formKey,
    \Magento\Checkout\Model\Session $checkoutSession,
     CustomerCart $cart,
      Sidebar $sidebar,
    PageFactory $resultPageFactory,
    array $data = []) 
    { 
        $this->formKey = $formKey;
        $this->resultPageFactory = $resultPageFactory;
         $this->checkoutSession = $checkoutSession;
        $this->cart = $cart;
        $this->sidebar = $sidebar;
        parent::__construct($context);
    }


  public function execute()
  {

      $pid=$_POST['id'];
      $type=$_POST['type'];
      $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
      if($type=="configurable")
      {
          $size=$_POST['size'];
          $color=$_POST['color'];
          $colorid=$_POST['coloropt'];
          $sizeid=$_POST['sizeopt'];
      $_product = $objectManager->create('Magento\Catalog\Model\Product')->load($pid);
      $resultPage = $this->resultPageFactory->create();
      $options = array(
               // $colorid =>$color,
                //$sizeid => $size
                356 => 5,
                357 => 29
            );
      $params = array(
                    'form_key' => $this->formKey->getFormKey(),
                    'product' =>$pid, //product Id
                    'super_attribute' => $options,
                    'qty'   =>1 //quantity of product                   
                    );              
                  $this->cart->addProduct($_product, $params);
                  $this->cart->save();
      }
      if($type=="simple")
      {
      $_product = $objectManager->create('Magento\Catalog\Model\Product')->load($pid);
      $resultPage = $this->resultPageFactory->create();
      $params = array(
                    'form_key' => $this->formKey->getFormKey(),
                    'product' =>$pid, //product Id
                    'qty'   =>1, //quantity of product 
                    'price' => $_product->getPrice()                    
                    );              
                  $this->cart->addProduct($_product, $params);
                  $this->cart->save();  
      }
     $this->messageManager->addSuccess(__('Item has been successfully added to cart.'));
    echo "success";

  }
}
  • Have you checked the attribute id and option id correctly? – Dinesh Yadav Nov 17 '17 at 4:48
  • @ Dinesh Yadav ya i have checked it there you can see aa commented code there i get the attributeid and option id – Amith Nov 17 '17 at 4:50
  • @Dinesh Yadav i just print the options and i get like this Array ( [356] => 5 [357] => 29 ) – Amith Nov 17 '17 at 5:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.