How to Add Product With Custom Option to Cart Programmatically in Magento 2
In this code snippet, we will see how we can add a product to cart along with custom options. For that, we will create one controller file, In that file, we will define execute method. In this method using cart model, we will add a product to cart.
Create Controller
In your custom module create a controller file Post.php under app/code/Codextblog/Customcart/Controller/Index
directory
namespace Codextblog\Customcart\Controller\Index; use Magento\Framework\App\Action\Context; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\App\RequestInterface; class Post extends \Magento\Framework\App\Action\Action { protected $_resultPageFactory; protected $_cart; protected $_productRepositoryInterface; protected $_url; protected $_responseFactory; protected $_logger; public function __construct( Context $context, \Magento\Framework\View\Result\PageFactory $resultPageFactory, \Magento\Checkout\Model\Cart $cart, \Magento\Catalog\Api\ProductRepositoryInterface $productRepositoryInterface, \Magento\Framework\App\ResponseFactory $responseFactory, \Psr\Log\LoggerInterface $logger ) { $this->_resultPageFactory = $resultPageFactory; $this->_cart = $cart; $this->_productRepositoryInterface = $productRepositoryInterface; $this->_responseFactory = $responseFactory; $this->_url = $context->getUrl(); $this->_logger = $logger; parent::__construct($context); } public function execute() { $productid = 111; $_product = $this->_productRepositoryInterface->getById($productid); $options = $_product->getOptions(); $customoptions = array(); $customoptions['my_custom_option'] = 'demo'; $params = array ( 'product' => $_product->getId(), 'qty' => 1, 'price' => $_product->getPrice(), 'options' => $customoptions ); $this->_cart->addProduct($_product, $params); $this->_cart->save(); $this->_redirect('checkout/cart/index'); } }
Update Minicart
In Magento 2 mini cart is update using knockout js. While adding a product to cart programmatically we need to specify the controller action in sections.xml file so that Magento 2 update the mini cart as well.
create sections.xml file under app/code/Codextblog/Customcart/etc/frontend
directory
<?xml version="1.0" encoding="UTF-8"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd"> <action name="customcart/index/post"> <section name="cart"/> </action> </config>
That’s it. Using above code we can easily add product to cart programmatically.
Leave a Comment
(5 Comments)
It Worked But Custom options are still not coming.
Not working with foreach loop in the case of multiple products
It worked
How to update the added Product With Custom Option to Cart Programmatically in Magento 2
can you give me this full module?
Useful Magento 2 Articles
Author Info
Chirag
Connect With MeWas this post helpful? Please support Us!
To Avoid Spam Downloads, We Want Your Email
away. Please submit form below.
Magento 2
Knowledge
to your Inbox