Pergunta

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

Foi útil?

Solução

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'); 
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a magento.stackexchange
scroll top