DeleteOutdatedPriceValues.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Cron;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Eav\Api\AttributeRepositoryInterface as AttributeRepository;
  9. use Magento\Framework\App\Config\MutableScopeConfigInterface as ScopeConfig;
  10. use Magento\Catalog\Api\Data\ProductAttributeInterface;
  11. use Magento\Store\Model\Store;
  12. /**
  13. * Cron job for removing outdated prices.
  14. *
  15. * Cron operation is responsible for deleting all product prices on WEBSITE level
  16. * in case 'Catalog Price Scope' configuration parameter is set to GLOBAL.
  17. */
  18. class DeleteOutdatedPriceValues
  19. {
  20. /**
  21. * @var ResourceConnection
  22. */
  23. private $resource;
  24. /**
  25. * @var AttributeRepository
  26. */
  27. private $attributeRepository;
  28. /**
  29. * @var ScopeConfig
  30. */
  31. private $scopeConfig;
  32. /**
  33. * @param ResourceConnection $resource
  34. * @param AttributeRepository $attributeRepository
  35. * @param ScopeConfig $scopeConfig
  36. */
  37. public function __construct(
  38. ResourceConnection $resource,
  39. AttributeRepository $attributeRepository,
  40. ScopeConfig $scopeConfig
  41. ) {
  42. $this->resource = $resource;
  43. $this->attributeRepository = $attributeRepository;
  44. $this->scopeConfig = $scopeConfig;
  45. }
  46. /**
  47. * Delete all price values for non-admin stores if PRICE_SCOPE is set to global.
  48. *
  49. * @return void
  50. */
  51. public function execute()
  52. {
  53. if (!$this->isPriceScopeSetToGlobal()) {
  54. return;
  55. }
  56. /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $priceAttribute */
  57. $priceAttribute = $this->attributeRepository
  58. ->get(ProductAttributeInterface::ENTITY_TYPE_CODE, ProductAttributeInterface::CODE_PRICE);
  59. $connection = $this->resource->getConnection();
  60. $conditions = [
  61. $connection->quoteInto('attribute_id = ?', $priceAttribute->getId()),
  62. $connection->quoteInto('store_id != ?', Store::DEFAULT_STORE_ID),
  63. ];
  64. $connection->delete(
  65. $priceAttribute->getBackend()->getTable(),
  66. $conditions
  67. );
  68. }
  69. /**
  70. * Checks if price scope config option explicitly equal to global value.
  71. *
  72. * Such strict comparison is required to prevent price deleting when
  73. * price scope config option is null for some reason.
  74. *
  75. * @return bool
  76. */
  77. private function isPriceScopeSetToGlobal()
  78. {
  79. $priceScope = $this->scopeConfig->getValue(Store::XML_PATH_PRICE_SCOPE);
  80. if ($priceScope === null) {
  81. return false;
  82. }
  83. return (int)$priceScope === Store::PRICE_SCOPE_GLOBAL;
  84. }
  85. }