CheckSalesRulesAvailability.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Observer;
  7. class CheckSalesRulesAvailability
  8. {
  9. /**
  10. * @var \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory
  11. */
  12. protected $_collectionFactory;
  13. /**
  14. * @var \Magento\Framework\Message\ManagerInterface
  15. */
  16. protected $messageManager;
  17. /**
  18. * @param \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory $collectionFactory
  19. * @param \Magento\Framework\Message\ManagerInterface $messageManager
  20. */
  21. public function __construct(
  22. \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory $collectionFactory,
  23. \Magento\Framework\Message\ManagerInterface $messageManager
  24. ) {
  25. $this->_collectionFactory = $collectionFactory;
  26. $this->messageManager = $messageManager;
  27. }
  28. /**
  29. * Check rules that contains affected attribute
  30. * If rules were found they will be set to inactive and notice will be add to admin session
  31. *
  32. * @param string $attributeCode
  33. * @return $this
  34. */
  35. public function checkSalesRulesAvailability($attributeCode)
  36. {
  37. /* @var $collection \Magento\SalesRule\Model\ResourceModel\Rule\Collection */
  38. $collection = $this->_collectionFactory->create()->addAttributeInConditionFilter($attributeCode);
  39. $disabledRulesCount = 0;
  40. foreach ($collection as $rule) {
  41. /* @var $rule \Magento\SalesRule\Model\Rule */
  42. $rule->setIsActive(0);
  43. /* @var $rule->getConditions() \Magento\SalesRule\Model\Rule\Condition\Combine */
  44. $this->_removeAttributeFromConditions($rule->getConditions(), $attributeCode);
  45. $this->_removeAttributeFromConditions($rule->getActions(), $attributeCode);
  46. $rule->save();
  47. $disabledRulesCount++;
  48. }
  49. if ($disabledRulesCount) {
  50. $this->messageManager->addWarningMessage(
  51. __(
  52. '%1 Cart Price Rules based on "%2" attribute have been disabled.',
  53. $disabledRulesCount,
  54. $attributeCode
  55. )
  56. );
  57. }
  58. return $this;
  59. }
  60. /**
  61. * Remove catalog attribute condition by attribute code from rule conditions
  62. *
  63. * @param \Magento\Rule\Model\Condition\Combine $combine
  64. * @param string $attributeCode
  65. * @return void
  66. */
  67. protected function _removeAttributeFromConditions($combine, $attributeCode)
  68. {
  69. $conditions = $combine->getConditions();
  70. foreach ($conditions as $conditionId => $condition) {
  71. if ($condition instanceof \Magento\Rule\Model\Condition\Combine) {
  72. $this->_removeAttributeFromConditions($condition, $attributeCode);
  73. }
  74. if ($condition instanceof \Magento\SalesRule\Model\Rule\Condition\Product) {
  75. if ($condition->getAttribute() == $attributeCode) {
  76. unset($conditions[$conditionId]);
  77. }
  78. }
  79. }
  80. $combine->setConditions($conditions);
  81. }
  82. }