Skip to content

Commit aec044d

Browse files
authored
Merge pull request #4024 from magento-tango/MAGETWO-98831
[tango] MAGETWO-98831: The SKU was not found in the catalog
2 parents ab188bc + 5008e85 commit aec044d

File tree

2 files changed

+77
-10
lines changed

2 files changed

+77
-10
lines changed

app/code/Magento/Sales/Block/Adminhtml/Order/Create/Search/Grid.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
*/
66
namespace Magento\Sales\Block\Adminhtml\Order\Create\Search;
77

8+
use Magento\Sales\Block\Adminhtml\Order\Create\Search\Grid\DataProvider\ProductCollection
9+
as ProductCollectionDataProvider;
10+
use Magento\Framework\App\ObjectManager;
11+
812
/**
913
* Adminhtml sales order create search products block
1014
*
1115
* @api
1216
* @author Magento Core Team <[email protected]>
1317
* @since 100.0.2
18+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1419
*/
1520
class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
1621
{
@@ -42,6 +47,11 @@ class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
4247
*/
4348
protected $_productFactory;
4449

50+
/**
51+
* @var ProductCollectionDataProvider $productCollectionProvider
52+
*/
53+
private $productCollectionProvider;
54+
4555
/**
4656
* @param \Magento\Backend\Block\Template\Context $context
4757
* @param \Magento\Backend\Helper\Data $backendHelper
@@ -50,6 +60,7 @@ class Grid extends \Magento\Backend\Block\Widget\Grid\Extended
5060
* @param \Magento\Backend\Model\Session\Quote $sessionQuote
5161
* @param \Magento\Sales\Model\Config $salesConfig
5262
* @param array $data
63+
* @param ProductCollectionDataProvider|null $productCollectionProvider
5364
*/
5465
public function __construct(
5566
\Magento\Backend\Block\Template\Context $context,
@@ -58,12 +69,15 @@ public function __construct(
5869
\Magento\Catalog\Model\Config $catalogConfig,
5970
\Magento\Backend\Model\Session\Quote $sessionQuote,
6071
\Magento\Sales\Model\Config $salesConfig,
61-
array $data = []
72+
array $data = [],
73+
ProductCollectionDataProvider $productCollectionProvider = null
6274
) {
6375
$this->_productFactory = $productFactory;
6476
$this->_catalogConfig = $catalogConfig;
6577
$this->_sessionQuote = $sessionQuote;
6678
$this->_salesConfig = $salesConfig;
79+
$this->productCollectionProvider = $productCollectionProvider
80+
?: ObjectManager::getInstance()->get(ProductCollectionDataProvider::class);
6781
parent::__construct($context, $backendHelper, $data);
6882
}
6983

@@ -140,20 +154,18 @@ protected function _addColumnFilterToCollection($column)
140154
*/
141155
protected function _prepareCollection()
142156
{
157+
143158
$attributes = $this->_catalogConfig->getProductAttributes();
159+
$store = $this->getStore();
160+
144161
/* @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
145-
$collection = $this->_productFactory->create()->getCollection();
146-
$collection->setStore(
147-
$this->getStore()
148-
)->addAttributeToSelect(
162+
$collection = $this->productCollectionProvider->getCollectionForStore($store);
163+
$collection->addAttributeToSelect(
149164
$attributes
150-
)->addAttributeToSelect(
151-
'sku'
152-
)->addStoreFilter()->addAttributeToFilter(
165+
);
166+
$collection->addAttributeToFilter(
153167
'type_id',
154168
$this->_salesConfig->getAvailableProductTypes()
155-
)->addAttributeToSelect(
156-
'gift_message_available'
157169
);
158170

159171
$this->setCollection($collection);
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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\Sales\Block\Adminhtml\Order\Create\Search\Grid\DataProvider;
9+
10+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
11+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
12+
use Magento\Store\Model\Store;
13+
14+
/**
15+
* Prepares product collection for the grid
16+
*/
17+
class ProductCollection
18+
{
19+
/**
20+
* @var ProductCollectionFactory
21+
*/
22+
private $collectionFactory;
23+
24+
/**
25+
* @param ProductCollectionFactory $collectionFactory
26+
*/
27+
public function __construct(
28+
ProductCollectionFactory $collectionFactory
29+
) {
30+
$this->collectionFactory = $collectionFactory;
31+
}
32+
33+
/**
34+
* Provide products collection filtered with store
35+
*
36+
* @param Store $store
37+
* @return Collection
38+
*/
39+
public function getCollectionForStore(Store $store):Collection
40+
{
41+
/** @var Collection $collection */
42+
$collection = $this->collectionFactory->create();
43+
44+
$collection->setStore($store);
45+
$collection->addAttributeToSelect(
46+
'gift_message_available'
47+
);
48+
$collection->addAttributeToSelect(
49+
'sku'
50+
);
51+
$collection->addStoreFilter();
52+
53+
return $collection;
54+
}
55+
}

0 commit comments

Comments
 (0)