Skip to content

Commit 3a53ad2

Browse files
author
Igor Melnikov
committed
MAGETWO-58643: Refactor ObjectManager, Interception, Reflection Framework
Merging with remote branch, conflicts: lib/internal/Magento/Framework/Interception/PluginList/PluginList.php
2 parents 8553cbc + 3a8df23 commit 3a53ad2

File tree

246 files changed

+12223
-1404
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

246 files changed

+12223
-1404
lines changed

app/code/Magento/AdvancedPricingImportExport/Model/Import/AdvancedPricing.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -542,19 +542,24 @@ protected function retrieveOldSkus()
542542
*/
543543
protected function processCountExistingPrices($prices, $table)
544544
{
545+
$oldSkus = $this->retrieveOldSkus();
546+
$existProductIds = array_intersect_key($oldSkus, $prices);
547+
if (!count($existProductIds)) {
548+
return $this;
549+
}
550+
545551
$tableName = $this->_resourceFactory->create()->getTable($table);
546552
$productEntityLinkField = $this->getProductEntityLinkField();
547553
$existingPrices = $this->_connection->fetchAssoc(
548554
$this->_connection->select()->from(
549555
$tableName,
550556
['value_id', $productEntityLinkField, 'all_groups', 'customer_group_id']
551-
)
557+
)->where($productEntityLinkField . ' IN (?)', $existProductIds)
552558
);
553-
$oldSkus = $this->retrieveOldSkus();
554559
foreach ($existingPrices as $existingPrice) {
555-
foreach ($oldSkus as $sku => $productId) {
556-
if ($existingPrice[$productEntityLinkField] == $productId && isset($prices[$sku])) {
557-
$this->incrementCounterUpdated($prices[$sku], $existingPrice);
560+
foreach ($prices as $sku => $skuPrices) {
561+
if (isset($oldSkus[$sku]) && $existingPrice[$productEntityLinkField] == $oldSkus[$sku]) {
562+
$this->incrementCounterUpdated($skuPrices, $existingPrice);
558563
}
559564
}
560565
}

app/code/Magento/Bundle/Block/Catalog/Product/View/Type/Bundle.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ class Bundle extends \Magento\Catalog\Block\Product\View\AbstractView
4848
*/
4949
private $selectedOptions = [];
5050

51+
/**
52+
* @var \Magento\CatalogRule\Model\ResourceModel\Product\CollectionProcessor
53+
*/
54+
private $catalogRuleProcessor;
55+
5156
/**
5257
* @param \Magento\Catalog\Block\Product\Context $context
5358
* @param \Magento\Framework\Stdlib\ArrayUtils $arrayUtils
@@ -77,6 +82,20 @@ public function __construct(
7782
);
7883
}
7984

85+
/**
86+
* @deprecated
87+
* @return \Magento\CatalogRule\Model\ResourceModel\Product\CollectionProcessor
88+
*/
89+
private function getCatalogRuleProcessor()
90+
{
91+
if ($this->catalogRuleProcessor === null) {
92+
$this->catalogRuleProcessor = \Magento\Framework\App\ObjectManager::getInstance()
93+
->get(\Magento\CatalogRule\Model\ResourceModel\Product\CollectionProcessor::class);
94+
}
95+
96+
return $this->catalogRuleProcessor;
97+
}
98+
8099
/**
81100
* Returns the bundle product options
82101
* Will return cached options data if the product options are already initialized
@@ -89,6 +108,7 @@ public function getOptions($stripSelection = false)
89108
{
90109
if (!$this->options) {
91110
$product = $this->getProduct();
111+
/** @var \Magento\Bundle\Model\Product\Type $typeInstance */
92112
$typeInstance = $product->getTypeInstance();
93113
$typeInstance->setStoreFilter($product->getStoreId(), $product);
94114

@@ -98,6 +118,8 @@ public function getOptions($stripSelection = false)
98118
$typeInstance->getOptionsIds($product),
99119
$product
100120
);
121+
$this->getCatalogRuleProcessor()->addPriceData($selectionCollection);
122+
$selectionCollection->addTierPriceData();
101123

102124
$this->options = $optionCollection->appendSelections(
103125
$selectionCollection,

app/code/Magento/Bundle/Model/Product/Type.php

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Magento\Bundle\Model\Product;
1010

1111
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Framework\App\ObjectManager;
1213
use Magento\Framework\Pricing\PriceCurrencyInterface;
1314

1415
/**
@@ -42,6 +43,7 @@ class Type extends \Magento\Catalog\Model\Product\Type\AbstractType
4243
* Cache key for Selections Collection
4344
*
4445
* @var string
46+
* @deprecated
4547
*/
4648
protected $_keySelectionsCollection = '_cache_instance_selections_collection';
4749

@@ -449,30 +451,24 @@ public function getOptionsCollection($product)
449451
*/
450452
public function getSelectionsCollection($optionIds, $product)
451453
{
452-
$keyOptionIds = is_array($optionIds) ? implode('_', $optionIds) : '';
453-
$key = $this->_keySelectionsCollection . $keyOptionIds;
454-
if (!$product->hasData($key)) {
455-
$storeId = $product->getStoreId();
456-
$selectionsCollection = $this->_bundleCollection->create()
457-
->addAttributeToSelect($this->_config->getProductAttributes())
458-
->addAttributeToSelect('tax_class_id')//used for calculation item taxes in Bundle with Dynamic Price
459-
->setFlag('product_children', true)
460-
->setPositionOrder()
461-
->addStoreFilter($this->getStoreFilter($product))
462-
->setStoreId($storeId)
463-
->addFilterByRequiredOptions()
464-
->setOptionIdsFilter($optionIds);
465-
466-
if (!$this->_catalogData->isPriceGlobal() && $storeId) {
467-
$websiteId = $this->_storeManager->getStore($storeId)
468-
->getWebsiteId();
469-
$selectionsCollection->joinPrices($websiteId);
470-
}
471-
472-
$product->setData($key, $selectionsCollection);
454+
$storeId = $product->getStoreId();
455+
$selectionsCollection = $this->_bundleCollection->create()
456+
->addAttributeToSelect($this->_config->getProductAttributes())
457+
->addAttributeToSelect('tax_class_id') //used for calculation item taxes in Bundle with Dynamic Price
458+
->setFlag('product_children', true)
459+
->setPositionOrder()
460+
->addStoreFilter($this->getStoreFilter($product))
461+
->setStoreId($storeId)
462+
->addFilterByRequiredOptions()
463+
->setOptionIdsFilter($optionIds);
464+
465+
if (!$this->_catalogData->isPriceGlobal() && $storeId) {
466+
$websiteId = $this->_storeManager->getStore($storeId)
467+
->getWebsiteId();
468+
$selectionsCollection->joinPrices($websiteId);
473469
}
474470

475-
return $product->getData($key);
471+
return $selectionsCollection;
476472
}
477473

478474
/**
@@ -543,42 +539,33 @@ public function isSalable($product)
543539
return $product->getData('all_items_salable');
544540
}
545541

546-
$optionCollection = $this->getOptionsCollection($product);
547-
548-
if (!count($optionCollection->getItems())) {
549-
return false;
550-
}
542+
$isSalable = false;
543+
foreach ($this->getOptionsCollection($product)->getItems() as $option) {
544+
$hasSalable = false;
551545

552-
$requiredOptionIds = [];
546+
$selectionsCollection = $this->_bundleCollection->create();
547+
$selectionsCollection->addAttributeToSelect('status');
548+
$selectionsCollection->addQuantityFilter();
549+
$selectionsCollection->addFilterByRequiredOptions();
550+
$selectionsCollection->setOptionIdsFilter([$option->getId()]);
553551

554-
foreach ($optionCollection->getItems() as $option) {
555-
if ($option->getRequired()) {
556-
$requiredOptionIds[$option->getId()] = 0;
552+
foreach ($selectionsCollection as $selection) {
553+
if ($selection->isSalable()) {
554+
$hasSalable = true;
555+
break;
556+
}
557557
}
558-
}
559558

560-
$selectionCollection = $this->getSelectionsCollection($optionCollection->getAllIds(), $product);
559+
if ($hasSalable) {
560+
$isSalable = true;
561+
}
561562

562-
if (!count($selectionCollection->getItems())) {
563-
return false;
564-
}
565-
$salableSelectionCount = 0;
566-
567-
foreach ($selectionCollection as $selection) {
568-
/* @var $selection \Magento\Catalog\Model\Product */
569-
if ($selection->isSalable()) {
570-
$selectionEnoughQty = $this->_stockRegistry->getStockItem($selection->getId())
571-
->getManageStock()
572-
? $selection->getSelectionQty() <= $this->_stockState->getStockQty($selection->getId())
573-
: $selection->isInStock();
574-
575-
if (!$selection->hasSelectionQty() || $selection->getSelectionCanChangeQty() || $selectionEnoughQty) {
576-
$requiredOptionIds[$selection->getOptionId()] = 1;
577-
$salableSelectionCount++;
578-
}
563+
if (!$hasSalable && $option->getRequired()) {
564+
$isSalable = false;
565+
break;
579566
}
580567
}
581-
$isSalable = array_sum($requiredOptionIds) == count($requiredOptionIds) && $salableSelectionCount;
568+
582569
$product->setData('all_items_salable', $isSalable);
583570

584571
return $isSalable;

app/code/Magento/Bundle/Model/ResourceModel/Selection/Collection.php

Lines changed: 124 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
*/
66
namespace Magento\Bundle\Model\ResourceModel\Selection;
77

8+
use Magento\Customer\Api\GroupManagementInterface;
9+
use Magento\Framework\DataObject;
10+
use Magento\Framework\DB\Select;
11+
812
/**
913
* Bundle Selections Resource Collection
10-
*
11-
* @author Magento Core Team <core@magentocommerce.com>
1214
*/
1315
class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
1416
{
@@ -19,6 +21,23 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
1921
*/
2022
protected $_selectionTable;
2123

24+
/**
25+
* @var DataObject
26+
*/
27+
private $itemPrototype = null;
28+
29+
/**
30+
* @var \Magento\CatalogRule\Model\ResourceModel\Product\CollectionProcessor
31+
*/
32+
private $catalogRuleProcessor = null;
33+
34+
/**
35+
* Is website scope prices joined to collection
36+
*
37+
* @var bool
38+
*/
39+
private $websiteScopePriceJoined = false;
40+
2241
/**
2342
* Initialize collection
2443
*
@@ -90,6 +109,8 @@ public function joinPrices($websiteId)
90109
'price_scope' => 'price.website_id'
91110
]
92111
);
112+
$this->websiteScopePriceJoined = true;
113+
93114
return $this;
94115
}
95116

@@ -131,4 +152,105 @@ public function setPositionOrder()
131152
$this->getSelect()->order('selection.position asc')->order('selection.selection_id asc');
132153
return $this;
133154
}
155+
156+
/**
157+
* Add filtering of product then havent enoght stock
158+
*
159+
* @return $this
160+
*/
161+
public function addQuantityFilter()
162+
{
163+
$this->getSelect()
164+
->joinInner(
165+
['stock' => $this->getTable('cataloginventory_stock_status')],
166+
'selection.product_id = stock.product_id',
167+
[]
168+
)
169+
->where(
170+
'(selection.selection_can_change_qty or selection.selection_qty <= stock.qty) and stock.stock_status'
171+
);
172+
return $this;
173+
}
174+
175+
/**
176+
* @inheritDoc
177+
*/
178+
public function getNewEmptyItem()
179+
{
180+
if (null === $this->itemPrototype) {
181+
$this->itemPrototype = parent::getNewEmptyItem();
182+
}
183+
return clone $this->itemPrototype;
184+
}
185+
186+
/**
187+
* Add filter by price
188+
*
189+
* @param \Magento\Catalog\Model\Product $product
190+
* @param bool $searchMin
191+
* @param bool $useRegularPrice
192+
*
193+
* @return $this
194+
*/
195+
public function addPriceFilter($product, $searchMin, $useRegularPrice = false)
196+
{
197+
if ($product->getPriceType() == \Magento\Bundle\Model\Product\Price::PRICE_TYPE_DYNAMIC) {
198+
$this->addPriceData();
199+
if ($useRegularPrice) {
200+
$minimalPriceExpression = 'price';
201+
} else {
202+
$this->getCatalogRuleProcessor()->addPriceData($this, 'selection.product_id');
203+
$minimalPriceExpression = 'LEAST(minimal_price, IFNULL(catalog_rule_price, minimal_price))';
204+
}
205+
$orderByValue = new \Zend_Db_Expr(
206+
'(' .
207+
$minimalPriceExpression .
208+
' * selection.selection_qty' .
209+
')'
210+
);
211+
} else {
212+
$connection = $this->getConnection();
213+
$priceType = $connection->getIfNullSql(
214+
'price.selection_price_type',
215+
'selection.selection_price_type'
216+
);
217+
$priceValue = $connection->getIfNullSql(
218+
'price.selection_price_value',
219+
'selection.selection_price_value'
220+
);
221+
if (!$this->websiteScopePriceJoined) {
222+
$websiteId = $this->_storeManager->getStore()->getWebsiteId();
223+
$this->getSelect()->joinLeft(
224+
['price' => $this->getTable('catalog_product_bundle_selection_price')],
225+
'selection.selection_id = price.selection_id AND price.website_id = ' . (int)$websiteId,
226+
[]
227+
);
228+
}
229+
$price = $connection->getCheckSql(
230+
$priceType . ' = 1',
231+
(float) $product->getPrice() . ' * '. $priceValue . ' / 100',
232+
$priceValue
233+
);
234+
$orderByValue = new \Zend_Db_Expr('('. $price. ' * '. 'selection.selection_qty)');
235+
}
236+
237+
$this->getSelect()->reset(Select::ORDER);
238+
$this->getSelect()->order($orderByValue . ($searchMin ? Select::SQL_ASC : Select::SQL_DESC));
239+
$this->getSelect()->limit(1);
240+
return $this;
241+
}
242+
243+
/**
244+
* @return \Magento\CatalogRule\Model\ResourceModel\Product\CollectionProcessor
245+
* @deprecated
246+
*/
247+
private function getCatalogRuleProcessor()
248+
{
249+
if (null === $this->catalogRuleProcessor) {
250+
$this->catalogRuleProcessor = \Magento\Framework\App\ObjectManager::getInstance()
251+
->get(\Magento\CatalogRule\Model\ResourceModel\Product\CollectionProcessor::class);
252+
}
253+
254+
return $this->catalogRuleProcessor;
255+
}
134256
}

0 commit comments

Comments
 (0)