Add Product To Cart Programmatically With Custom Options Step-by-step

by Grace Bui

Add product to cart programmatically with custom options is a valuable feature in Magento 2.  Consumer competition on the internet is no longer exceptional, thus it’s become vital to choose the most appealing product design. Customers today demand to see all product options (e.g., available colors, size, material, and so on) before making a purchase choice. Adding a variety of options to your products is the best method to suit their needs. Magento 2 custom choices extension is the key to attracting more customers and giving them the feeling that they always have a choice on the Magento 2 platform.

So let’s dive into our guide to add product to the cart programmatically with custom options!

What Are Custom Options?

Customizable options are features that give unique characteristics to simple products (as well as downloadable and virtual products) in Magento 2. 

Simple products, which make up the foundation of a Magento 2 store, are stand-alone items that can be sold separately or as part of more complicated entities like grouped, bundled, and customizable products. The same is true for downloadable and virtual products that are not physically present. One of the types’ peculiarities is that merchants are unable to sell products that have many attribute sets within a single product. As a result, the variety of the products is limited. 


Magento does support custom options for several product types – by its nature. For example, when you sell clothes, color and size choices are custom options that help customers pick items that suit their needs. You can increase the flexibility of your offers by selecting custom options.

READ NOW >>> Compare Magento 2 Layered Navigation Extensions Free & Paid if you want to know more about custom options

Why We Need Custom Options?

Consider this scenario: clients visit a website, and the only product information available is the name of the product and the amount to be purchased.

In the viewpoint of customers, this renders them blank and useless!

Customized options, as shown in the image above, will aid in making the information associated with your product more comprehensive than ever before.

Customers will feel more at ease if they have a variety of options for the things they need to buy, from color to material to even size.

When a consumer buys a product with configurable options, a description of each option displays underneath the product description, and any corresponding markup (or markdown) is applied to the item’s price automatically.


If the purchase triggers a cart price rule, the original calculation is applied first to the product price and then to the line item price, with any adjustments made for any customizable options. In the following example, the buyer pays $74.00 for a duffle bag with a monogram customization option. The adjusted price is $88.80 after a $14.80 markup is applied to the original product price.

The purchase of the duffle bag, in this scenario, activates a cart price rule based on the product SKU, which gives the customer a discount and free shipping. Despite the fact that the customized option does not activate the cart price rule, it does apply the discount to the cart items, which includes the markup for the customizable option.

Then your website’s consumer experience will improve! As a result, you can increase your conversation rate and purchases. 

You can CHECK >>> Custom Product Labels For Magento 2 Tips To Steal NOW! to have more information.

How To Add Product To Cart Programmatically With Custom Options

  1. Create registration.php file in app\code\[Vendor]\[Namespace]\






2. Create module.xml file in app\code\[Vendor]\[Namespace]\etc

<?xml version="1.0"?>

<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">

<module name="[Vendor]_[Namespace]" setup_version="1.0.0"/>



How-to-Add-Product-to-Cart-Programmatically-With-Custom-Options-in-Magento-23. Create Data.php file in app\code\[Vendor]\[Namespace]\Helper

namespace [Vendor]\[namespace]\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
use Magento\Checkout\Model\Cart;
use Magento\Catalog\Model\ProductFactory;
class Data extends AbstractHelper
    private $cart
    private $productFactory;
    public function __construct(Context $context, Cart $cart, ProductFactory $productFactory)
        $this->productFactory = $productFactory;
        $this->cart = $cart;
    public function getAddCustomProduct($productId)

        $product = $this->productFactory->load($productId);
        $cart = $this->cart;
        $params = array();

        $options = array();

        $params['qty'] = 1;

        $params['product'] = $productId;

        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);




That is how you can add product to cart programmatically with custom options. Take your chance now and see what you can do! 

READ NOW >>> Magento 2 Product Image: Top 4 Practices To Boost Your Sale! to have more details. 

BSS Commerce is one of the leading Multi-platform eCommerce solutions and web development services providers in the world. With experienced and certified developers, we commit to bring high-quality products and services to optimize your business effectively.

CONTACT NOW to let us know your problems. We are willing to support you every time.

Next Reading Suggestions

© 2019 BSS Commerce owned by THANH CONG INTER ., JSC. All Rights Reserved.
Business registration certificate no. 0106064469 issued by Hanoi Department of Planning and Investment on 19 December 2019.
Legal Representative: Mr. Nguyen Quang Trung.