Skip to content

Commit 0c1aba8

Browse files
committed
Moved the Totals Collector earlier in the stack, just after the discounts have been applied.
1 parent 2dd81b2 commit 0c1aba8

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

Model/Collector/AutoCustomerGroup.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,23 @@
1313
use Gw\AutoCustomerGroup\Model\AutoCustomerGroup as AutoCustomerGroupModel;
1414

1515
/**
16-
* AutoCustomerGroup totals collector, configured from sales.xml which runs at the end of the stack
17-
* of collectors. If the group has changed, then the tax collectors must be re-run as the tax to be
18-
* applied will now be different. The collectors to be rerun can be configured in di.xml
16+
* Magento Vanilla Collectors sequence
17+
*
18+
* 100 Magento\Quote\Model\Quote\Address\Total\Subtotal
19+
* 200 Magento\Tax\Model\Sales\Total\Quote\Subtotal
20+
* 225 Magento\Weee\Model\Total\Quote\Weee
21+
* 300 Magento\SalesRule\Model\Quote\Discount
22+
* 325 <-- This is a good place for us to be. Product discounts already applied
23+
* 350 Magento\Quote\Model\Quote\Address\Total\Shipping
24+
* 375 Magento\Tax\Model\Sales\Total\Quote\Shipping
25+
* 400 Magento\SalesRule\Model\Quote\Address\Total\ShippingDiscount
26+
* 450 Magento\Tax\Model\Sales\Total\Quote\Tax
27+
* 460 Magento\Weee\Model\Total\Quote\WeeeTax
28+
* 550 Magento\Quote\Model\Quote\Address\Total\Grand
29+
* AutoCustomerGroup totals collector, configured from sales.xml
30+
* What happens if we switch groups to a group where discount no longer applies? Should we re-run
31+
* the discount collectors, what if that then changes the order total. We need to re-evaluate groups again.
32+
* For now, lets just re-run all collectors that came before us, and carry on
1933
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
2034
*/
2135
class AutoCustomerGroup extends AbstractTotal
@@ -227,9 +241,10 @@ private function updateGroup($newGroup, Quote $quote, $customer, $shippingAssign
227241
);
228242
$quote->setCustomerGroupId($newGroup);
229243

230-
//The group has changed. We need to rerun the Tax collectors.
231-
foreach ($this->additionalCollectors as $collector) {
244+
//The group has changed. Which collectors should we re-run
245+
foreach ($this->additionalCollectors as $code => $collector) {
232246
if ($collector) {
247+
$collector->setCode($code);
233248
$collector->collect($quote, $shippingAssignment, $total);
234249
}
235250
}

etc/di.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
<type name="Gw\AutoCustomerGroup\Model\Collector\AutoCustomerGroup">
1212
<arguments>
1313
<argument name="additionalCollectors" xsi:type="array">
14-
<item name="redo_tax_subtotal" xsi:type="object">Magento\Tax\Model\Sales\Total\Quote\Subtotal</item>
15-
<item name="redo_tax_shipping" xsi:type="object">Magento\Tax\Model\Sales\Total\Quote\Shipping</item>
16-
<item name="redo_tax" xsi:type="object">Magento\Tax\Model\Sales\Total\Quote\Tax</item>
17-
<item name="redo_tax_weee_tax" xsi:type="object">Magento\Weee\Model\Total\Quote\WeeeTax</item>
14+
<item name="subtotal" xsi:type="object">Magento\Quote\Model\Quote\Address\Total\Subtotal</item>
15+
<item name="tax_subtotal" xsi:type="object">Magento\Tax\Model\Sales\Total\Quote\Subtotal</item>
16+
<item name="weee" xsi:type="object">Magento\Weee\Model\Total\Quote\Weee</item>
17+
<item name="discount" xsi:type="object">Magento\SalesRule\Model\Quote\Discount</item>
1818
</argument>
1919
</arguments>
2020
</type>

etc/sales.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/sales.xsd">
33
<section name="quote">
44
<group name="totals">
5-
<item name="autocustomergroup" instance="Gw\AutoCustomerGroup\Model\Collector\AutoCustomerGroup" sort_order="1000"/>
5+
<item name="autocustomergroup" instance="Gw\AutoCustomerGroup\Model\Collector\AutoCustomerGroup" sort_order="325"/>
66
</group>
77
</section>
88
</config>

0 commit comments

Comments
 (0)