3

How to get Configurable Products options, price on related products sections. Also, put add to cart button on each product. right now related products show only the price as low as but want to add customer can also see configurable options and add cart button click then product should be added to cart without going to the product detail page.

can any help me?

Any kind of help will be appreciated.

Magento Version: 2.2.6

  • Can you please add more details to question ? – Vivek Jan 24 at 12:49
  • @Vivek please see edited question. – H_Parekh Jan 24 at 12:54
3

You can create same controller as default addToCart controller.

public function execute()
    {
        $currentStore = $this->storeManager->getStore();
        $baseUrl = $currentStore->getBaseUrl();

        $post = $this->getRequest()->getPostValue();


        $option=array();

        foreach($post as $key => $val) { 


            if($key =='p_id' || $key =='qty' || $key == 'customoption_id' || $key == 'product-custom-option')
            {
                continue;
            }
            else{

                $option[$key] = $val;
            }
        }

       $productid = $post['p_id'];

        $_product = $this->_productRepositoryInterface->getById($productid);

        if ($_product->hasOptions()){    

            $options = $_product->getOptions();
            $customoptions = array();
            $customoptions[$post['customoption_id']] = $post['product-custom-option'];

            $params = array (
                'product' => $_product->getId(),
                'qty' => $post['qty'],
                'price' => $_product->getPrice(),
                'options' => $customoptions,
                'super_attribute'=>$option

            );

        }else{

            $params = array (
                'product' => $_product->getId(),
                'qty' => $post['qty'],
                'price' => $_product->getPrice(),
                'super_attribute'=>$option

            );

        }



       $this->_cart->addProduct($_product, $params);
       $this->_cart->save();
       $this->messageManager->addSuccess(__("You added ".$_product->getName()." to your shopping cart."));
       //$this->_redirect('checkout/cart/index'); 
    }

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.