-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #634 from magento-dragons/MAGETWO-54704
MAGETWO-54704: [Backport] [GITHUB] Product prices not scoped to Website
- Loading branch information
Showing
17 changed files
with
812 additions
and
83 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
app/code/Magento/Catalog/Cron/DeleteOutdatedPriceValues.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
/** | ||
* Copyright © 2016 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Catalog\Cron; | ||
|
||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Eav\Api\AttributeRepositoryInterface as AttributeRepository; | ||
use Magento\Framework\App\Config\MutableScopeConfigInterface as ScopeConfig; | ||
use Magento\Catalog\Api\Data\ProductAttributeInterface; | ||
use Magento\Store\Model\Store; | ||
|
||
/** | ||
* Cron operation is responsible for deleting all product prices on WEBSITE level | ||
* in case 'Catalog Price Scope' configuratoin parameter is set to GLOBAL. | ||
*/ | ||
class DeleteOutdatedPriceValues | ||
{ | ||
/** | ||
* @var ResourceConnection | ||
*/ | ||
private $resource; | ||
|
||
/** | ||
* @var AttributeRepository | ||
*/ | ||
private $attributeRepository; | ||
|
||
/** | ||
* @var ScopeConfig | ||
*/ | ||
private $scopeConfig; | ||
|
||
/** | ||
* @param ResourceConnection $resource | ||
* @param AttributeRepository $attributeRepository | ||
* @param ScopeConfig $scopeConfig | ||
*/ | ||
public function __construct( | ||
ResourceConnection $resource, | ||
AttributeRepository $attributeRepository, | ||
ScopeConfig $scopeConfig | ||
) { | ||
$this->resource = $resource; | ||
$this->attributeRepository = $attributeRepository; | ||
$this->scopeConfig = $scopeConfig; | ||
} | ||
|
||
/** | ||
* Delete all price values for non-admin stores if PRICE_SCOPE is global | ||
* | ||
* @return void | ||
*/ | ||
public function execute() | ||
{ | ||
$priceScope = $this->scopeConfig->getValue(Store::XML_PATH_PRICE_SCOPE); | ||
if ($priceScope == Store::PRICE_SCOPE_GLOBAL) { | ||
/** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $priceAttribute */ | ||
$priceAttribute = $this->attributeRepository | ||
->get(ProductAttributeInterface::ENTITY_TYPE_CODE, ProductAttributeInterface::CODE_PRICE); | ||
$connection = $this->resource->getConnection(); | ||
$conditions = [ | ||
$connection->quoteInto('attribute_id = ?', $priceAttribute->getId()), | ||
$connection->quoteInto('store_id != ?', Store::DEFAULT_STORE_ID), | ||
]; | ||
|
||
$connection->delete( | ||
$priceAttribute->getBackend()->getTable(), | ||
$conditions | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
app/code/Magento/Catalog/Observer/SwitchPriceAttributeScopeOnConfigChange.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
/** | ||
* Copyright © 2016 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Catalog\Observer; | ||
|
||
use Magento\Framework\Event\Observer as EventObserver; | ||
use Magento\Framework\Event\ObserverInterface; | ||
use Magento\Catalog\Api\Data\ProductAttributeInterface; | ||
use Magento\Catalog\Api\ProductAttributeRepositoryInterface; | ||
use Magento\Store\Model\Store; | ||
use Magento\Framework\App\Config\ReinitableConfigInterface; | ||
use Magento\Framework\Api\SearchCriteriaBuilder; | ||
|
||
/** | ||
* Observer is responsible for changing scope for all price attributes in system | ||
* depending on 'Catalog Price Scope' configuration parameter | ||
*/ | ||
class SwitchPriceAttributeScopeOnConfigChange implements ObserverInterface | ||
{ | ||
/** | ||
* @var ReinitableConfigInterface | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @var ProductAttributeRepositoryInterface | ||
*/ | ||
private $productAttributeRepository; | ||
|
||
/** | ||
* @var SearchCriteriaBuilder | ||
*/ | ||
private $searchCriteriaBuilder; | ||
|
||
/** | ||
* @param ReinitableConfigInterface $config | ||
* @param ProductAttributeRepositoryInterface $productAttributeRepository | ||
* @param SearchCriteriaBuilder $searchCriteriaBuilder | ||
*/ | ||
public function __construct( | ||
ReinitableConfigInterface $config, | ||
ProductAttributeRepositoryInterface $productAttributeRepository, | ||
SearchCriteriaBuilder $searchCriteriaBuilder | ||
){ | ||
$this->config = $config; | ||
$this->productAttributeRepository = $productAttributeRepository; | ||
$this->searchCriteriaBuilder = $searchCriteriaBuilder; | ||
} | ||
|
||
/** | ||
* Change scope for all price attributes according to | ||
* 'Catalog Price Scope' configuration parameter value | ||
* | ||
* @param EventObserver $observer | ||
* @return void | ||
*/ | ||
public function execute(EventObserver $observer) | ||
{ | ||
$this->searchCriteriaBuilder->addFilter('frontend_input', 'price'); | ||
$criteria = $this->searchCriteriaBuilder->create(); | ||
|
||
$scope = $this->config->getValue(Store::XML_PATH_PRICE_SCOPE); | ||
$scope = ($scope == Store::PRICE_SCOPE_WEBSITE) | ||
? ProductAttributeInterface::SCOPE_WEBSITE_TEXT | ||
: ProductAttributeInterface::SCOPE_GLOBAL_TEXT; | ||
|
||
$priceAttributes = $this->productAttributeRepository->getList($criteria)->getItems(); | ||
|
||
/** @var ProductAttributeInterface $priceAttribute */ | ||
foreach ($priceAttributes as $priceAttribute) { | ||
$priceAttribute->setScope($scope); | ||
$this->productAttributeRepository->save($priceAttribute); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.