Magento – collectTotals() on quote does not update configurable product item price

magento2quotequoteitemtiered-pricingtierprice

I have a problem with tier prices and configurable products in magento2. In our solution the customer puts everything in their cart and then in the checkout they qualify for a customer group and through that also for a tier price.

This works very well with simple products. After they have qualified for the price i can make a get request for the rest api and get the totals with these prices updated from the backend. However, configurable products does not seem to update.

The code sets the customer group for the checkout/customer session and then runs collecttotals:

$this->_customerSession->setCustomerGroupId($level->getCustomerGroup());
$this->_checkoutSession->setCustomerData($customerInterface);
$quote->collectTotals()->save(); 

customerInterface object is initalized and has the same group id as mentioned in the code.

I find it very weird that the code acts so differently on a configured product as opposed to a simple one.

Any help would be appreciated.

Best Answer

I have a little different type of issue with a configurable product. The total of configurable product counted double time.

So I dig into code in Magento 2.2 and found that in Magento 2.2 they change collect function.

vendor/magento/module-quote/Model/Quote/Address/Total/Grand.php

In Magento 2.1

public function collect(
    \Magento\Quote\Model\Quote $quote,
    \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
    \Magento\Quote\Model\Quote\Address\Total $total
) {
    $totals = array_sum($total->getAllTotalAmounts());
    $baseTotals = array_sum($total->getAllBaseTotalAmounts());

    $total->setGrandTotal($totals);
    $total->setBaseGrandTotal($baseTotals);
    return $this;
}

In Magento 2.2

public function collect(
    \Magento\Quote\Model\Quoet $quote,
    \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
    \Magento\Quote\Model\Quote\Address\Total $total
) {
    $grandTotal = $total->getGrandTotal();
    $baseGrandTotal = $total->getBaseGrandTotal();
    $totals = array_sum($total->getAllTotalAmounts());
    $baseTotals = array_sum($total->getAllBaseTotalAmounts());

    $total->setGrandTotal($grandTotal + $totals);
    $total->setBaseGrandTotal(b$aseGrandTotal + $baseTotals);
    return $this;
}

As this code they first get totals and add into setGrandTotal() and setBaseGrandTotal(). So it makes double time count in products totals.

Also, they change other logic about totals in Magento 2.2. So compare both vendor/module-quote module and found the issue.

May this will helpful for you.