Skip to content

Commit 66b7ff4

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-98013' into 2.3-develop-pr17
2 parents 250045e + 1845bcf commit 66b7ff4

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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\CatalogInventory\Ui\DataProvider\Product;
9+
10+
use Magento\Framework\Data\Collection;
11+
use Magento\Ui\DataProvider\AddFieldToCollectionInterface;
12+
13+
/**
14+
* Add quantity_and_stock_status field to collection
15+
*/
16+
class AddQuantityAndStockStatusFieldToCollection implements AddFieldToCollectionInterface
17+
{
18+
/**
19+
* @inheritdoc
20+
*/
21+
public function addField(Collection $collection, $field, $alias = null)
22+
{
23+
$collection->joinField(
24+
'quantity_and_stock_status',
25+
'cataloginventory_stock_item',
26+
'is_in_stock',
27+
'product_id=entity_id',
28+
'{{table}}.stock_id=1',
29+
'left'
30+
);
31+
}
32+
}

app/code/Magento/CatalogInventory/etc/adminhtml/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<arguments>
2424
<argument name="addFieldStrategies" xsi:type="array">
2525
<item name="qty" xsi:type="object">Magento\CatalogInventory\Ui\DataProvider\Product\AddQuantityFieldToCollection</item>
26+
<item name="quantity_and_stock_status" xsi:type="object">Magento\CatalogInventory\Ui\DataProvider\Product\AddQuantityAndStockStatusFieldToCollection</item>
2627
</argument>
2728
<argument name="addFilterStrategies" xsi:type="array">
2829
<item name="qty" xsi:type="object">Magento\CatalogInventory\Ui\DataProvider\Product\AddQuantityFilterToCollection</item>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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\Catalog\Ui\DataProvider\Product;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\CatalogInventory\Model\Stock\StockItemRepository;
12+
use Magento\CatalogInventory\Ui\DataProvider\Product\AddQuantityAndStockStatusFieldToCollection;
13+
use PHPUnit\Framework\TestCase;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use Magento\CatalogInventory\Api\StockItemCriteriaInterface;
16+
use Magento\CatalogInventory\Api\StockRegistryInterface;
17+
18+
/**
19+
* Quantity and stock status test
20+
*/
21+
class QuantityAndStockStatusTest extends TestCase
22+
{
23+
/**
24+
* @var string
25+
*/
26+
private static $quantityAndStockStatus = 'quantity_and_stock_status';
27+
28+
/**
29+
* @var \Magento\Framework\ObjectManagerInterface
30+
*/
31+
private $objectManager;
32+
33+
/**
34+
* @inheritdoc
35+
*/
36+
protected function setUp()
37+
{
38+
$this->objectManager = Bootstrap::getObjectManager();
39+
}
40+
41+
/**
42+
* Test product stock status in the products grid column
43+
*
44+
* @magentoDataFixture Magento/Catalog/_files/quantity_and_stock_status_attribute_used_in_grid.php
45+
* @magentoDataFixture Magento/Catalog/_files/products.php
46+
*/
47+
public function testProductStockStatus()
48+
{
49+
/** @var StockItemRepository $stockItemRepository */
50+
$stockItemRepository = $this->objectManager->create(StockItemRepository::class);
51+
52+
/** @var StockRegistryInterface $stockRegistry */
53+
$stockRegistry = $this->objectManager->create(StockRegistryInterface::class);
54+
55+
$stockItem = $stockRegistry->getStockItemBySku('simple');
56+
$stockItem->setIsInStock(false);
57+
$stockItemRepository->save($stockItem);
58+
$savedStockStatus = (int)$stockItem->getIsInStock();
59+
60+
$dataProvider = $this->objectManager->create(
61+
ProductDataProvider::class,
62+
[
63+
'name' => 'product_listing_data_source',
64+
'primaryFieldName' => 'entity_id',
65+
'requestFieldName' => 'id',
66+
'addFieldStrategies' => [
67+
'quantity_and_stock_status' =>
68+
$this->objectManager->get(AddQuantityAndStockStatusFieldToCollection::class)
69+
]
70+
]
71+
);
72+
73+
$dataProvider->addField(self::$quantityAndStockStatus);
74+
$data = $dataProvider->getData();
75+
$dataProviderStockStatus = $data['items'][0][self::$quantityAndStockStatus];
76+
77+
$this->assertEquals($dataProviderStockStatus, $savedStockStatus);
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
9+
$eavSetupFactory = $objectManager->create(\Magento\Eav\Setup\EavSetupFactory::class);
10+
/** @var \Magento\Eav\Setup\EavSetup $eavSetup */
11+
$eavSetup = $eavSetupFactory->create();
12+
$eavSetup->updateAttribute(
13+
\Magento\Catalog\Model\Product::ENTITY,
14+
'quantity_and_stock_status',
15+
[
16+
'is_used_in_grid' => 1,
17+
]
18+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
9+
$eavSetupFactory = $objectManager->create(\Magento\Eav\Setup\EavSetupFactory::class);
10+
/** @var \Magento\Eav\Setup\EavSetup $eavSetup */
11+
$eavSetup = $eavSetupFactory->create();
12+
$eavSetup->updateAttribute(
13+
\Magento\Catalog\Model\Product::ENTITY,
14+
'quantity_and_stock_status',
15+
[
16+
'is_used_in_grid' => 0,
17+
]
18+
);

0 commit comments

Comments
 (0)