Skip to main content

Programmatically applying coupon in Magento when product added to cart

· 3 min read

In this article, we will see how to apply custom coupon code in magento cart programmatically. For that, we will write a simple Magento plugin that programmatically applies a specific coupon when a user adds a particular product to the cart.

Sales rule (coupon) creation

In this example, we created a simple sales rule with a coupon code "disc10" that applies 10% on a Wayfayer Bag (that has an SKU 24-MB05).

Here are the fields of the sales rule that we created:

  • Coupon code: disc10
  • Conditions Tab: empty
  • Actions Tab: Sku is 24-MB05

Here is the screenshot of the cart rule:

Rule Information:

The Information tab of the coupon sales rule

Conditions and Actions Tabs:

The Conditions and Actions Tabs of the coupon sales rule

Plugin creation

Now, we will create a simple Magento extension that will apply the coupon code "disc10" programmatically once the specific product is added to the cart.

Standard plugin files creation

To do that, let's first create the standard plugin files in the app/code/Mexbs/ApplyCouponProgrammatically folder in our Magento installation:

app/code/Mexbs/ApplyCouponProgrammatically/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Mexbs_ApplyCouponProgrammatically" setup_version="1.0.0" />
</config>

app/code/Mexbs/ApplyCouponProgrammatically/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Mexbs_ApplyCouponProgrammatically',
__DIR__
);

Observer files creation

Now, let's create an observer that will programmatically set the coupon code to the quote once the user adds the specific product to the cart.

For that, we will hook to the checkout_cart_product_add_after event that is triggered after the product is added to the cart in Magento\Checkout\Model\Cart::addProduct.

app/code/Mexbs/ApplyCouponProgrammatically/etc/events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="checkout_cart_product_add_after">
<observer name="mexbsApplyCouponAfterAddProduct" instance="Mexbs\ApplyCouponProgrammatically\Observer\ApplyCouponAfterAddProduct" />
</event>
</config>

Now, let's create the observer class to perform the coupon application.

app/code/Mexbs/ApplyCouponProgrammatically/Observer/ApplyCouponAfterAddProduct.php
<?php
namespace Mexbs\ApplyCouponProgrammatically\Observer;

use Magento\Framework\Event\ObserverInterface;

class ApplyCouponAfterAddProduct implements ObserverInterface{
public function execute(\Magento\Framework\Event\Observer $observer)
{
$quoteItem = $observer->getEvent()->getQuoteItem();
$product = $observer->getEvent()->getProduct();

$wayfayerBagSku = '24-MB05';
$couponCode = 'disc10';

if($product->getSku() == $wayfayerBagSku){
$quoteItem->getQuote()->setCouponCode($couponCode)->collectTotals();
}
}
}

Installing the module

Now, we need to run the usual commands to enable the module, compile the code, etc:

php bin/magento module:enable Mexbs_ApplyCouponProgrammatically
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush

Testing the module

To test our plugin, let's add the Wayfayer bag to the cart. We will see that the coupon code "disc10" was applied automatically:

The coupon coe disc10 is applied in the cart with the Wayfayer bag in it

To sum up

In this article, we showed how to create a simple extension that allows programmatically apply coupon magento when a product is added to a cart.

info

This code was tested on Magento CE 2.4.7-p3