0

Hei all I am working on customisation of product detail page where I have added text box for qty with every available size, my module will work like if user want S size with 5 pieces , M size with 2 pieces then he will able to choose that. I have added custom form action and my controller code is

    <?php

namespace Custom\Product\Controller\Add;


use Magento\Catalog\Model\Product;
use Magento\Checkout\Model\Cart;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;

class Index extends \Magento\Framework\App\Action\Action
{
    /**
     * @var Cart
     */
    protected $cart;
    /**
     * @var Product
     */
    protected $product;

    public function __construct(
        Context $context,
        PageFactory $resultPageFactory,
        Product $product,
        Cart $cart
    ) {
        $this->resultPageFactory = $resultPageFactory;
         $this->cart = $cart;
        $this->product = $product;
        parent::__construct($context);
    }

    public function execute()
    {
        try {
            $post = $this->getRequest()->getPostValue();
            $params = array();
            foreach($post['selected_qty'] as $key => $qty) {
                if($qty > 0 && !empty($qty))
                    $params['qty'][$key] = $qty;
           }

            $params['product'] = $post['product'];
            $params['form_key'] = $post['form_key'];
            $_product = $this->product->load($params['product']);
            if ($_product) {
                $this->cart->addProduct($_product, $params);
                $this->cart->save();
            }

            $this->messageManager->addSuccess(__('Add to cart successfully.'));
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->messageManager->addException(
                $e,
                __('%1', $e->getMessage())
            );
        } catch (\Exception $e) {
            $this->messageManager->addException($e, __('error.'));
        }
        /*cart page*/
        $this->getResponse()->setRedirect('/checkout/cart/index');


    }
}

but my product is not adding in cart getting below error

You need to choose options for your item

Can anyone help me?

  • please check whether you are passing size with qty in params – Vishnunath Jun 13 '18 at 12:14
  • I am passing Array ( [qty] => Array ( [178] => 2 [179] => 2 [184] => 2 [186] => 2 ) [product] => 43074 [form_key] => HYUkYP5CdTBCltyp ) array in qty array ID is size id – Daniel_12 Jun 13 '18 at 12:18
  • Array ( [uenc] => aHR0cDovL2xvY2FsaG9zdC9tYWdlbnRvMi9hdGxhcy1maXRuZXNzLXRhbmsuaHRtbA,[product] => 723 [form_key] => UD8OwJ4Hf8zTKxno [super_attribute] => Array ( [141] => 168 ) [qty] => 1 ). pass like this 141 is size ID, 168 is size option ID(M or S). you need to pass this in foreach function and also add cartProduct() – Vishnunath Jun 13 '18 at 14:05

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.