Hello Everyone,
In this blog, we will learn about Add Custom Discount Programmatically in Magento 2.
Sometimes business goes slow, at that time business owners are required to implement sales promotion.
Programmatically Add Custom Discount in Magento 2 is a part of marketing strategies to increase sales.
Without wasting your time, let us guide you straight away. Follow the easy step given below to Add Pagination in Custom Collection in Magento 2.
STEPS FOR ADD CUSTOM DISCOUNT PROGRAMMATICALLY IN MAGENTO 2
Step 1: Create sales.xml file
app/code/Vendor/Extension/etc/sales.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/sales.xsd">
<section name="quote">
<group name="totals">
<item name="customdiscount" instance="Vendor\Extension\Model\Quote\Address\Total\CustomDiscount" sort_order="400"/>
</group>
</section>
</config>
Step 2: Create Custom Discount.php file
app/code/Vendor/Extension/Model/Quote/Address/Total/CustomDiscount.php
<?php
namespace Vendor\Extension\Model\Quote\Address\Total;
class CustomDiscount extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
{
/**
* @var \Magento\Framework\Pricing\PriceCurrencyInterface
*/
protected $_priceCurrency;
/**
* @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency [description]
*/
public function __construct(
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
) {
$this->_priceCurrency = $priceCurrency;
}
public function collect(
\Magento\Quote\Model\Quote $quote,
\Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
\Magento\Quote\Model\Quote\Address\Total $total
) {
parent::collect($quote, $shippingAssignment, $total);
$customDiscount = -10;
$total->addTotalAmount('customdiscount', $customDiscount);
$total->addBaseTotalAmount('customdiscount', $customDiscount);
$quote->setCustomDiscount($customDiscount);
return $this;
}
/**
* Assign subtotal amount and label to address object
*
* @param \Magento\Quote\Model\Quote $quote
* @param Address\Total $total
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
{
return [
'code' => 'Custom_Discount',
'title' => $this->getLabel(),
'value' => 10
];
}
/**
* get label
* @return string
*/
public function getLabel()
{
return __('Custom Discount');
}
}
Step 3: Create checkout_cart_index.xml file
app/code/Vendor/Extension/view/frontend/layout/checkout_cart_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="checkout.cart.totals">
<arguments>
<argument name="jsLayout" xsi:type="array">
<item name="components" xsi:type="array">
<item name="block-totals" xsi:type="array">
<item name="children" xsi:type="array">
<item name="customdiscount" xsi:type="array">
<item name="component" xsi:type="string">Vendor_Extension/js/view/checkout/summary/custom-discount</item>
<item name="sortOrder" xsi:type="string">20</item>
<item name="config" xsi:type="array">
<item name="customdiscount" xsi:type="string" translate="true">Custom Discount</item>
</item>
</item>
</item>
</item>
</item>
</argument>
</arguments>
</referenceBlock>
</body>
</page>
Step 4: Create checkout_index_index.xml file
app/code/Vendor/Extension/view/frontend/layout/checkout_index_index.xml
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="checkout.root">
<arguments>
<argument name="jsLayout" xsi:type="array">
<item name="components" xsi:type="array">
<item name="checkout" xsi:type="array">
<item name="children" xsi:type="array">
<item name="sidebar" xsi:type="array">
<item name="children" xsi:type="array">
<item name="summary" xsi:type="array">
<item name="children" xsi:type="array">
<item name="totals" xsi:type="array">
<item name="children" xsi:type="array">
<item name="customdiscount" xsi:type="array">
<item name="component" xsi:type="string">Vendor_Extension/js/view/checkout/cart/totals/custom-discount</item>
<item name="sortOrder" xsi:type="string">20</item>
<item name="config" xsi:type="array">
<item name="template" xsi:type="string">Vendor_Extension/checkout/cart/totals/custom-discount</item>
<item name="title" xsi:type="string" translate="true">Custom Discount</item>
</item>
</item>
</item>
</item>
<item name="cart_items" xsi:type="array">
<item name="children" xsi:type="array">
<item name="details" xsi:type="array">
<item name="children" xsi:type="array">
<item name="subtotal" xsi:type="array">
<item name="component" xsi:type="string">Magento_Tax/js/view/checkout/summary/item/details/subtotal</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</argument>
</arguments>
</referenceBlock>
</body>
</page>
Step 5: Create custom-discount.js file
app/code/Vendor/Extension/view/frontend/web/js/view/checkout/cart/totals/custom-discount.js
define(
[
'Vendor_Extension/js/view/checkout/summary/custom-discount'
],
function (Component) {
'use strict';
return Component.extend({
/**
* @override
*/
isDisplayed: function () {
return true;
}
});
}
);
Step 6: Create custom-discount.js file
app/code/Vendor/Extension/view/frontend/web/js/view/checkout/summary/custom-discount.js
define(
[
'jquery',
'Magento_Checkout/js/view/summary/abstract-total',
'Magento_Checkout/js/model/quote',
'Magento_Checkout/js/model/totals',
'Magento_Catalog/js/price-utils'
],
function ($,Component,quote,totals,priceUtils) {
"use strict";
return Component.extend({
defaults: {
template: 'Vendor_Extension/checkout/summary/custom-discount'
},
totals: quote.getTotals(),
isDisplayedCustomdiscountTotal : function () {
return true;
},
getCustomdiscountTotal : function () {
var price = 10;
return this.getFormattedPrice(price);
}
});
}
);
Step 7: Create custom-discount.html file
app/code/Vendor/Extension/view/frontend/web/template/checkout/cart/totals/custom-discount.html
<!-- ko if: isDisplayedCustomdiscountTotal() -->
<tr class="totals customdiscount excl">
<th class="mark" colspan="1" scope="row" data-bind="text: title"></th>
<td class="amount">
<span class="price" data-bind="text: getCustomdiscountTotal()"></span>
</td>
</tr>
<!-- /ko →
Step 8: Create custom-discount.html file
app/code/Vendor/Extension/view/frontend/web/template/checkout/summary/custom-discount.html
<!-- ko if: isDisplayedCustomdiscountTotal() -->
<tr class="totals coupon-discount excl">
<th class="mark" colspan="1" scope="row" data-bind="text: customdiscount"></th>
<td class="amount">
<span class="price" data-bind="text: getCustomdiscountTotal(), attr: {'data-th': customdiscount}"></span>
</td>
</tr>
<!-- /ko -->
Step 9: Finally run the below commands
$ php bin/magento setup:upgrade
$ php bin/magento cache:clean
$ php bin/magento cache:flush
Step 10: Output:
In Cart Page:
In Checkout Page:
Final Thoughts:
So this was the easiest way which we have told you in this blog. This is how you can Add Custom Discount in Magento 2. Hope you liked the blog.
So quickly go to the comment box and tell me how you like this blog?
Stay tuned with us on our site to get new updates of Magento.
Thanks for reading and visiting our site.
