Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Action.php
Go to the documentation of this file.
1 <?php
8 
18 {
25 
29  protected $indexerRegistry;
30 
34  protected $_eavConfig;
35 
40 
52  public function __construct(
53  \Magento\Framework\Model\Context $context,
54  \Magento\Framework\Registry $registry,
55  \Magento\Catalog\Model\Product\WebsiteFactory $productWebsiteFactory,
56  \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
57  \Magento\Eav\Model\Config $eavConfig,
58  \Magento\Catalog\Model\Indexer\Product\Eav\Processor $productEavIndexerProcessor,
59  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
60  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
61  array $data = []
62  ) {
63  $this->_productWebsiteFactory = $productWebsiteFactory;
64  $this->indexerRegistry = $indexerRegistry;
65  $this->_eavConfig = $eavConfig;
66  $this->_productEavIndexerProcessor = $productEavIndexerProcessor;
67  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
68  }
69 
75  protected function _construct()
76  {
77  $this->_init(\Magento\Catalog\Model\ResourceModel\Product\Action::class);
78  }
79 
88  public function updateAttributes($productIds, $attrData, $storeId)
89  {
90  $this->_eventManager->dispatch(
91  'catalog_product_attribute_update_before',
92  ['attributes_data' => &$attrData, 'product_ids' => &$productIds, 'store_id' => &$storeId]
93  );
94 
95  $this->_getResource()->updateAttributes($productIds, $attrData, $storeId);
96  $this->setData(
97  ['product_ids' => array_unique($productIds), 'attributes_data' => $attrData, 'store_id' => $storeId]
98  );
99 
100  if ($this->_hasIndexableAttributes($attrData)) {
101  $this->_productEavIndexerProcessor->reindexList(array_unique($productIds));
102  }
103 
104  $categoryIndexer = $this->indexerRegistry->get(\Magento\Catalog\Model\Indexer\Product\Category::INDEXER_ID);
105  if (!$categoryIndexer->isScheduled()) {
106  $categoryIndexer->reindexList(array_unique($productIds));
107  }
108  return $this;
109  }
110 
119  {
120  foreach ($attributesData as $code => $value) {
121  if ($this->_attributeIsIndexable($code)) {
122  return true;
123  }
124  }
125  return false;
126  }
127 
134  protected function _attributeIsIndexable($attribute)
135  {
136  if (!$attribute instanceof \Magento\Catalog\Model\ResourceModel\Eav\Attribute) {
137  $attribute = $this->_eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $attribute);
138  }
139 
140  return $attribute->isIndexable();
141  }
142 
156  {
157  if ($type == 'add') {
158  $this->_productWebsiteFactory->create()->addProducts($websiteIds, $productIds);
159  } elseif ($type == 'remove') {
160  $this->_productWebsiteFactory->create()->removeProducts($websiteIds, $productIds);
161  }
162 
163  $this->setData(
164  ['product_ids' => array_unique($productIds), 'website_ids' => $websiteIds, 'action_type' => $type]
165  );
166 
167  $categoryIndexer = $this->indexerRegistry->get(\Magento\Catalog\Model\Indexer\Product\Category::INDEXER_ID);
168  if (!$categoryIndexer->isScheduled()) {
169  $categoryIndexer->reindexList(array_unique($productIds));
170  }
171  }
172 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Catalog\Model\Product\WebsiteFactory $productWebsiteFactory, \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry, \Magento\Eav\Model\Config $eavConfig, \Magento\Catalog\Model\Indexer\Product\Eav\Processor $productEavIndexerProcessor, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Action.php:52
$resource
Definition: bulk.php:12
$type
Definition: item.phtml:13
$attributesData
$value
Definition: gender.phtml:16
updateAttributes($productIds, $attrData, $storeId)
Definition: Action.php:88
updateWebsites($productIds, $websiteIds, $type)
Definition: Action.php:155
_hasIndexableAttributes($attributesData)
Definition: Action.php:118
$code
Definition: info.phtml:12