如何在minicart magento 2中显示总计而不是小计

时间:2019-11-21 13:30:35

标签: magento magento2

我想在mini cart中显示总计,而不是magento 2 porto主题中的小计。

1 个答案:

答案 0 :(得分:0)

方法:1

您需要覆盖以下文件

  • Magento_Checkout / templates / cart / minicart.phtml
  • Magento / Checkout / CustomerData / Cart.php

1)minicart.phtml

    <!-- ko if: getCartParam('summary_count') -->
    <span class="total-price grand-total" data-bind="html: getCartParam('grand_total')"></span>
    <!-- /ko -->

2)Cart.php

覆盖getSectionData()函数

public function getSectionData()
{
    $totals = $this->getQuote()->getTotals();
    $grand_total = $totals['grand_total']->getValue();

    return [
        'grand_total' => $grand_total ? $this->checkoutHelper->formatPrice($grand_total) : '',
    ];
}

方法:2

您需要覆盖以下文件

  • Magento_Checkout / templates / cart / minicart.phtml
  • di.xml中添加插件

1)minicart.phtml

    <!-- ko if: getCartParam('summary_count') -->
    <span class="total-price grand-total" data-bind="html: getCartParam('grand_total')"></span>
    <!-- /ko -->

2)将插件添加到di.xml并在提到的位置创建插件文件

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\Checkout\CustomerData\Cart">
        <plugin name="cart_totals" type="Vendor\Module\Plugin\Checkout\CustomerData\CartTotals"/>
    </type>

</config>

在上述位置创建CartTotals.php文件。

<?php
namespace Vendor\Module\Plugin\Checkout\CustomerData;
class CartTotals
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $checkoutSession;
    /**
     * @param \Magento\Checkout\Model\Session $checkoutSession
     */
    public function __construct(\Magento\Checkout\Model\Session $checkoutSession) {
        $this->checkoutSession = $checkoutSession;
    }
    /**
     * Add cart grand total to result data
     *
     * @param \Magento\Checkout\CustomerData\Cart $subject
     * @param array $result
     * @return array
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function afterGetSectionData(\Magento\Checkout\CustomerData\Cart $subject, $result)
    {
        $totals = $this->checkoutSession->getQuote()->getTotals();
        if(isset($totals['grand_total'])) {
            $result['grand_total'] = $totals['grand_total']->getValueInclTax() ?: $totals['grand_total']->getValue();
        }
        return $result;
    }
}