|
5 | 5 | */
|
6 | 6 | namespace Magento\Bundle\Model\ResourceModel\Selection;
|
7 | 7 |
|
8 |
| -use Magento\Customer\Api\GroupManagementInterface; |
9 | 8 | use Magento\Framework\DataObject;
|
10 | 9 | use Magento\Framework\DB\Select;
|
11 |
| -use Magento\Framework\EntityManager\MetadataPool; |
12 | 10 | use Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitationFactory;
|
13 | 11 | use Magento\Framework\App\ObjectManager;
|
14 | 12 |
|
@@ -45,6 +43,95 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
|
45 | 43 | */
|
46 | 44 | private $websiteScopePriceJoined = false;
|
47 | 45 |
|
| 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 | + |
48 | 135 | /**
|
49 | 136 | * Initialize collection
|
50 | 137 | *
|
@@ -170,27 +257,31 @@ public function setPositionOrder()
|
170 | 257 | */
|
171 | 258 | public function addQuantityFilter()
|
172 | 259 | {
|
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 | + |
175 | 278 | $this->getSelect()
|
176 | 279 | ->joinInner(
|
177 |
| - ['stock' => $stockStatusTable], |
178 |
| - 'selection.product_id = stock.product_id', |
179 |
| - [] |
180 |
| - )->joinInner( |
181 |
| - ['stock_item' => $stockItemTable], |
| 280 | + ['stock_item' => $this->stockItem->getMainTable()], |
182 | 281 | 'selection.product_id = stock_item.product_id',
|
183 | 282 | []
|
184 |
| - ) |
185 |
| - ->where( |
186 |
| - '(' |
187 |
| - . 'selection.selection_can_change_qty' |
188 |
| - . ' or ' |
189 |
| - . 'selection.selection_qty <= stock.qty' |
190 |
| - . ' or ' |
191 |
| - .'stock_item.manage_stock = 0' |
192 |
| - . ') and stock.stock_status = 1' |
193 |
| - ); |
| 283 | + )->where($where); |
| 284 | + |
194 | 285 | return $this;
|
195 | 286 | }
|
196 | 287 |
|
|
0 commit comments