I wrote a plugin to add custom option while adding the product to cart

public function beforeAddProduct($subject, $productInfo, $requestInfo = null) {        
        $additionalOptions['setting_style'] = [
            'label' => "Unique Product",
            'value' => $productInfo->getId() . "_" . rand()."_".$productInfo->getQty()
        //additional option for showing the items uniquely  
        $productInfo->addCustomOption('additional_options', $this->_serializer->serialize($additionalOptions));
        return [$productInfo, $requestInfo];

It's working well for a simple product but for a configurable product, it's not working.

My simple objective is to add line items irrespective of quantity. If two products already added to cart, it should be the separate line item in the cart.

1 Answer 1


Above solution work for simple product but in case of configurable product we have to add addition index to array $additionalOptions like $additionalOptions[]['setting_style'] = [ 'label' => "Unique Product", 'value' => $productInfo->getId() . "_" . rand()."_".$productInfo->getQty() ]; But this will also give error for label not found because we have added addition index to the option. so we have to override the files and update the array wherever label and value of the options is called in file Magento\Quote\Model\Cart\Totals\ItemConverter line no 114 add bellow code if(isset($optionValue['setting_style'])){ $optionValue = $optionValue['setting_style']; }

same in the other two files Magento\Checkout\Model\DefaultConfigProvider & Magento\Catalog\Helper\Product\Configuration. Do not forgot to override the files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.