I want to apply multiple discount in separate line.Right now i have coupn code and sales rule applied in same cart what should i do so cart and checkout page display relevant discount in separate line in magento2.
1 Answer
1) Create checkout_cart_index
<?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>
<referenceContainer name="content">
<block class="Ktpl\Extrafee\Block\Discounts" name="block.banner" as="block.custom.cms" template="Vendor_Module::discounts.phtml" before="-" />
<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="before_grandtotal" xsi:type="array">
<item name="children" xsi:type="array">
<item name="fee" xsi:type="array">
<item name="component" xsi:type="string">Vendor_Module/js/view/checkout/cart/totals/fee</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/fee</item>
<!-- <item name="title" xsi:type="helper" translate="true" helper="Ktpl\Extrafee\Helper\Data::getTitle"/> -->
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</argument>
</arguments>
</referenceBlock>
</referenceContainer>
</body>
</page>
2) Now create fee.js
at
app/code/Vendor/Module/view/frontend/web/js/view/checkout/cart/totals
define(
[
'Ktpl_Extrafee/js/view/checkout/summary/fee'
],
function (Component) {
'use strict';
return Component.extend({
/**
* @override
*/
isDisplayed: function () {
return true;
},
discounts: function() {
var alldiscount = window.discounts;
return alldiscount;
}
});
}
);
3) Create fee.html
at
app/code/Vendor/Module/view/frontend/web/template/checkout/cart/totals/fee.html
<!-- ko -->
<tr>
<td>
<table data-bind="foreach: discounts()">
<tr class="totals fee excl" >
<th class="mark" colspan="1" scope="row" data-bind="text: title"></th>
<td class="amount">
<span class="price" data-bind="text: amount"></span>
</td>
</tr>
</table>
</td>
</tr>
<!-- /ko -->
4) Now create discounts and pass your array of discount
app/code/Vendor/Module/view/frontend/templates/discounts.phtml
<?php
$totalDiscounts = $block->getAllDiscounts();
/* Your array should look like this
$discounts = [
['title' => 'First Discount', 'amount' => '-10' ],
[ 'title' => 'Second Discount', 'amount' => '-5' ]
]; */
?>
<script>
window.discounts = <?php /* @escapeNotVerified */ echo \Zend_Json::encode($totalDiscounts) ?>;
</script>
-
2