Example #1
0
 /**
  * Register event data during category save process
  *
  * @param \Magento\Index\Model\Event $event
  * @return void
  */
 protected function _registerCategoryEvent(\Magento\Index\Model\Event $event)
 {
     $category = $event->getDataObject();
     if (!$category->getInitialSetupFlag() && $category->getLevel() > 1) {
         if ($category->dataHasChangedFor('url_key') || $category->getIsChangedProductList()) {
             $event->addNewData('rewrite_category_ids', array($category->getId()));
         }
         /**
          * Check if category has another affected category ids (category move result)
          */
         if ($category->getAffectedCategoryIds()) {
             $event->addNewData('rewrite_category_ids', $category->getAffectedCategoryIds());
         }
     }
 }
Example #2
0
 /**
  * Register data required by catatalog product process in event object
  *
  * @param Event $event
  * @return $this
  */
 protected function _registerCatalogProductEvent(Event $event)
 {
     switch ($event->getType()) {
         case Event::TYPE_SAVE:
             /* @var $product Product */
             $product = $event->getDataObject();
             $event->addNewData('catalogsearch_update_product_id', $product->getId());
             break;
         case Event::TYPE_DELETE:
             /* @var $product Product */
             $product = $event->getDataObject();
             $event->addNewData('catalogsearch_delete_product_id', $product->getId());
             break;
         case Event::TYPE_MASS_ACTION:
             /* @var $actionObject \Magento\Framework\Object */
             $actionObject = $event->getDataObject();
             $reindexData = array();
             $rebuildIndex = false;
             // check if status changed
             $attrData = $actionObject->getAttributesData();
             if (isset($attrData['status'])) {
                 $rebuildIndex = true;
                 $reindexData['catalogsearch_status'] = $attrData['status'];
             }
             // check changed websites
             if ($actionObject->getWebsiteIds()) {
                 $rebuildIndex = true;
                 $reindexData['catalogsearch_website_ids'] = $actionObject->getWebsiteIds();
                 $reindexData['catalogsearch_action_type'] = $actionObject->getActionType();
             }
             $searchableAttributes = array();
             if (is_array($attrData)) {
                 $searchableAttributes = array_intersect($this->_getSearchableAttributes(), array_keys($attrData));
             }
             if (count($searchableAttributes) > 0) {
                 $rebuildIndex = true;
                 $reindexData['catalogsearch_force_reindex'] = true;
             }
             // register affected products
             if ($rebuildIndex) {
                 $reindexData['catalogsearch_product_ids'] = $actionObject->getProductIds();
                 foreach ($reindexData as $k => $v) {
                     $event->addNewData($k, $v);
                 }
             }
             break;
         default:
             break;
     }
     return $this;
 }