How to include a custom discount in Magento 2

The following steps will explain you How to include a custom discount in Magento 2

  • Register a total in a file sales.xml
  • Add discount to modify the grand total in the model
  • Add total to layout layouts
  • Check out the model knock out

Include a custom discount in Magento 2

  • First of all , we will register the entire file sales.xml 
app/code/Webnexs/Webpos/etc/sales.xml:
<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="Webnexs\Webpos\Model\Total\Quote\Custom" sort_order="420"/>

   </group>

 </section>

</config>
  • Then we will Add discount to modify the grand total in the model
app/code/Webnexs/Webpos/Model/Total/Quote/Custom.php:
<?php

namespace Webnexs\Webpos\Model\Total\Quote;

/**

* Class Custom

* @package Webnexs\Webpos\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;

   }

}
  • Now, you will observe the Grand Total was modified. But there is no total discount information. Because the magento use knockout js to explain the total. We require to include the total in the layout file
app/code/Webnexs/Webpos/view/frontend/layout/checkout_cart_index.xml:
<?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">Webnexs_Webpos/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>
  • It will call view model knockout
app/code/Webnexs/Webpos/view/frontend/web/js/view/checkout/summary/customdiscount.js
define(

   [

       'jquery',

       'Magento_Checkout/js/view/summary/abstract-total'

   ],

   function ($,Component) {

       "use strict";

       return Component.extend({

           defaults: {

               template: 'Webnexs_Webpos/checkout/summary/customdiscount'

           },

           isDisplayedCustomdiscount : function(){

               return true;

           },

           getCustomDiscount : function(){

               return '$10';

           }

       });

   }

);
  • And the total discount will be illustrate in the template knockout
app/code/Webnexs/Webpos/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 -->
  • You can observe it like my screenshot:Image result for shopping cart in magento

Last Update: April 5, 2018  

February 27, 2018   267   Nandini R    Magento2    
Total 2 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
INSTAGRAM
LinkedIn