/** * Process category data after save category object * save related products ids and update path value * * @param \Magento\Framework\Object $object * @return $this */ protected function _afterSave(\Magento\Framework\Object $object) { /** * Add identifier for new category */ if (substr($object->getPath(), -1) == '/') { $object->setPath($object->getPath() . $object->getId()); $this->_savePath($object); } $this->_saveCategoryProducts($object); return parent::_afterSave($object); }
/** * Retrieve category child data objects * * @param \Magento\Framework\Object $category * @return \Magento\Framework\Object */ public function loadCategoryChilds(\Magento\Framework\Object $category) { if ($category->getId() === null || $category->getStoreId() === null) { return $category; } $categories = $this->_getCategories(null, $category->getStoreId(), $category->getPath() . '/'); $category->setChilds(array()); foreach ($categories as $child) { if (!is_array($child->getChilds())) { $child->setChilds(array()); } if ($child->getParentId() == $category->getId()) { $category->setChilds($category->getChilds() + array($child->getId() => $child)); } else { if (isset($categories[$child->getParentId()])) { if (!is_array($categories[$child->getParentId()]->getChilds())) { $categories[$child->getParentId()]->setChilds(array()); } $categories[$child->getParentId()]->setChilds($categories[$child->getParentId()]->getChilds() + array($child->getId() => $child)); } } } $category->setAllChilds($categories); return $category; }
/** * Prepare category parentId * * @param \Magento\Framework\Object $category * @return $this */ protected function _prepareCategoryParentId(\Magento\Framework\Object $category) { if ($category->getPath() != $category->getId()) { $split = explode('/', $category->getPath()); $category->setParentId($split[count($split) - 2]); } else { $category->setParentId(0); } return $this; }
/** * Refresh product rewrite * * @param \Magento\Framework\Object $product * @param \Magento\Framework\Object $category * @return $this */ protected function _refreshProductRewrite(\Magento\Framework\Object $product, \Magento\Framework\Object $category) { if ($category->getId() == $category->getPath()) { return $this; } if ($product->getUrlKey() == '') { $urlKey = $this->productUrl->formatUrlKey($product->getName()); } else { $urlKey = $this->productUrl->formatUrlKey($product->getUrlKey()); } $idPath = $this->generatePath('id', $product, $category); $targetPath = $this->generatePath('target', $product, $category); $requestPath = $this->getProductRequestPath($product, $category); $categoryId = null; $updateKeys = true; if ($category->getLevel() > 1) { $categoryId = $category->getId(); $updateKeys = false; } $rewriteData = array('store_id' => $category->getStoreId(), 'category_id' => $categoryId, 'product_id' => $product->getId(), 'id_path' => $idPath, 'request_path' => $requestPath, 'target_path' => $targetPath, 'is_system' => 1); $this->getResource()->saveRewrite($rewriteData, $this->_rewrite); if ($this->getShouldSaveRewritesHistory($category->getStoreId())) { $this->_saveRewriteHistory($rewriteData, $this->_rewrite); } if ($updateKeys && $product->getUrlKey() != $urlKey) { $product->setUrlKey($urlKey); $this->getResource()->saveProductAttribute($product, 'url_key'); } if ($updateKeys && $product->getUrlPath() != $requestPath) { $product->setUrlPath($requestPath); $this->getResource()->saveProductAttribute($product, 'url_path'); } return $this; }