Skip to content

Commit 86dc050

Browse files
authored
Merge pull request #7090 from magento-performance/MCP-648
Sales Rule opitimization
2 parents 5788e74 + f77da0c commit 86dc050

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Model\Quote;
9+
10+
use Magento\Quote\Model\Quote;
11+
12+
class QuantityCollector
13+
{
14+
/**
15+
* Collect items qty
16+
*
17+
* @param Quote $quote
18+
* @return Quote
19+
*/
20+
public function collectItemsQtys(Quote $quote)
21+
{
22+
$quoteItems = $quote->getAllVisibleItems();
23+
$quote->setItemsCount(0);
24+
$quote->setItemsQty(0);
25+
$quote->setVirtualItemsQty(0);
26+
27+
foreach ($quoteItems as $item) {
28+
if ($item->getParentItem()) {
29+
continue;
30+
}
31+
32+
$children = $item->getChildren();
33+
if ($children && $item->isShipSeparately()) {
34+
foreach ($children as $child) {
35+
if ($child->getProduct()->getIsVirtual()) {
36+
$quote->setVirtualItemsQty($quote->getVirtualItemsQty() + $child->getQty() * $item->getQty());
37+
}
38+
}
39+
}
40+
41+
if ($item->getProduct()->getIsVirtual()) {
42+
$quote->setVirtualItemsQty($quote->getVirtualItemsQty() + $item->getQty());
43+
}
44+
$quote->setItemsCount($quote->getItemsCount() + 1);
45+
$quote->setItemsQty((float)$quote->getItemsQty() + $item->getQty());
46+
}
47+
48+
return $quote;
49+
}
50+
}

app/code/Magento/Quote/Model/Quote/TotalsCollector.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77

88
namespace Magento\Quote\Model\Quote;
99

10+
use Magento\Framework\App\ObjectManager;
1011
use Magento\Quote\Model\Quote\Address\Total\Collector;
1112
use Magento\Quote\Model\Quote\Address\Total\CollectorFactory;
1213
use Magento\Quote\Model\Quote\Address\Total\CollectorInterface;
1314

1415
/**
15-
* Class TotalsCollector
16+
* Composite object for collecting total.
1617
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1718
*/
1819
class TotalsCollector
@@ -68,6 +69,11 @@ class TotalsCollector
6869
*/
6970
protected $shippingAssignmentFactory;
7071

72+
/**
73+
* @var QuantityCollector
74+
*/
75+
private $quantityCollector;
76+
7177
/**
7278
* @param Collector $totalCollector
7379
* @param CollectorFactory $totalCollectorFactory
@@ -78,6 +84,7 @@ class TotalsCollector
7884
* @param \Magento\Quote\Model\ShippingFactory $shippingFactory
7985
* @param \Magento\Quote\Model\ShippingAssignmentFactory $shippingAssignmentFactory
8086
* @param \Magento\Quote\Model\QuoteValidator $quoteValidator
87+
* @param QuantityCollector $quantityCollector
8188
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8289
*/
8390
public function __construct(
@@ -89,7 +96,8 @@ public function __construct(
8996
\Magento\Quote\Model\Quote\TotalsCollectorList $collectorList,
9097
\Magento\Quote\Model\ShippingFactory $shippingFactory,
9198
\Magento\Quote\Model\ShippingAssignmentFactory $shippingAssignmentFactory,
92-
\Magento\Quote\Model\QuoteValidator $quoteValidator
99+
\Magento\Quote\Model\QuoteValidator $quoteValidator,
100+
QuantityCollector $quantityCollector = null
93101
) {
94102
$this->totalCollector = $totalCollector;
95103
$this->totalCollectorFactory = $totalCollectorFactory;
@@ -100,6 +108,8 @@ public function __construct(
100108
$this->shippingFactory = $shippingFactory;
101109
$this->shippingAssignmentFactory = $shippingAssignmentFactory;
102110
$this->quoteValidator = $quoteValidator;
111+
$this->quantityCollector = $quantityCollector
112+
?: ObjectManager::getInstance()->get(QuantityCollector::class);
103113
}
104114

105115
/**
@@ -132,7 +142,7 @@ public function collect(\Magento\Quote\Model\Quote $quote)
132142
['quote' => $quote]
133143
);
134144

135-
$this->_collectItemsQtys($quote);
145+
$this->quantityCollector->collectItemsQtys($quote);
136146

137147
$total->setSubtotal(0);
138148
$total->setBaseSubtotal(0);
@@ -206,6 +216,8 @@ protected function _validateCouponCode(\Magento\Quote\Model\Quote $quote)
206216
*
207217
* @param \Magento\Quote\Model\Quote $quote
208218
* @return $this
219+
* @deprecated
220+
* @see \Magento\Quote\Model\Quote\QuantityCollector
209221
*/
210222
protected function _collectItemsQtys(\Magento\Quote\Model\Quote $quote)
211223
{
@@ -273,7 +285,7 @@ public function collectAddressTotals(
273285
/** @var CollectorInterface $collector */
274286
$collector->collect($quote, $shippingAssignment, $total);
275287
}
276-
288+
277289
$this->eventManager->dispatch(
278290
'sales_quote_address_collect_totals_after',
279291
[

0 commit comments

Comments
 (0)