Let’s initiate a discussion!!
1) Create a sales.xml file on app/code/Vendor/module/etc
1 2 3 4 5 6 7 |
<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="promodiscount" instance="Vendor\Module\Model\Quote\Discount" sort_order="500"/> </group> </section> </config> |
2) Create file Discount.php on this path app\code\Vendor\Module\Model\Quote\Discount
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
namespace Vendor\Module\Model\Quote; class Discount extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal { 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 = -20; $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) { $customDiscount = 20; return [ 'code' => 'Custom_Discount', 'title' => $this->getLabel(), 'value' => $customDiscount ]; } /** * get label * @return string */ public function getLabel() { return __('Custom Discount'); } } |
3) Create file checkout_cart_index.xml on path app\code\Vendor\Module\view\frontend\layout
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?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_Module/js/view/checkout/summary/custom-discount</item> <item name="sortOrder" xsi:type="string">30</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> |
4) Create file checkout_index_index.xml on path app\code\Vendor\Module\view\frontend\layout
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
<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_Module/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_Module/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> |
5) Create file custom-discount.js on path app/code/Vendor/Module/view/frontend/web/js/view/checkout/cart/totals
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
define( [ 'Vendor_Module/js/view/checkout/summary/custom-discount' ], function (Component) { 'use strict'; return Component.extend({ /** * @override */ isDisplayed: function () { return true; } }); } ); |
6) Create file custom-discount.html on path app/code/Vendor/Module/view/frontend/web/template/checkout/cart/totals
1 2 3 4 5 6 7 8 |
<!-- 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 --> |
7) Create file on this path custom-discount.js app/code/Vendor/Module/view/frontend/web/js/view/checkout/summary
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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_Module/checkout/summary/custom-discount' }, totals: quote.getTotals(), isDisplayedCustomdiscountTotal : function () { return true; }, getCustomdiscountTotal : function () { var price = totals.getSegment('Custom_Discount').value; return this.getFormattedPrice(price); } }); } ); |
8) Create file custom-discount.html on path app/code/Vendor/Module/view/frontend/web/template/checkout/summary
1 2 3 4 5 6 7 8 |
<!-- 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 --> |
[crayon-641fb3bdcf920435713035/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.