Skip to content

Commit 7f61ebd

Browse files
committed
Merge remote-tracking branch 'performance/MCP-549' into MCP-648
2 parents eea292a + 468a025 commit 7f61ebd

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
* @var []
16+
*/
17+
private $quoteCache = [];
18+
19+
/**
20+
* Collect items qty
21+
*
22+
* @param Quote $quote
23+
* @return Quote
24+
*/
25+
public function collectItemsQtys(Quote $quote)
26+
{
27+
if (!isset($this->quoteCache[$quote->getEntityId()])) {
28+
$quoteItems = $quote->getAllVisibleItems();
29+
$quote->setItemsCount(0);
30+
$quote->setItemsQty(0);
31+
$quote->setVirtualItemsQty(0);
32+
33+
foreach ($quoteItems as $item) {
34+
if ($item->getParentItem()) {
35+
continue;
36+
}
37+
38+
$children = $item->getChildren();
39+
if ($children && $item->isShipSeparately()) {
40+
foreach ($children as $child) {
41+
if ($child->getProduct()->getIsVirtual()) {
42+
$quote->setVirtualItemsQty(
43+
$quote->getVirtualItemsQty() + $child->getQty() * $item->getQty()
44+
);
45+
}
46+
}
47+
}
48+
49+
if ($item->getProduct()->getIsVirtual()) {
50+
$quote->setVirtualItemsQty($quote->getVirtualItemsQty() + $item->getQty());
51+
}
52+
$quote->setItemsCount($quote->getItemsCount() + 1);
53+
$quote->setItemsQty((float)$quote->getItemsQty() + $item->getQty());
54+
}
55+
56+
$this->quoteCache[$quote->getEntityId()] = $quote;
57+
58+
return $quote;
59+
}
60+
61+
return $this->quoteCache[$quote->getEntityId()];
62+
}
63+
}

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
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;
@@ -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
{

0 commit comments

Comments
 (0)