/** * After save handler * * @param \Magento\Framework\App\Config\Value $subject * @param mixed $result * * @return mixed */ public function afterSave(\Magento\Framework\App\Config\Value $subject, $result) { if ($subject->isValueChanged()) { $this->_eavIndexerProcessor->markIndexerAsInvalid(); } return $result; }
/** * Test reindex for configurable product with both disabled and enabled variations. */ public function testReindexEntitiesForConfigurableProduct() { /** @var \Magento\Catalog\Model\Resource\Eav\Attribute $attr **/ $attr = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Eav\\Model\\Config')->getAttribute('catalog_product', 'test_configurable'); $attr->setIsFilterable(1)->save(); $this->_eavIndexerProcessor->reindexAll(); /** @var \Magento\Eav\Model\Resource\Entity\Attribute\Option\Collection $options **/ $options = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Eav\\Model\\Resource\\Entity\\Attribute\\Option\\Collection'); $options->setAttributeFilter($attr->getId())->load(); $optionIds = $options->getAllIds(); $adapter = $this->productResource->getReadConnection(); $select = $adapter->select()->from($this->productResource->getTable('catalog_product_index_eav'))->where('entity_id = ?', 1)->where('attribute_id = ?', $attr->getId())->where('value IN (?)', $optionIds); $result = $adapter->fetchAll($select); $this->assertCount(2, $result); /** @var \Magento\Catalog\Model\Product $product1 **/ $product1 = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Catalog\\Model\\Product'); $product1 = $product1->load(10); $product1->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED)->save(); /** @var \Magento\Catalog\Model\Product $product2 **/ $product2 = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Catalog\\Model\\Product'); $product2 = $product2->load(20); $product2->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED)->save(); $result = $adapter->fetchAll($select); $this->assertCount(0, $result); }
/** * After import handler * * @param \Magento\ImportExport\Model\Import $subject * @param Object $import * * @return mixed * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function afterImportSource(\Magento\ImportExport\Model\Import $subject, $import) { if (!$this->_indexerEavProcessor->isIndexerScheduled()) { $this->_indexerEavProcessor->markIndexerAsInvalid(); } return $import; }
/** * Update attribute values for entity list per store * * @param array $productIds * @param array $attrData * @param int $storeId * @return $this */ public function updateAttributes($productIds, $attrData, $storeId) { $this->_eventManager->dispatch('catalog_product_attribute_update_before', ['attributes_data' => &$attrData, 'product_ids' => &$productIds, 'store_id' => &$storeId]); $this->_getResource()->updateAttributes($productIds, $attrData, $storeId); $this->setData(['product_ids' => array_unique($productIds), 'attributes_data' => $attrData, 'store_id' => $storeId]); if ($this->_hasIndexableAttributes($attrData)) { $this->_productEavIndexerProcessor->reindexList(array_unique($productIds)); } $categoryIndexer = $this->indexerRegistry->get(\Magento\Catalog\Model\Indexer\Product\Category::INDEXER_ID); if (!$categoryIndexer->isScheduled()) { $categoryIndexer->reindexList(array_unique($productIds)); } return $this; }
/** * @magentoDataFixture Magento/Catalog/_files/products_with_multiselect_attribute.php */ public function testReindexMultiselectAttribute() { /** @var ProductRepositoryInterface $productRepository */ $productRepository = Bootstrap::getObjectManager()->create(ProductRepositoryInterface::class); /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attr **/ $attr = Bootstrap::getObjectManager()->get('Magento\\Eav\\Model\\Config')->getAttribute('catalog_product', 'multiselect_attribute'); $attr->setIsFilterable(1)->save(); /** @var $options \Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection */ $options = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\\Eav\\Model\\ResourceModel\\Entity\\Attribute\\Option\\Collection'); $options->setAttributeFilter($attr->getId()); $optionIds = $options->getAllIds(); $product1Id = $optionIds[0] * 10; $product2Id = $optionIds[1] * 10; /** @var \Magento\Catalog\Model\Product $product1 **/ $product1 = $productRepository->getById($product1Id); $product1->setSpecialFromDate(date('Y-m-d H:i:s')); $product1->setNewsFromDate(date('Y-m-d H:i:s')); $productRepository->save($product1); /** @var \Magento\Catalog\Model\Product $product2 **/ $product2 = $productRepository->getById($product2Id); $product1->setSpecialFromDate(date('Y-m-d H:i:s')); $product1->setNewsFromDate(date('Y-m-d H:i:s')); $productRepository->save($product2); $this->_eavIndexerProcessor->reindexAll(); $connection = $this->productResource->getConnection(); $select = $connection->select()->from($this->productResource->getTable('catalog_product_index_eav'))->where('entity_id in (?)', [$product1Id, $product2Id])->where('attribute_id = ?', $attr->getId()); $result = $connection->fetchAll($select); $this->assertCount(3, $result); }
/** * Invalidate EAV indexer if attribute set has indexable attributes changes * * @param \Magento\Eav\Model\Entity\Attribute\Set $subject * @param callable $proceed * * @return \Magento\Eav\Model\Entity\Attribute\Set * * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundSave(\Magento\Eav\Model\Entity\Attribute\Set $subject, \Closure $proceed) { $requiresReindex = false; if ($subject->getId()) { /** @var \Magento\Eav\Model\Entity\Attribute\Set $originalSet */ $originalSet = clone $subject; $originalSet->initFromSkeleton($subject->getId()); $originalAttributeCodes = array_flip($this->_attributeFilter->filter($originalSet)); $subjectAttributeCodes = array_flip($this->_attributeFilter->filter($subject)); $requiresReindex = (bool) count(array_merge(array_diff_key($subjectAttributeCodes, $originalAttributeCodes), array_diff_key($originalAttributeCodes, $subjectAttributeCodes))); } $result = $proceed(); if ($requiresReindex) { $this->_indexerEavProcessor->markIndexerAsInvalid(); } return $result; }
/** * Init indexing process after catalog eav attribute delete commit * * @return $this */ public function afterDeleteCommit() { parent::afterDeleteCommit(); if ($this->_isOriginalEnabledInFlat()) { $this->_productFlatIndexerProcessor->markIndexerAsInvalid(); } if ($this->_isOriginalIndexable()) { $this->_indexerEavProcessor->markIndexerAsInvalid(); } return $this; }
/** * @magentoDbIsolation disabled * @magentoAppIsolation enabled * @magentoDataFixture Magento/Catalog/_files/product_simple.php */ public function testReindexAll() { /** @var \Magento\Catalog\Model\ResourceModel\Eav\Attribute $attr **/ $attr = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Eav\\Model\\Config')->getAttribute('catalog_product', 'weight'); $attr->setIsFilterable(1)->save(); $this->assertTrue($attr->isIndexable()); $this->_processor->reindexAll(); $categoryFactory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Catalog\\Model\\CategoryFactory'); /** @var \Magento\Catalog\Block\Product\ListProduct $listProduct */ $listProduct = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get('Magento\\Catalog\\Block\\Product\\ListProduct'); $category = $categoryFactory->create()->load(2); $layer = $listProduct->getLayer(); $layer->setCurrentCategory($category); $productCollection = $layer->getProductCollection(); $productCollection->addAttributeToSelect('weight'); $this->assertCount(1, $productCollection); /** @var $product \Magento\Catalog\Model\Product */ foreach ($productCollection as $product) { $this->assertEquals('Simple Product', $product->getName()); $this->assertEquals('Short description', $product->getShortDescription()); $this->assertEquals(1, $product->getWeight()); } }
/** * Update attribute values for entity list per store * * @param array $productIds * @param array $attrData * @param int $storeId * @return $this */ public function updateAttributes($productIds, $attrData, $storeId) { $this->_eventManager->dispatch('catalog_product_attribute_update_before', array('attributes_data' => &$attrData, 'product_ids' => &$productIds, 'store_id' => &$storeId)); $this->_getResource()->updateAttributes($productIds, $attrData, $storeId); $this->setData(array('product_ids' => array_unique($productIds), 'attributes_data' => $attrData, 'store_id' => $storeId)); if ($this->_hasIndexableAttributes($attrData)) { $this->_productEavIndexerProcessor->reindexList(array_unique($productIds)); } // register mass action indexer event $this->_indexIndexer->processEntityAction($this, \Magento\Catalog\Model\Product::ENTITY, \Magento\Index\Model\Event::TYPE_MASS_ACTION); if (!$this->getCategoryIndexer()->isScheduled()) { $this->getCategoryIndexer()->reindexList(array_unique($productIds)); } return $this; }
/** * Reindex callback for EAV indexer * * @return void */ public function eavReindexCallback() { if ($this->isObjectNew() || $this->hasDataChanges()) { $this->_productEavIndexerProcessor->reindexRow($this->getEntityId()); } }
/** * Before save handler * * @param \Magento\Store\Model\Resource\Store $subject * @param \Magento\Framework\Model\AbstractModel $object * * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeSave(\Magento\Store\Model\Resource\Store $subject, \Magento\Framework\Model\AbstractModel $object) { if ((!$object->getId() || $object->dataHasChangedFor('group_id')) && $object->getIsActive()) { $this->_indexerEavProcessor->markIndexerAsInvalid(); } }