Applying Discounts Directly to Subtotal in Magento
Discounts are an essential part of e-commerce that can significantly impact your sales strategy. If you want to offer discounts directly on the subtotal for logged-in customers who have purchased certain products, this guide will help you achieve just that.
Understanding Magento’s Default Discount Mechanisms
Magento comes with built-in discount functionality, but it typically works based on rules applied at the shopping cart level or via coupons. These methods require users to add a coupon code or meet specific conditions before seeing the discounted price.
However, if you want a more personalized and immediate discount experience for certain customers, you’ll need to dive deeper into Magento’s core functionalities.
The Custom Discount Process: An Overview
To apply a discount directly to the subtotal on the fly, we’ll create a custom module that intercepts the quote update process. Here’s an overview of what we’ll do:
- Create a custom module.
- Override the
quote_updateevent. - Check if the customer meets your criteria for the discount.
- Apply the discount to the subtotal.
Creating and Configuring Your Custom Module
First, let’s create a new module called DiscountOnTheFly. Follow these steps:
-
Create a directory structure under
app/code/LaravelCompany/DiscountOnTheFly. -
Inside the
etcfolder, create a file namedmodule.xml:
<config xmlns: xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="LaravelCompany_DiscountOnTheFly" setupVersion="1.0.0">
<sequence>
<depends/>
</sequence>
</module>
</config>
- Enable the module in your Magento installation.
Overriding the quote_update Event
Now, let’s override the quote_update event to apply our discount logic. Create a file named di.xml inside the app/code/LaravelCompany/DiscountOnTheFly/etc directory:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Quote\Model\CartManagement">
<plugin name="LaravelCompany_DiscountOnTheFly_applyDiscount"
type="LaravelCompany\DiscountOnTheFly\Model\Observer"
sortOrder="10"/>
</type>
</config>
Here, we’re adding a plugin for the Magento\Quote\Model\CartManagement class.
Implementing the Discount Logic in the Observer
Create a file named Observer.php inside the app/code/LaravelCompany/DiscountOnTheFly/Model directory:
namespace LaravelCompany\DiscountOnTheFly\Model;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
class Observer implements ObserverInterface
{
public function execute(Observer $observer)
{
$quote = $observer->getQuote();
if ($this->shouldApplyDiscount($quote)) {
$discountAmount = 50; // Set your desired discount amount here.
$quote->setSubtotal($quote->getSubtotal() - $discountAmount);
}
}
private function shouldApplyDiscount(\Magento\Quote\Model\Quote $quote): bool
{
// Implement your criteria for applying the discount here.
// For example, you can check if a specific product is in the cart or if it's a logged-in customer.
return true; // Replace this with your actual logic.
}
}
In the execute method, we’re checking if the customer meets our criteria (shouldApplyDiscount()), and if so, we subtract the discount amount from the subtotal. The shouldApplyDiscount() method is where you’ll implement your personalized rules for applying the discount.
Conclusion
Applying discounts directly to the subtotal in Magento requires a custom approach that involves overriding core functionalities. By following this guide and creating a custom module, you can provide logged-in customers with immediate discounts on their cart totals based on your specific criteria.
Remember to replace the shouldApplyDiscount() method with your actual discount rules, and adjust the $discountAmount variable according to your business requirements.
As always, consult the Magento documentation for more information on working with events and plugins: https://laravelcompany.com/magento-development/working-with-events-and-plugins-in-magento-2.
If you’re looking for professional Magento development services, feel free to reach out to Laravel Company. Our team of experienced developers can help you implement advanced discount strategies and optimize your e-commerce platform.