/** * @param Category $category * @return void */ protected function updateUrlPathForChildren(Category $category) { foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) { $childCategory->unsUrlPath(); $childCategory->setUrlPath($this->categoryUrlPathGenerator->getUrlPath($childCategory)); $childCategory->getResource()->saveAttribute($childCategory, 'url_path'); } }
/** * Generate list of children urls * * @param int $storeId * @param \Magento\Catalog\Model\Category $category * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] */ public function generate($storeId, Category $category) { $urls = []; foreach ($this->childrenCategoriesProvider->getChildren($category) as $childCategory) { $childCategory->setStoreId($storeId); $childCategory->setData('save_rewrites_history', $category->getData('save_rewrites_history')); $urls = array_merge($urls, $this->categoryUrlRewriteGeneratorFactory->create()->generate($childCategory)); } return $urls; }
public function testGetChildren() { $categoryLevel = 3; $this->select->expects($this->at(1))->method('where')->with('path LIKE :c_path')->willReturnSelf(); $this->select->expects($this->at(2))->method('where')->with('level <= :c_level')->willReturnSelf(); $this->category->expects($this->once())->method('isObjectNew')->willReturn(false); $this->category->expects($this->once())->method('getLevel')->willReturn($categoryLevel); $bind = ['c_path' => 'category-path/%', 'c_level' => $categoryLevel + 1]; $this->connection->expects($this->any())->method('fetchCol')->with($this->select, $bind)->willReturn(['id']); $this->childrenCategoriesProvider->getChildren($this->category, false); }
/** * @param Category $category * @return void */ protected function updateUrlPathForChildren(Category $category) { $children = $this->childrenCategoriesProvider->getChildren($category, true); if ($this->isGlobalScope($category->getStoreId())) { foreach ($children as $child) { foreach ($category->getStoreIds() as $storeId) { if ($this->storeViewService->doesEntityHaveOverriddenUrlPathForStore($storeId, $child->getId(), Category::ENTITY)) { $child->setStoreId($storeId); $this->updateUrlPathForCategory($child); } } } } else { foreach ($children as $child) { $child->setStoreId($category->getStoreId()); $this->updateUrlPathForCategory($child); } } }
/** * Generate url rewrites for products assigned to category * * @param Category $category * @return array */ public function generateProductUrlRewrites(Category $category) { $this->isSkippedProduct = []; $saveRewriteHistory = $category->getData('save_rewrites_history'); $storeId = $category->getStoreId(); $productUrls = []; if ($category->getAffectedProductIds()) { $this->isSkippedProduct = $category->getAffectedProductIds(); $collection = $this->productCollectionFactory->create()->setStoreId($storeId)->addIdFilter($category->getAffectedProductIds())->addAttributeToSelect('name')->addAttributeToSelect('url_key')->addAttributeToSelect('url_path'); foreach ($collection as $product) { $product->setStoreId($storeId); $product->setData('save_rewrites_history', $saveRewriteHistory); $productUrls = array_merge($productUrls, $this->productUrlRewriteGenerator->generate($product)); } } else { $productUrls = array_merge($productUrls, $this->getCategoryProductsUrlRewrites($category, $storeId, $saveRewriteHistory)); } foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) { $productUrls = array_merge($productUrls, $this->getCategoryProductsUrlRewrites($childCategory, $storeId, $saveRewriteHistory)); } return $productUrls; }