3

In the Magento 2 mini cart I would like to display the line item subtotal rather than unit price.

So in the example below the price in the minicart should be $90 because I've added two watches to the cart.

enter image description here

I see this Magento displays this price from a KnockoutJS template

app/code/Magento/Checkout/view/frontend/web/template/minicart/item/default.html

with this piece of code:

<!-- ko foreach: $parent.getRegion('priceSidebar') -->
    <!-- ko template: {name: getTemplate(), data: item.product_price, as: 'price'} --><!-- /ko -->
<!-- /ko -->

The item object contains item data like below:

canApplyMsrp:false
is_visible_in_site_visibility:true
item_id:"155"
product_has_url:true
product_id:"2"
product_image:Object
product_name:"Test Product"
product_price:"↵↵    <span class="price-excluding-tax" data-label="Excl. Tax">↵            <span class="minicart-price">↵            <span class="price">$45.00</span>        </span>↵↵        </span>↵"
product_price_value:30
product_sku:"01002"
product_type:"simple"
product_url:"http://dev.example.com/test-product.html"
qty:2

In my theme, how can I override the current price value used, product_price, to display the line item sub-total price instead? Properly formatted with the correct currency symbol.

So far I've tried the below:

<!-- ko template: {name: getTemplate(), data: item.product_price * item.qty, as: 'price'} --><!-- /ko -->  // returns a NaN

<!-- ko template: {name: getTemplate(), data: item.product_price_value * item.qty, as: 'price'} --><!-- /ko -->  // returns the correct price but without the currency symbol
6
+50

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.

  • Thanks, I tried overriding that file as you suggested but it did not work. I believe because KO is being used and not that file. Is there a step beforehand which I'm missing. Is this the correct file to override app/code/Magento/Weee/view/frontend/templates/checkout/cart/item/price/sidebar.phtml, I have flushed the Magento cache and static file cache – Holly May 10 '17 at 8:31
  • Also, if I followed this approach and did not use KO the mini-cart price would not update automatically with KO ajax when the user added more items to their cart – Holly May 10 '17 at 8:31
  • I have just given demo for theme override. – Rakesh Jesadiya May 10 '17 at 8:34
  • oh ok, it didn't work when I tested it – Holly May 10 '17 at 8:35
  • Have you keep at this location, app/design/frontend/{Packagename}/{themename}/Magento_Weee/templates/checkout/cart/item/price/sidebar.phtml? – Rakesh Jesadiya May 10 '17 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.