3

How can I retrieve the cart subtotal without tax in Magento 2?

  • In Mini cart and shopping cart page? – Khoa TruongDinh Nov 5 '16 at 0:45
  • I'm trying to get it from my custom shipping method. I need to enable or disable my shipping method based on cart subtotal without tax. – nuwaus Nov 5 '16 at 14:42
  • How about your issue? – Khoa TruongDinh Nov 12 '16 at 5:34
3

Try to use \Magento\Quote\Model\Quote\Address\Total to get the total.

/** @var \Magento\Quote\Model\Quote\Address\Total $total **/

$total->getSubtotal(); // Subtotal excluded tax
$total->getSubtotalInclTax() // Subtotal included tax.
  • $total->getSubtotalInclTax() is working, but its including tax, but $total->getSubtotal() did not work for me. I have tried. :( – nuwaus Nov 5 '16 at 15:15
  • $total->getSubtotal() returns value? – Khoa TruongDinh Nov 5 '16 at 15:16
  • No, it does not return anything, not even error or null it just blank. – nuwaus Nov 5 '16 at 15:18
  • You should add full code lines. – Khoa TruongDinh Nov 5 '16 at 15:19
  • Thanks, I will try one more time and, let you know the feedback. – nuwaus Nov 5 '16 at 15:20
0

Add this plugin in your code and after that you will get $total->getSubtotal(),$total->getSubtotalInclTax() and $total->getSubtotalExclTax()

<?php

namespace Vendor\Module\Plugin\Model\Sales\Total\Quote;


class TaxPlugin
{
    /**
     * Tax configuration object
     *
     * @var \Magento\Tax\Model\Config
     */
    protected $_config;

    /**
     * TaxPlugin constructor.
     * @param \Magento\Tax\Model\Config $taxConfig
     */
    public function __construct(
        \Magento\Tax\Model\Config $taxConfig
    )
    {
        $this->_config = $taxConfig;
    }

    /**
     * add subtotal inclusive of tax
     *
     * @param \Magento\Tax\Model\Sales\Total\Quote\Tax $subject
     * @param $result
     * @param $quote
     * @param $total
     * @return array
     */
    public function afterFetch(\Magento\Tax\Model\Sales\Total\Quote\Tax $subject, $result, $quote, $total)
    {
        $store = $quote->getStore();
        $amount = $total->getTaxAmount();
        if ($amount === null) {
            $this->enhanceTotalData($quote, $total);
            $amount = $total->getTaxAmount();
        }
        $taxAmount = $amount + $total->getTotalAmount('discount_tax_compensation');

        if (!($this->_config->displayCartSubtotalBoth($store) || $this->_config->displayCartSubtotalInclTax($store))) {
            if ($total->getSubtotalInclTax() > 0) {
                $subtotalInclTax = $total->getSubtotalInclTax();
            } else {
                $subtotalInclTax = $total->getSubtotal() + $taxAmount - $total->getShippingTaxAmount();
            }

            $result[] = [
                'code' => 'subtotal',
                'title' => __('Subtotal'),
                'value' => $total->getSubtotal(),
                'value_incl_tax' => $subtotalInclTax,
                'value_excl_tax' => $total->getSubtotal(),
            ];
        }

        return $result;
    }
}

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.