Programmatically applying coupon in Magento when product added to cart
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:
Conditions and Actions Tabs:
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:
<?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>
<?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.
<?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.
<?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:
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.
This code was tested on Magento CE 2.4.7-p3