Get 15% off Mageplaza extensions & subscriptions! Use code HIMAY at checkout.

How to Add Product to Cart Programmatically in Magento 2?

Vinh Jacker | 12-05-2017

The Most Popular Extension Builder for Magento 2

With a big catalog of 224+ extensions for your online store

The product page is the most important aspects of any ecommerce store as it contains all the details about the products.

However, if the appearance of your product page is not up to the mark, you can lose a lot of sales. An attractive front end of your store can make your customers stay and can help in boosting sales. The look of add to cart button also plays a vital role in making the product page look attractive.

Magento 2 is one of the most used ecommerce platforms, so in this guide I am going to show you how to customize Add to cart button in Magento 2. You also can custom Add to Cart button to other types by Call for Price extension

Call for Price

Call for Price for Magento 2

Unlock the potential of your product sales with our custom pricing options on Magento 2

Check it out!

I will implement the procedure using a custom module. So let’s start by creating a custom module:

Related Topics

Configuration and Registration of Module

  • Create module.xml in app/code/Magenticians/Mymodule/etc:
<?xml version="1.0"?>
	<configxmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
	<module name="Magenticians_Mymodule" setup_version="1.0.0"></module>
  • Create registration.php in app/code/Magenticians/Mymodule:

Copy addtocart.phtml File

Copy addtocart.phtml file from vendor/magento/module-catalog/view/frontend/templates/product/view paste this file in app/code/Magenticians/Mymodule/view/frontend/templates/catalog/product/view.

Now create catalog_product_view.xml file in app/code/Magenticians/Mymodule/view/frontend/layout and add this code in the file:

<?xml version="1.0"?>
<page xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">

       <referenceBlock name="">
           <action method="setTemplate">
               <argument name="template" xsi:type="string">Magenticians_Mymodule::catalog/product/view/addtocart.phtml</argument>

I have added colorcart.css file which I will use to customize add to cart button.

Edit addtocart.phtml

Now open addtocart.phtml file by going to app/code/Magenticians/Mymodule/view/frontend/templates/catalog/product/view and add css class newcolor in the button tag:

* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.

// @codingStandardsIgnoreFile

/** @var $block \Magento\Catalog\Block\Product\View */
<?php $_product = $block->getProduct(); ?>
<?php $buttonTitle = __('Add to Cart'); ?>
<?php if ($_product->isSaleable()): ?>
<div class="box-tocart">
   <div class="fieldset">
       <?php if ($block->shouldRenderQuantity()): ?>
       <div class="field qty">
           <label class="label" for="qty"><span><?= /* @escapeNotVerified */ __('Qty') ?></span></label>
           <div class="control">
               <input type="number"
                      value="<?= /* @escapeNotVerified */ $block->getProductDefaultQty() * 1 ?>"
                      title="<?= /* @escapeNotVerified */ __('Qty') ?>"
                      class="input-text qty"
                      data-validate="<?= $block->escapeHtml(json_encode($block->getQuantityValidators())) ?>"
       <?phpendif; ?>
       <div class="actions">
           <button type="submit"
                   title="<?= /* @escapeNotVerified */ $buttonTitle ?>"
                   class="action primary tocartnewcolor"
               <span><?= /* @escapeNotVerified */ $buttonTitle ?></span>
           <?= $block->getChildHtml('', true) ?>
<?phpendif; ?>
<?php if ($block->isRedirectToCartEnabled()) : ?>
<script type="text/x-magento-init">
       "#product_addtocart_form": {
           "Magento_Catalog/product/view/validation": {
               "radioCheckboxClosest": ".nested"
<?php else : ?>
<script type="text/x-magento-init">
       "#product_addtocart_form": {
           "Magento_Catalog/js/validate-product": {}
<?phpendif; ?>

Create CSS file

Now create a css file colorcart.css in app/code/Magenticians/Mymodule/view/frontend/web/css and add the following code in it:

   background: #29487d !important;
   box-shadow: 1px 5px 5px 1px rgba(66, 103, 178, 0.51);
   font-size: 23px !important;
   font-weight: bold !important;
   border-radius: 10px;
   height: 60px;
   font-family: cursive !important;

Now go to your Magento 2 root directory through SSH terminal and run these commands:

php bin/magentomodule:enableMagenticians_Mymodule
php bin/magentosetup:upgrade
php bin/magentosetup:di:compile
php bin/magentosetup:static-content:deploy
php bin/magentocache:clean
php bin/magentocache:flush

Launch the product’s front page and you will see the new design applied to the add to cart button:

new design applied to the add to cart button

Wrapping Up

I have demonstrated just one example, but you can design your Add to cart button according to your intention. With the help of this guide, I believe that you can now customize Add to cart button in Magento 2. If you want to discuss something related to this tutorial, just leave a message in the comment box below and I will get back to you.

Customize Add to Cart button with Mageplaza extension


Hello, I'm the Chief Technology Officer of Mageplaza, and I am thrilled to share my story with you. My deep love and passion for technology have fueled my journey as a professional coder and an ultra-marathon runner. Over the past decade, I have accumulated extensive experience and honed my expertise in PHP development.

Related Post

Website Support & Maintenance Services

mageplaza services

Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.