{"id":480,"date":"2019-06-27T07:30:03","date_gmt":"2019-06-27T07:30:03","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/06\/27\/add-product-to-cart-with-custom-options-in-magento-2\/"},"modified":"2025-05-22T16:30:15","modified_gmt":"2025-05-22T11:00:15","slug":"add-product-to-cart-with-custom-options-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-product-to-cart-with-custom-options-in-magento-2\/","title":{"rendered":"How to Add Product to Cart Programmatically With Custom Options in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Earlier, I showed you&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/add-magento-2-configurable-products-to-cart\/\">How to Add Magento 2 Configurable Products Programmatically to Cart<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, what if you are required to&nbsp;<em><strong>add product to cart programmatically with custom options in Magento 2<\/strong><\/em>?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Crazy business requirements, isn\u2019t it?&nbsp; <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I get you, fellow developers  <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Moreover, this solution can also be helpful if you want an automatic \u201cadd to cart\u201d functionality for the configurable product with custom options. For example, the store is offering a free product with a particular product,&nbsp;attach a warranty, or add a gift product!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But what if such products you are adding to the Magento 2 cart are the configurable product! And, if that was not enough, they have custom options too!&nbsp;It can be a bit tricky. Along with custom options you can also&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/set-custom-price-for-products-in-cart-in-magento-2\/\">add product to cart with custom price<\/a>&nbsp;that allows store to create products with custom price in their online store.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s the solution for the same:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Add Product to Cart Programmatically With Custom Options in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<strong>registration.php<\/strong>&nbsp;file in&nbsp;<em><em><strong><em><em><strong>app\\code\\[Vendor]\\[Namespace]\\<\/strong><\/em><\/em><\/strong><\/em><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n    MagentoFrameworkComponentComponentRegistrar::register(\n\t\t\tMagentoFrameworkComponentComponentRegistrar::MODULE,\n\t\t\t'[Vendor]_[Namespace]',\n\t\t\t__DIR__\n\t\t);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<strong>module.xml<\/strong>&nbsp;file in&nbsp;<em><em><strong><em><em><strong>app\\code\\[Vendor]\\[Namespace]\\etc<\/strong><\/em><\/em><\/strong><\/em><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n\t&lt;config xmlns_xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi_noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n\t\t&lt;module name=\"[Vendor]_[Namespace]\" setup_version=\"1.0.0\"\/>\n\t&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create&nbsp;<strong>Data.php<\/strong>&nbsp;file in&nbsp;<em><em><strong><em><em><strong>app\\code\\[Vendor]\\[Namespace]\\Helper<\/strong><\/em><\/em><\/strong><\/em><\/em><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace [Vendor][namespace]Helper;\n\nuse MagentoFrameworkAppHelperAbstractHelper;\nuse MagentoFrameworkAppHelperContext;\nuse MagentoCheckoutModelCart;\nuse MagentoCatalogModelProductFactory;\n\nclass Data extends AbstractHelper\n{\n    private $cart;\n    private $productFactory;\n    \n    public function __construct(Context $context, Cart $cart, ProductFactory $productFactory)\n    {\n\n        $this->productFactory = $productFactory;\n        $this->cart = $cart;\n        parent::__construct($context);\n\n    }\n\n    public function getAddCustomProduct($productId)\n    {\n        $product = $this->productFactory->load($productId);\n\n        $cart = $this->cart;\n\n        $params = array();\n        $options = array();\n        $params['qty'] = 1;\n        $params['product'] = $productId;\n\n        foreach ($product->getOptions() as $o) {\n            foreach ($o->getValues() as $value) {\n                $options[$value['option_id']] = $value['option_type_id'];\n\n            }\n        }\n        $params['options'] = $options;\n        $cart->addProduct($product, $params);\n        $cart->save();\n\n\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can use the above helper method by passing product id in anywhere in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Earlier, I showed you&nbsp;How to Add Magento 2 Configurable Products Programmatically to Cart Now, what if you are required to&nbsp;add product to cart programmatically with&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-480","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/480","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=480"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/480\/revisions"}],"predecessor-version":[{"id":15364,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/480\/revisions\/15364"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=480"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=480"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=480"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}