/** * @param \Magento\UrlRewrite\Service\V1\Data\UrlRewrite $url * @param int $storeId * @return array */ protected function generateForAutogenerated($url, $storeId) { $urls = []; if ($this->category->getData('save_rewrites_history')) { $targetPath = $this->categoryUrlPathGenerator->getUrlPathWithSuffix($this->category, $storeId); if ($url->getRequestPath() !== $targetPath) { $urls[$url->getRequestPath() . '_' . $storeId] = $this->urlRewriteFactory->create()->setEntityType(CategoryUrlRewriteGenerator::ENTITY_TYPE)->setEntityId($this->category->getId())->setRequestPath($url->getRequestPath())->setTargetPath($targetPath)->setRedirectType(OptionProvider::PERMANENT)->setStoreId($storeId)->setIsAutogenerated(0); } } return $urls; }
public function testGetCurrentCategoryIfCurrentCategoryIsNotSet() { $rootCategoryId = 333; $this->currentCategory->getData('current_category', null); $this->registry->expects($this->once())->method('registry')->with($this->equalTo('current_category'))->willReturn(null); $this->categoryRepository->expects($this->once())->method('get')->with($rootCategoryId)->willReturn($this->category); $this->store->expects($this->any())->method('getRootCategoryId')->will($this->returnValue($rootCategoryId)); $this->assertEquals($this->currentCategory, $this->model->getCurrentCategory()); $this->assertEquals($this->currentCategory, $this->model->getData('current_category')); }
/** * Generate permanent rewrites based on current rewrites * * @param int $storeId * @return array */ protected function generateRewritesBasedOnCurrentRewrites($storeId) { $urls = []; foreach ($this->urlMatcher->findAllByFilter($this->createCurrentUrlRewritesFilter($storeId)) as $url) { $targetPath = null; if ($url->getRedirectType()) { $targetPath = str_replace($this->category->getOrigData('url_key'), $this->category->getData('url_key'), $url->getTargetPath()); $redirectType = $url->getRedirectType(); } elseif ($this->category->getData('save_rewrites_history')) { $targetPath = str_replace($this->category->getOrigData('url_key'), $this->category->getData('url_key'), $url->getRequestPath()); $redirectType = OptionProvider::PERMANENT; } if ($targetPath && $url->getRequestPath() != $targetPath) { $urls[] = $this->createUrlRewrite($storeId, $url->getRequestPath(), $targetPath, $redirectType); } } return $urls; }
/** * 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; }
/** * Retrieve category url * * @param ModelCategory $category * @return string */ public function getCategoryUrl($category) { if ($category instanceof ModelCategory) { return $category->getUrl(); } return $this->_categoryFactory->create()->setData($category->getData())->getUrl(); }
/** * Validate category process * * @param Category $category * @return void * @throws \Magento\Framework\Exception\LocalizedException */ protected function validateCategory(Category $category) { $useConfigFields = []; foreach ($this->useConfigFields as $field) { if (!$category->getData($field)) { $useConfigFields[] = $field; } } $category->setData('use_post_data_config', $useConfigFields); $validate = $category->validate(); if ($validate !== true) { foreach ($validate as $code => $error) { if ($error === true) { $attribute = $this->categoryResource->getAttribute($code)->getFrontend()->getLabel(); throw new \Magento\Framework\Exception\LocalizedException(__('Attribute "%1" is required.', $attribute)); } else { throw new \Magento\Framework\Exception\LocalizedException(__($error)); } } } $category->unsetData('use_post_data_config'); }
public function getObject(Category $category) { /** @var $productCollection Mage_Catalog_Model_Resource_Product_Collection */ $productCollection = $category->getProductCollection(); $productCollection = $productCollection->addMinimalPrice(); $category->setProductCount($productCollection->getSize()); $transport = new DataObject(); $this->eventManager->dispatch('algolia_category_index_before', ['category' => $category, 'custom_data' => $transport]); $customData = $transport->getData(); $storeId = $category->getStoreId(); $category->getUrlInstance()->setStore($storeId); $path = ''; foreach ($category->getPathIds() as $categoryId) { if ($path != '') { $path .= ' / '; } $path .= $this->getCategoryName($categoryId, $storeId); } $image_url = null; try { $image_url = $category->getImageUrl(); } catch (\Exception $e) { /* no image, no default: not fatal */ } $data = ['objectID' => $category->getId(), 'name' => $category->getName(), 'path' => $path, 'level' => $category->getLevel(), 'url' => $category->getUrl(), 'include_in_menu' => $category->getIncludeInMenu(), '_tags' => ['category'], 'popularity' => 1, 'product_count' => $category->getProductCount()]; if (!empty($image_url)) { $data['image_url'] = $image_url; } foreach ($this->config->getCategoryAdditionalAttributes($storeId) as $attribute) { $value = $category->getData($attribute['attribute']); $attribute_resource = $category->getResource()->getAttribute($attribute['attribute']); if ($attribute_resource) { $value = $attribute_resource->getFrontend()->getValue($category); } if (isset($data[$attribute['attribute']])) { $value = $data[$attribute['attribute']]; } if ($value) { $data[$attribute['attribute']] = $value; } } $data = array_merge($data, $customData); foreach ($data as &$data0) { $data0 = $this->try_cast($data0); } return $data; }
/** * Get url for category data * * @param Category $category * @return string */ public function getCategoryUrl($category) { if ($category instanceof Category) { $url = $category->getUrl(); } else { $url = $this->_categoryInstance->setData($category->getData())->getUrl(); } return $url; }
/** * Check EAV and flat data * * @param \Magento\Catalog\Model\Category $category */ protected function checkCategoryData(\Magento\Catalog\Model\Category $category) { foreach (self::$attributeCodes as $attributeCode) { $this->assertEquals(self::$attributeValues[$category->getId()][$attributeCode], $category->getData($attributeCode), "Data for {$category->getId()} attribute code [{$attributeCode}] is wrong"); } }
/** * @param Category $category * @return Metadata */ public function createDataFromModel(Category $category) { $builder = $this->builder->populateWithArray($category->getData())->setCategoryId($category->getId())->setActive($category->getIsActive())->setChildren($category->getAllChildren(true)); return $builder->create(); }
/** * {@inheritdoc} */ public function getData($key = '', $index = null) { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getData'); if (!$pluginInfo) { return parent::getData($key, $index); } else { return $this->___callPlugins('getData', func_get_args(), $pluginInfo); } }