Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CheckSalesRulesAvailability.php
Go to the documentation of this file.
1 <?php
7 
9 {
14 
18  protected $messageManager;
19 
24  public function __construct(
25  \Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory $collectionFactory,
26  \Magento\Framework\Message\ManagerInterface $messageManager
27  ) {
28  $this->_collectionFactory = $collectionFactory;
29  $this->messageManager = $messageManager;
30  }
31 
40  {
41  /* @var $collection \Magento\SalesRule\Model\ResourceModel\Rule\Collection */
42  $collection = $this->_collectionFactory->create()->addAttributeInConditionFilter($attributeCode);
43 
44  $disabledRulesCount = 0;
45  foreach ($collection as $rule) {
46  /* @var $rule \Magento\SalesRule\Model\Rule */
47  $rule->setIsActive(0);
48  /* @var $rule->getConditions() \Magento\SalesRule\Model\Rule\Condition\Combine */
49  $this->_removeAttributeFromConditions($rule->getConditions(), $attributeCode);
50  $this->_removeAttributeFromConditions($rule->getActions(), $attributeCode);
51  $rule->save();
52 
53  $disabledRulesCount++;
54  }
55 
56  if ($disabledRulesCount) {
57  $this->messageManager->addWarningMessage(
58  __(
59  '%1 Cart Price Rules based on "%2" attribute have been disabled.',
60  $disabledRulesCount,
62  )
63  );
64  }
65 
66  return $this;
67  }
68 
76  protected function _removeAttributeFromConditions($combine, $attributeCode)
77  {
78  $conditions = $combine->getConditions();
79  foreach ($conditions as $conditionId => $condition) {
80  if ($condition instanceof \Magento\Rule\Model\Condition\Combine) {
82  }
83  if ($condition instanceof \Magento\SalesRule\Model\Rule\Condition\Product) {
84  if ($condition->getAttribute() == $attributeCode) {
85  unset($conditions[$conditionId]);
86  }
87  }
88  }
89  $combine->setConditions($conditions);
90  }
91 }
__()
Definition: __.php:13
__construct(\Magento\SalesRule\Model\ResourceModel\Rule\CollectionFactory $collectionFactory, \Magento\Framework\Message\ManagerInterface $messageManager)
$attributeCode
Definition: extend.phtml:12