Skip to content

Commit b1f9109

Browse files
committed
Merge branch 'MAGETWO-75222' into MPI-PR-2.1.10
2 parents 5ef3f40 + aea969e commit b1f9109

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed

app/code/Magento/Catalog/Cron/DeleteOutdatedPriceValues.php

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
/**
1515
* Cron operation is responsible for deleting all product prices on WEBSITE level
16-
* in case 'Catalog Price Scope' configuratoin parameter is set to GLOBAL.
16+
* in case 'Catalog Price Scope' configuration parameter is set to GLOBAL.
1717
*/
1818
class DeleteOutdatedPriceValues
1919
{
@@ -48,27 +48,46 @@ public function __construct(
4848
}
4949

5050
/**
51-
* Delete all price values for non-admin stores if PRICE_SCOPE is global
51+
* Delete all price values for non-admin stores if PRICE_SCOPE is set to global.
5252
*
5353
* @return void
5454
*/
5555
public function execute()
5656
{
57-
$priceScope = $this->scopeConfig->getValue(Store::XML_PATH_PRICE_SCOPE);
58-
if ($priceScope == Store::PRICE_SCOPE_GLOBAL) {
59-
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $priceAttribute */
60-
$priceAttribute = $this->attributeRepository
61-
->get(ProductAttributeInterface::ENTITY_TYPE_CODE, ProductAttributeInterface::CODE_PRICE);
62-
$connection = $this->resource->getConnection();
63-
$conditions = [
64-
$connection->quoteInto('attribute_id = ?', $priceAttribute->getId()),
65-
$connection->quoteInto('store_id != ?', Store::DEFAULT_STORE_ID),
66-
];
57+
if ($this->isPriceScopeSetToGlobal() === false) {
58+
return;
59+
}
60+
61+
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $priceAttribute */
62+
$priceAttribute = $this->attributeRepository
63+
->get(ProductAttributeInterface::ENTITY_TYPE_CODE, ProductAttributeInterface::CODE_PRICE);
64+
$connection = $this->resource->getConnection();
65+
$conditions = [
66+
$connection->quoteInto('attribute_id = ?', $priceAttribute->getId()),
67+
$connection->quoteInto('store_id != ?', Store::DEFAULT_STORE_ID),
68+
];
6769

68-
$connection->delete(
69-
$priceAttribute->getBackend()->getTable(),
70-
$conditions
71-
);
70+
$connection->delete(
71+
$priceAttribute->getBackend()->getTable(),
72+
$conditions
73+
);
74+
}
75+
76+
/**
77+
* Checks if price scope config option explicitly equal to global value.
78+
*
79+
* Such strict comparision is required to prevent price deleting when
80+
* price scope config option is null for some reason.
81+
*
82+
* @return bool
83+
*/
84+
private function isPriceScopeSetToGlobal()
85+
{
86+
$priceScope = $this->scopeConfig->getValue(Store::XML_PATH_PRICE_SCOPE);
87+
if ($priceScope === null) {
88+
return false;
7289
}
90+
91+
return (int)$priceScope === Store::PRICE_SCOPE_GLOBAL;
7392
}
7493
}

dev/tests/integration/testsuite/Magento/Catalog/Cron/DeleteOutdatedPriceValuesTest.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,13 @@ public function setUp()
4242
/**
4343
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
4444
* @magentoDataFixture Magento/Store/_files/second_website_with_two_stores.php
45-
* @magentoConfigFixture current_store catalog/price/scope 2
45+
* @magentoConfigFixture current_store catalog/price/scope 1
4646
* @magentoDbIsolation enabled
4747
*/
4848
public function testExecute()
4949
{
50+
$defaultStorePrice = 10.00;
51+
$secondStorePrice = 9.99;
5052
$secondStoreId = $this->store->load('fixture_second_store')->getId();
5153
/** @var \Magento\Catalog\Model\Product\Action $productAction */
5254
$productAction = $this->objectManager->create(
@@ -64,7 +66,7 @@ public function testExecute()
6466
);
6567
$product->setOrigData();
6668
$product->setStoreId($secondStoreId);
67-
$product->setPrice(9.99);
69+
$product->setPrice($secondStorePrice);
6870

6971
$productResource->save($product);
7072
$attribute = $this->objectManager->get(\Magento\Eav\Model\Config::class)
@@ -73,22 +75,33 @@ public function testExecute()
7375
'price'
7476
);
7577
$this->assertEquals(
76-
'9.99',
78+
$secondStorePrice,
7779
$productResource->getAttributeRawValue($productId, $attribute->getId(), $secondStoreId)
7880
);
7981
/** @var MutableScopeConfigInterface $config */
8082
$config = $this->objectManager->get(
8183
MutableScopeConfigInterface::class
8284
);
85+
86+
$config->setValue(
87+
\Magento\Store\Model\Store::XML_PATH_PRICE_SCOPE,
88+
null,
89+
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
90+
);
91+
$this->cron->execute();
92+
$this->assertEquals(
93+
$secondStorePrice,
94+
$productResource->getAttributeRawValue($productId, $attribute->getId(), $secondStoreId)
95+
);
96+
8397
$config->setValue(
8498
\Magento\Store\Model\Store::XML_PATH_PRICE_SCOPE,
8599
\Magento\Store\Model\Store::PRICE_SCOPE_GLOBAL,
86100
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
87101
);
88-
/** @var \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig */
89102
$this->cron->execute();
90103
$this->assertEquals(
91-
'10.0000',
104+
$defaultStorePrice,
92105
$productResource->getAttributeRawValue($productId, $attribute->getId(), $secondStoreId)
93106
);
94107
}

0 commit comments

Comments
 (0)