cancel
Showing results for 
Search instead for 
Did you mean: 

How to add product in cart with custom options in Magento 2

How to add product in cart with custom options in Magento 2

Hello,

I'm trying to add a product on cart, with option, type file. I can add products on cart with this code, but my option file doesn't work.

My params variable to add a product is :

$params = array(
    'form_key' => $this->formKey->getFormKey(),
    'product' => $productId,
    'qty'   =>1,
    'options' => array(
      '11'=> array( //11 is the id of my custom option (find it on catalog_product_options table)
          'type' => "application/octet-stream",
          'quote_path' =>"/upload/".$img_name,
          'secret_key' => substr(md5(file_get_contents($_SERVER['DOCUMENT_ROOT']."/upload/".img_name)), 0, 20),
          )
      )
);

When i'm going to the cart, I can see my product, but I have error:

THE PRODUCT HAS REQUIRED OPTIONS.

Does someone have any idea ? Or am I missing something on my $params ?

Thank you

3 REPLIES 3

Re: How to add product in cart with custom options in Magento 2

Hi @VincentAZ 

Try or look into the following code. This code is working for me. 

$productId = 127;
$product = $obj->create('\Magento\Catalog\Model\Product')->load($productId);

$cart = $obj->create('Magento\Checkout\Model\Cart');    
$params = array();      
$options = array();
$params['qty'] = 1;
$params['product'] = 127;

foreach ($product->getOptions() as $o) 
{       
    foreach ($o->getValues() as $value) 
    {
        $options[$value['option_id']] = $value['option_type_id'];

    }           
}

$params['options'] = $options;
$cart->addProduct($product, $params);
$cart->save();

I hope it will help you!

Re: How to add product in cart with custom options in Magento 2

Hello @VincentAZ 

 

Magento\Catalog\Model\Product\Option\Type\File.php

 

check line no

 

 } catch (ProductException $e) {
            switch ($this->getProcessMode()) {
                case \Magento\Catalog\Model\Product\Type\AbstractType::PROCESS_MODE_FULL:
                    throw new LocalizedException(__('Please specify product\'s required option(s).'));
                    break;
                default:
                    $this->setUserValue(null);
                    break;
            }

try to debug into this file

 

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: How to add product in cart with custom options in Magento 2

Thank for your answer.

 

But I can't find what I have to put in my options.

 

I'm begginer on Magento 2 and I've lot of difficulty to find an example to add product in cart with a file option (I can add with text option).

I try to delete some elements (quote_path, secret_key) on my params options (11), to replace by value=$_SERVER['DOCUMENT_ROOT']."/upload/".$img_name and a label with the name of my option.

 

Don't work for me Smiley Sad I have the same error.

 

I search others solutions to add my product,  can I use Magento API to add cart in product (with my file option) ?