Skip to content

Commit 93d7761

Browse files
committed
MAGETWO-91558: Enable Add to Cart on bundle products when bundle item qty is not User Defined while backorders are allowed
1 parent f1154d4 commit 93d7761

File tree

3 files changed

+231
-176
lines changed

3 files changed

+231
-176
lines changed

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

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

8-
use Magento\Customer\Api\GroupManagementInterface;
98
use Magento\Framework\DataObject;
109
use Magento\Framework\DB\Select;
11-
use Magento\Framework\EntityManager\MetadataPool;
1210
use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory;
1311
use Magento\Framework\App\ObjectManager;
1412

@@ -45,6 +43,95 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
4543
*/
4644
private $websiteScopePriceJoined = false;
4745

46+
/**
47+
* @var \Magento\CatalogInventory\Model\ResourceModel\Stock\Item
48+
*/
49+
private $stockItem;
50+
51+
/**
52+
* Collection constructor.
53+
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
54+
* @param \Psr\Log\LoggerInterface $logger
55+
* @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
56+
* @param \Magento\Framework\Event\ManagerInterface $eventManager
57+
* @param \Magento\Eav\Model\Config $eavConfig
58+
* @param \Magento\Framework\App\ResourceConnection $resource
59+
* @param \Magento\Eav\Model\EntityFactory $eavEntityFactory
60+
* @param \Magento\Catalog\Model\ResourceModel\Helper $resourceHelper
61+
* @param \Magento\Framework\Validator\UniversalFactory $universalFactory
62+
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
63+
* @param \Magento\Framework\Module\Manager $moduleManager
64+
* @param \Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState
65+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
66+
* @param \Magento\Catalog\Model\Product\OptionFactory $productOptionFactory
67+
* @param \Magento\Catalog\Model\ResourceModel\Url $catalogUrl
68+
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
69+
* @param \Magento\Customer\Model\Session $customerSession
70+
* @param \Magento\Framework\Stdlib\DateTime $dateTime
71+
* @param \Magento\Customer\Api\GroupManagementInterface $groupManagement
72+
* @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection
73+
* @param ProductLimitationFactory|null $productLimitationFactory
74+
* @param \Magento\Framework\EntityManager\MetadataPool|null $metadataPool
75+
* @param \Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer|null $tableMaintainer
76+
* @param \Magento\CatalogInventory\Model\ResourceModel\Stock\Item|null $stockItem
77+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
78+
*/
79+
public function __construct(
80+
\Magento\Framework\Data\Collection\EntityFactory $entityFactory,
81+
\Psr\Log\LoggerInterface $logger,
82+
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
83+
\Magento\Framework\Event\ManagerInterface $eventManager,
84+
\Magento\Eav\Model\Config $eavConfig,
85+
\Magento\Framework\App\ResourceConnection $resource,
86+
\Magento\Eav\Model\EntityFactory $eavEntityFactory,
87+
\Magento\Catalog\Model\ResourceModel\Helper $resourceHelper,
88+
\Magento\Framework\Validator\UniversalFactory $universalFactory,
89+
\Magento\Store\Model\StoreManagerInterface $storeManager,
90+
\Magento\Framework\Module\Manager $moduleManager,
91+
\Magento\Catalog\Model\Indexer\Product\Flat\State $catalogProductFlatState,
92+
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
93+
\Magento\Catalog\Model\Product\OptionFactory $productOptionFactory,
94+
\Magento\Catalog\Model\ResourceModel\Url $catalogUrl,
95+
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate,
96+
\Magento\Customer\Model\Session $customerSession,
97+
\Magento\Framework\Stdlib\DateTime $dateTime,
98+
\Magento\Customer\Api\GroupManagementInterface $groupManagement,
99+
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
100+
ProductLimitationFactory $productLimitationFactory = null,
101+
\Magento\Framework\EntityManager\MetadataPool $metadataPool = null,
102+
\Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer $tableMaintainer = null,
103+
\Magento\CatalogInventory\Model\ResourceModel\Stock\Item $stockItem = null
104+
) {
105+
parent::__construct(
106+
$entityFactory,
107+
$logger,
108+
$fetchStrategy,
109+
$eventManager,
110+
$eavConfig,
111+
$resource,
112+
$eavEntityFactory,
113+
$resourceHelper,
114+
$universalFactory,
115+
$storeManager,
116+
$moduleManager,
117+
$catalogProductFlatState,
118+
$scopeConfig,
119+
$productOptionFactory,
120+
$catalogUrl,
121+
$localeDate,
122+
$customerSession,
123+
$dateTime,
124+
$groupManagement,
125+
$connection,
126+
$productLimitationFactory,
127+
$metadataPool,
128+
$tableMaintainer
129+
);
130+
131+
$this->stockItem = $stockItem
132+
?? ObjectManager::getInstance()->get(\Magento\CatalogInventory\Model\ResourceModel\Stock\Item::class);
133+
}
134+
48135
/**
49136
* Initialize collection
50137
*
@@ -170,28 +257,30 @@ public function setPositionOrder()
170257
*/
171258
public function addQuantityFilter()
172259
{
173-
$stockItemTable = $this->getTable('cataloginventory_stock_item');
174-
$stockStatusTable = $this->getTable('cataloginventory_stock_status');
260+
$manageStockExpr = $this->stockItem->getManageStockExpr('stock_item');
261+
$backordersExpr = $this->stockItem->getBackordersExpr('stock_item');
262+
$minQtyExpr = $this->getConnection()->getCheckSql(
263+
'selection.selection_can_change_qty',
264+
$this->stockItem->getMinSaleQtyExpr('stock_item'),
265+
'selection.selection_qty'
266+
);
267+
268+
$where = $manageStockExpr . ' = 0';
269+
$where .= ' OR ('
270+
. 'stock_item.is_in_stock = ' . \Magento\CatalogInventory\Model\Stock::STOCK_IN_STOCK
271+
. ' AND ('
272+
. $backordersExpr . ' != ' . \Magento\CatalogInventory\Model\Stock::BACKORDERS_NO
273+
. ' OR '
274+
. $minQtyExpr . ' <= stock_item.qty'
275+
. ')'
276+
. ')';
277+
175278
$this->getSelect()
176279
->joinInner(
177-
['stock' => $stockStatusTable],
178-
'selection.product_id = stock.product_id',
179-
[]
180-
)->joinInner(
181-
['stock_item' => $stockItemTable],
280+
['stock_item' => $this->stockItem->getMainTable()],
182281
'selection.product_id = stock_item.product_id',
183282
[]
184-
)
185-
->where(
186-
'('
187-
. 'selection.selection_can_change_qty > 0'
188-
. ' or '
189-
. 'selection.selection_qty <= stock.qty'
190-
. ' or '
191-
.'stock_item.manage_stock = 0'
192-
. ')'
193-
)
194-
->where('stock.stock_status = 1');
283+
)->where($where);
195284

196285
return $this;
197286
}

app/code/Magento/Bundle/Test/Unit/Model/ResourceModel/Selection/CollectionTest.php

Lines changed: 0 additions & 156 deletions
This file was deleted.

0 commit comments

Comments
 (0)