Well I spent a lot of hours looking how templates and other stuffs are related with knockout.js
, i will try to explain how subtotal price format is displayed to order summary at checkout module, but is same to other things too. My current version is Magento ver. 2.0.4.
First if you navigate to the corresponding layout file for checkout at
vendor/magento/module-checkout/view/frontend/layout/checkout_index_index.xml
you will see at line 392 inside cart_items
block the subtotal block like this:
As you can see , Magento calls a js template to display subtotal of product right after product details.
If you navigate to
vendor/magento/module-checkout/view/frontend/web/js/view/summary/subtotal.js
you will see at line 15 is called subtotal template , and a function called getValue()
,
getValue: function () {
return this.getFormattedPrice(this.getPureValue());
}
which later i saw it is called in subtotal template , which is located to
vendor/magento/module-checkout/view/frontend/web/template/summary/subtotal.html
with this code:
![enter image description here](https://i.stack.imgur.com/oVhk3.png)
Subtotal.js
file uses
Magento_Checkout/view/frontend/web/js/view/summary/abstract-total.js
getFormattedPrice()
function, while this file uses
Magento_Checkout/view/frontend/web/js/model/quote.js getPriceFormat()
function read about require.js dependencies ,
which returns a price format, and here i saw that this variable is populated like this,
var priceFormat = window.checkoutConfig.priceFormat;
So where does window.checkoutConfig.priceFormat comes from?
Magento 2 did this:
If you navigate to
/vendor/magento/module-checkout/view/frontend/templates/onepage.phtml
at line 26 is initialized window.checkoutConfig with $block->getCheckoutConfig()
, which function is at
/vendor/magento/module-checkout/Block/Onepage.php
line 89, here it takes all config providers and returns them.
Where are config providers defined?
They are defined at
vendor/magento/module-checkout/etc/frontend/di.xml
at line 46, at sends us to
Magento\Checkout\Model\DefaultConfigProvider.php
which has getConfig()
function, at line 254 you can see getConfig()
function and at line 271 is priceFormat populated like this:
$output['priceFormat'] = $this->localeFormat->getPriceFormat(
null,
$this->checkoutSession->getQuote()->getQuoteCurrencyCode()
);
This means priceFormat is declared at populated here at getConfig()
function, so you can later use it with window.checkouConfig.priceFormat
.
You can set line item subtotal using below way, You dont need to use of knockout js anyway you can just setup your goal using below way,
You can get item qty using $qty = $item->getQty();
Now just you have to set $block->getUnitDisplayPriceInclTax()*$qty
to get lineItem subtotal for each item.
You just need to override template file into your theme or module.
I have just setup using override template into theme,
Final Solution,
app/design/frontend/{Packagename}/{themename}/Magento_Weee/templates/checkout/cart/item/price/sidebar.phtml
<?php
/** @var $block \Magento\Weee\Block\Item\Price\Renderer */
$item = $block->getItem();
$originalZone = $block->getZone();
$block->setZone(\Magento\Framework\Pricing\Render::ZONE_CART);
/* custom logic*/
$qty = $item->getQty();
?>
<?php if ($block->displayPriceInclTax() || $block->displayBothPrices()): ?>
<span class="price-including-tax" data-label="<?php echo $block->escapeHtml(__('Incl. Tax')); ?>">
<?php if ($block->displayPriceWithWeeeDetails()): ?>
<span class="minicart-tax-total">
<?php else: ?>
<span class="minicart-price">
<?php endif; ?>
<!-- custom logic -->
<?php /* @escapeNotVerified */ echo $block->formatPrice($block->getUnitDisplayPriceInclTax()*$qty); ?>
</span>
<?php if ($block->displayPriceWithWeeeDetails()): ?>
<?php if ($this->helper('Magento\Weee\Helper\Data')->getApplied($item)): ?>
<span class="minicart-tax-info">
<?php foreach ($this->helper('Magento\Weee\Helper\Data')->getApplied($item) as $tax): ?>
<span class="weee" data-label="<?php /* @escapeNotVerified */ echo $tax['title']; ?>">
<?php /* @escapeNotVerified */ echo $block->formatPrice($tax['amount_incl_tax'], true, true); ?>
</span>
<?php endforeach; ?>
</span>
<?php if ($block->displayFinalPrice()): ?>
<span class="minicart-tax-total">
<span class="weee" data-label="<?php echo $block->escapeHtml(__('Total Incl. Tax')); ?>">
<!-- custom logic -->
<?php /* @escapeNotVerified */ echo $block->formatPrice($block->getFinalUnitDisplayPriceInclTax()*$qty); ?>
</span>
</span>
<?php endif; ?>
<?php endif; ?>
<?php endif; ?>
</span>
<?php endif; ?>
<?php if ($block->displayPriceExclTax() || $block->displayBothPrices()): ?>
<span class="price-excluding-tax" data-label="<?php echo $block->escapeHtml(__('Excl. Tax')); ?>">
<?php if ($block->displayPriceWithWeeeDetails()): ?>
<span class="minicart-tax-total">
<?php else: ?>
<span class="minicart-price">
<?php endif; ?>
<!-- custom logic -->
<?php /* @escapeNotVerified */ echo $block->formatPrice($block->getUnitDisplayPriceExclTax()*$qty); ?>
</span>
<?php if ($block->displayPriceWithWeeeDetails()): ?>
<?php if ($this->helper('Magento\Weee\Helper\Data')->getApplied($item)): ?>
<span class="minicart-tax-info">
<?php foreach ($this->helper('Magento\Weee\Helper\Data')->getApplied($item) as $tax): ?>
<span class="weee" data-label="<?php /* @escapeNotVerified */ echo $tax['title']; ?>">
<?php /* @escapeNotVerified */ echo $block->formatPrice($tax['amount'], true, true); ?>
</span>
<?php endforeach; ?>
</span>
<?php if ($block->displayFinalPrice()): ?>
<span class="minicart-tax-total">
<span class="weee" data-label="<?php echo $block->escapeHtml(__('Total')); ?>">
<!-- custom logic -->
<?php /* @escapeNotVerified */ echo $block->formatPrice($block->getFinalUnitDisplayPriceExclTax()*$qty); ?>
</span>
</span>
<?php endif; ?>
<?php endif; ?>
<?php endif; ?>
</span>
<?php endif; ?>
<?php $block->setZone($originalZone); ?>
In above file i have just comment as custom logic at which place i have added price multiply $qty to get lineitem subtotal.
Clear cache and check it again.
Best Answer
As far as I know, the header minicart will get the data from customer data
vendor/magento/module-checkout/view/frontend/web/js/view/minicart.js
Look into the customer data js![enter image description here](https://i.stack.imgur.com/yG4uW.png)
vendor/magento/module-customer/view/frontend/web/js/customer-data.js
, we can get the customer data from local storage. For example, in your browser console, run the line:localStorage.getItem('mage-cache-storage')
, we also can get the cart information.Navigate to vendor/magento/module-checkout/CustomerData/DefaultItem.php
vendor/magento/module-checkout/CustomerData/AbstractItem.php
To get the SKU item, I think we need to add data to
getItemData()
(Should try with Plugin). And then override the template htmlvendor/magento/module-checkout/view/frontend/web/template/minicart/item/default.html
Update Magento 2.1.0 version
In the Magento 2.1.0, you only need to override
default.html
. This is because the methoddoGetItemData
has already the product sku.