We use cookies to make your experience better. To comply with the new e-Privacy directive, we need to ask for your consent to set the cookies. Learn more.
How to Add Custom Discount in Magento 2
Enter a total in the sale.xml file
Directory
app/code/VendorName/ModuleName/etc/sales.xml
Content for this file
<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="customer_discount" instance="VendorName\ModuleName\Model\Total\Quote\Custom" sort_order="420"/>
</group>
</section>
</config>
Set the value of the discount
Directory
app/code/VendorName/ModuleName/Model/Total/Quote/Custom.php
Content for this file
<?php
namespace VendorName\ModuleName\Model\Total\Quote;
/**
* Class Custom
* @package VendorName\ModuleName\Model\Total\Quote
*/
class Custom extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
{
/**
* @var \Magento\Framework\Pricing\PriceCurrencyInterface
*/
protected $_priceCurrency;
/**
* Custom constructor.
* @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
*/
public function __construct(
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
){
$this->_priceCurrency = $priceCurrency;
}
/**
* @param \Magento\Quote\Model\Quote $quote
* @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
* @param \Magento\Quote\Model\Quote\Address\Total $total
* @return $this|bool
*/
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);
$baseDiscount = 10;
$discount = $this->_priceCurrency->convert($baseDiscount);
$total->addTotalAmount('customdiscount', -$discount);
$total->addBaseTotalAmount('customdiscount', -$baseDiscount);
$total->setBaseGrandTotal($total->getBaseGrandTotal() - $baseDiscount);
$quote->setCustomDiscount(-$discount);
return $this;
}
}
Show the custom discount information in the layout file
Directory
app/code/VendorName/ModuleName/view/frontend/layout/checkout_cart_index.xml
Content for this file
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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="custom_discount" xsi:type="array">
<item name="component" xsi:type="string">VendorName_ModuleName/js/view/checkout/summary/customdiscount</item>
<item name="sortOrder" xsi:type="string">20</item>
<item name="config" xsi:type="array">
<item name="custom_discount" xsi:type="string" translate="true">Custom Discount</item>
</item>
</item>
</item>
</item>
</item>
</argument>
</arguments>
</referenceBlock>
</body>
</page>
View model knockout
Directory
app/code/VendorName/ModuleName/view/frontend/web/js/view/checkout/summary/customdiscount.js
Content for this file
define(
[
'jquery',
'Magento_Checkout/js/view/summary/abstract-total'
],
function ($,Component) {
"use strict";
return Component.extend({
defaults: {
template: 'VendorName_ModuleName/checkout/summary/customdiscount'
},
isDisplayedCustomdiscount : function(){
return true;
},
getCustomDiscount : function(){
return '$10';
}
});
}
);
Create the template knockout,
app/code/VendorName/ModuleName/view/frontend/web/template/checkout/summary/customdiscount.html
<!-- ko if: isDisplayedCustomdiscount() -->
<tr class="totals customdiscount excl">
<th class="mark" colspan="1" scope="row" data-bind="text: custom_discount"></th>
<td class="amount">
<span class="price" data-bind="text: getCustomDiscount(), attr: {'data-th': custom_discount}"></span>
</td>
</tr>
<!-- /ko -->
If you are looking for Magento Developers, visit Magento Developer Agency.