How to Add Product to Cart Programmatically With Custom Options in Magento 2
Earlier, I showed you How to Add Magento 2 Configurable Products Programmatically to Cart
Now, what if you are required to add product to cart programmatically with custom options in Magento 2?
Crazy business requirements, isn’t it? 😰
I get you, fellow developers 😄
Moreover, this solution can also be helpful if you want an automatic “add to cart” functionality for the configurable product with custom options. For example, the store is offering a free product with a particular product, attach a warranty, or add a gift product!
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! It can be a bit tricky. Along with custom options you can also add product to cart with custom price that allows store to create products with custom price in their online store.
Here’s the solution for the same:
Method to Add Product to Cart Programmatically With Custom Options in Magento 2:
- Create registration.php file in app\code\[Vendor]\[Namespace]\
123456<?php\Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,'[Vendor]_[Namespace]',__DIR__); - Create module.xml file in app\code\[Vendor]\[Namespace]\etc
1234<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"><module name="[Vendor]_[Namespace]" setup_version="1.0.0"/></config> - Create Data.php file in app\code\[Vendor]\[Namespace]\Helper
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647<?phpnamespace [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;parent::__construct($context);}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);$cart->save();}}
You can use the above helper method by passing product id in anywhere in Magento 2.
That’s it.
Do let me know in the Comments section below if you have issues in the implementation of the given solution. I’d be happy to help.
Thank you.
Also read:
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
5 Comments
HI ,
Error: Call to undefined method Magento\Catalog\Model\ProductFactory::load() not working Magento 2.4
Hey,
Please try the above solution again as it is working perfectly for us.
Thanks
I tried this code but not working.
throw error you need to choose options for your item.
Hi,
Above code is not working for me.It given the error like this “You need to choose options for your item”.
Can you help me to resolve this error?
Please set proper custom options ID for the product you are trying to add programmatically.