/** * @return $this */ protected function _prepareLayout() { parent::_prepareLayout(); $this->getLayout()->createBlock('Magento\\Catalog\\Block\\Breadcrumbs'); $category = $this->getCurrentCategory(); if ($category) { $title = $category->getMetaTitle(); if ($title) { $this->pageConfig->getTitle()->set($title); } $description = $category->getMetaDescription(); if ($description) { $this->pageConfig->setDescription($description); } $keywords = $category->getMetaKeywords(); if ($keywords) { $this->pageConfig->setKeywords($keywords); } if ($this->_categoryHelper->canUseCanonicalTag()) { $this->pageConfig->addRemotePageAsset($category->getUrl(), 'canonical', ['attributes' => ['rel' => 'canonical']]); } $pageMainTitle = $this->getLayout()->getBlock('page.main.title'); if ($pageMainTitle) { $pageMainTitle->setPageTitle($this->getCurrentCategory()->getName()); } } return $this; }
public function testGetMenuCategoryData() { $category = $this->getMock('Magento\\Catalog\\Model\\Category', ['getId', 'getName'], [], '', false); $category->expects($this->once())->method('getId')->willReturn('id'); $category->expects($this->once())->method('getName')->willReturn('name'); $this->_catalogCategory->expects($this->once())->method('getCategoryUrl')->willReturn('url'); $this->assertEquals(['name' => 'name', 'id' => 'category-node-id', 'url' => 'url', 'is_active' => false, 'has_active' => false], $this->_observer->getMenuCategoryData($category)); }
/** * Get category data to be added to the Menu * * @param \Magento\Framework\Data\Tree\Node $category * @return array */ public function getMenuCategoryData($category) { $nodeId = 'category-node-' . $category->getId(); $isActiveCategory = false; /** @var \Magento\Catalog\Model\Category $currentCategory */ $currentCategory = $this->registry->registry('current_category'); if ($currentCategory && $currentCategory->getId() == $category->getId()) { $isActiveCategory = true; } $categoryData = ['name' => $category->getName(), 'id' => $nodeId, 'url' => $this->catalogCategory->getCategoryUrl($category), 'has_active' => $this->hasActive($category), 'is_active' => $isActiveCategory]; return $categoryData; }
public function getConfig($key, $default = '') { $widget_key = explode('/', $key); if (count($widget_key) == 2 && ($resultData = $this->hasData($widget_key[1]))) { return $this->getData($widget_key[1]); } $result = $this->_brandHelper->getConfig($key); if ($result == "") { return $default; } return $result; }
/** * Recursively adds categories to top menu * * @param \Magento\Framework\Data\Tree\Node\Collection|array $categories * @param \Magento\Framework\Data\Tree\Node $parentCategoryNode * @param \Magento\Theme\Block\Html\Topmenu $block * @return void */ protected function _addCategoriesToMenu($categories, $parentCategoryNode, $block) { foreach ($categories as $category) { if (!$category->getIsActive()) { continue; } $nodeId = 'category-node-' . $category->getId(); $block->addIdentity(\Magento\Catalog\Model\Category::CACHE_TAG . '_' . $category->getId()); $tree = $parentCategoryNode->getTree(); $isActiveCategory = false; /** @var \Magento\Catalog\Model\Category $currentCategory */ $currentCategory = $this->_registry->registry('current_category'); if ($currentCategory && $currentCategory->getId() == $category->getId()) { $isActiveCategory = true; } $categoryData = ['name' => $category->getName(), 'id' => $nodeId, 'url' => $this->_catalogCategory->getCategoryUrl($category), 'has_active' => $this->hasActive($category), 'is_active' => $isActiveCategory]; $categoryNode = new \Magento\Framework\Data\Tree\Node($categoryData, 'id', $tree, $parentCategoryNode); $parentCategoryNode->addChild($categoryNode); if ($this->categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource()) { $subcategories = (array) $category->getChildrenNodes(); } else { $subcategories = $category->getChildren(); } $this->_addCategoriesToMenu($subcategories, $categoryNode, $block); } }
/** * Retrieve Visitor/Customer Last Viewed URL * * @return string */ public function getLastViewedUrl() { $productId = $this->_catalogSession->getLastViewedProductId(); if ($productId) { try { $product = $this->productRepository->getById($productId); } catch (NoSuchEntityException $e) { return ''; } /* @var $product \Magento\Catalog\Model\Product */ if ($this->_catalogProduct->canShow($product, 'catalog')) { return $product->getProductUrl(); } } $categoryId = $this->_catalogSession->getLastViewedCategoryId(); if ($categoryId) { try { $category = $this->categoryRepository->get($categoryId); } catch (NoSuchEntityException $e) { return ''; } /* @var $category \Magento\Catalog\Model\Category */ if (!$this->_catalogCategory->canShow($category)) { return ''; } return $category->getCategoryUrl(); } return ''; }
public function _toHtml() { if (!$this->_brandHelper->getConfig('general_settings/enable')) { return; } $carousel_layout = $this->getConfig('carousel_layout'); if ($carousel_layout == 'owl_carousel') { $this->setTemplate('widget/brand_list_owl.phtml'); } else { $this->setTemplate('widget/brand_list_bootstrap.phtml'); } if (($template = $this->getConfig('template')) != '') { $this->setTemplate($template); } return parent::_toHtml(); }
/** * Prepare breadcrumbs * * @param \Magento\Cms\Model\Page $brand * @throws \Magento\Framework\Exception\LocalizedException * @return void */ protected function _addBreadcrumbs() { $breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs'); $baseUrl = $this->_storeManager->getStore()->getBaseUrl(); $brandRoute = $this->_brandHelper->getConfig('general_settings/route'); $page_title = $this->_brandHelper->getConfig('brand_list_page/page_title'); $brand = $this->getCurrentBrand(); $group = ''; if ($groupId = $brand->getGroupId()) { $group = $this->_groupModel->load($groupId); } $breadcrumbsBlock->addCrumb('home', ['label' => __('Home'), 'title' => __('Go to Home Page'), 'link' => $baseUrl]); $breadcrumbsBlock->addCrumb('vesbrand', ['label' => $page_title, 'title' => $page_title, 'link' => $baseUrl . $brandRoute]); if ($group && $group->getStatus()) { $breadcrumbsBlock->addCrumb('group', ['label' => $group->getName(), 'title' => $group->getName(), 'link' => $group->getUrl()]); } $breadcrumbsBlock->addCrumb('brand', ['label' => $brand->getName(), 'title' => $brand->getName(), 'link' => '']); }
protected function _preparationData() { $this->_childrenCategory = $this->getMock('\\Magento\\Catalog\\Model\\Category', ['getIsActive', '__wakeup'], [], '', false); $this->_childrenCategory->expects($this->once())->method('getIsActive')->will($this->returnValue(false)); $this->_category = $this->getMock('\\Magento\\Catalog\\Model\\Category', ['getIsActive', '__wakeup', 'getName', 'getChildren', 'getUseFlatResource', 'getChildrenNodes'], [], '', false); $this->_category->expects($this->once())->method('getIsActive')->will($this->returnValue(true)); $this->_catalogCategory->expects($this->once())->method('getStoreCategories')->will($this->returnValue([$this->_category])); $this->menuCategoryData->expects($this->once())->method('getMenuCategoryData')->with($this->_category); $blockMock = $this->_getCleanMock('\\Magento\\Theme\\Block\\Html\\Topmenu'); $treeMock = $this->_getCleanMock('\\Magento\\Framework\\Data\\Tree'); $menuMock = $this->getMock('\\Magento\\Framework\\Data\\Tree\\Node', ['getTree', 'addChild'], [], '', false); $menuMock->expects($this->once())->method('getTree')->will($this->returnValue($treeMock)); $eventMock = $this->getMock('\\Magento\\Framework\\Event', ['getBlock'], [], '', false); $eventMock->expects($this->once())->method('getBlock')->will($this->returnValue($blockMock)); $observerMock = $this->getMock('\\Magento\\Framework\\Event\\Observer', ['getEvent', 'getMenu'], [], '', false); $observerMock->expects($this->once())->method('getEvent')->will($this->returnValue($eventMock)); $observerMock->expects($this->once())->method('getMenu')->will($this->returnValue($menuMock)); return $observerMock; }
/** * Retrieve Product Url path (with category if exists) * * @param \Magento\Catalog\Model\Product $product * @param \Magento\Catalog\Model\Category $category * * @return string * @throws \Magento\Framework\Model\Exception */ public function getUrlPath($product, $category = null) { $path = $product->getData('url_path'); if (is_null($category)) { /** @todo get default category */ return $path; } elseif (!$category instanceof \Magento\Catalog\Model\Category) { throw new \Magento\Framework\Model\Exception('Invalid category object supplied'); } return $this->_catalogCategory->getCategoryUrlPath($category->getUrlPath()) . '/' . $path; }
public function getUrl() { $url = $this->_storeManager->getStore()->getBaseUrl(); $route = $this->_brandHelper->getConfig('general_settings/route'); $url_prefix = $this->_brandHelper->getConfig('general_settings/url_prefix'); $urlPrefix = ''; if ($url_prefix) { $urlPrefix = $url_prefix . '/'; } $url_suffix = $this->_brandHelper->getConfig('general_settings/url_suffix'); return $url . $urlPrefix . $this->getUrlKey() . $url_suffix; }
/** * @expectedException \Magento\Framework\Model\Exception * @expectedExceptionMessage Invalid category object supplied */ public function testGetUrlPath() { $urlPathProduct = '/some/url/path'; $urlPathCategory = '/some/url/path/category'; $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['getData', '__wakeup'])->getMock(); $product->expects($this->atLeastOnce())->method('getData')->with('url_path')->will($this->returnValue($urlPathProduct)); $category = $this->getMockBuilder('Magento\\Catalog\\Model\\Category')->disableOriginalConstructor()->setMethods(['getUrlPath', '__wakeup'])->getMock(); $category->expects($this->atLeastOnce())->method('getUrlPath')->will($this->returnValue($urlPathCategory)); $this->catalogCategory->expects($this->atLeastOnce())->method('getCategoryUrlPath')->with($urlPathCategory)->will($this->returnValue($urlPathCategory)); $this->assertEquals($urlPathProduct, $this->model->getUrlPath($product)); $this->assertEquals($urlPathCategory . '/' . $urlPathProduct, $this->model->getUrlPath($product, $category)); $this->model->getUrlPath($product, 1); }
/** * Recursively adds categories to top menu * * @param \Magento\Framework\Data\Tree\Node\Collection|array $categories * @param \Magento\Framework\Data\Tree\Node $parentCategoryNode * @param \Magento\Theme\Block\Html\Topmenu $block * @return void */ protected function _addCategoriesToMenu($categories, $parentCategoryNode, $block) { foreach ($categories as $category) { if (!$category->getIsActive()) { continue; } $nodeId = 'category-node-' . $category->getId(); $block->addIdentity(\Magento\Catalog\Model\Category::CACHE_TAG . '_' . $category->getId()); $tree = $parentCategoryNode->getTree(); $categoryData = array('name' => $category->getName(), 'id' => $nodeId, 'url' => $this->_catalogCategory->getCategoryUrl($category), 'is_active' => $this->_isActiveMenuCategory($category)); $categoryNode = new \Magento\Framework\Data\Tree\Node($categoryData, 'id', $tree, $parentCategoryNode); $parentCategoryNode->addChild($categoryNode); if ($this->categoryFlatConfig->isFlatEnabled()) { $subcategories = (array) $category->getChildrenNodes(); } else { $subcategories = $category->getChildren(); } $this->_addCategoriesToMenu($subcategories, $categoryNode, $block); } }
/** * @return $this */ protected function _prepareLayout() { parent::_prepareLayout(); $this->getLayout()->createBlock('Magento\\Catalog\\Block\\Breadcrumbs'); $headBlock = $this->getLayout()->getBlock('head'); $category = $this->getCurrentCategory(); if ($headBlock && $category) { $title = $category->getMetaTitle(); if ($title) { $headBlock->setTitle($title); } $description = $category->getMetaDescription(); if ($description) { $headBlock->setDescription($description); } $keywords = $category->getMetaKeywords(); if ($keywords) { $headBlock->setKeywords($keywords); } //@todo: move canonical link to separate block if ($this->_categoryHelper->canUseCanonicalTag() && !$headBlock->getChildBlock('magento-page-head-category-canonical-link')) { $headBlock->addChild('magento-page-head-category-canonical-link', 'Magento\\Theme\\Block\\Html\\Head\\Link', array('url' => $category->getUrl(), 'properties' => array('attributes' => array('rel' => 'canonical')))); } /** * want to show rss feed in the url */ if ($this->isRssCatalogEnable() && $this->isTopCategory()) { $title = __('%1 RSS Feed', $this->getCurrentCategory()->getName()); $headBlock->addRss($title, $this->getRssLink()); } $pageMainTitle = $this->getLayout()->getBlock('page.main.title'); if ($pageMainTitle) { $pageMainTitle->setPageTitle($this->getCurrentCategory()->getName()); } } return $this; }
/** * Checking whether the using static urls in WYSIWYG allowed event * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function execute(\Magento\Framework\Event\Observer $observer) { $block = $observer->getEvent()->getBlock(); $block->addIdentity(\Magento\Catalog\Model\Category::CACHE_TAG); $this->_addCategoriesToMenu($this->catalogCategory->getStoreCategories(), $observer->getMenu(), $block); }
/** * @return array */ protected function _getItemsData() { $this->_requestVar = str_replace('bx_products_', '', $this->bxFacets->getFacetParameterName($this->fieldName)); if (!$this->bxDataHelper->isHierarchical($this->fieldName)) { $attributeModel = $this->_config->getAttribute('catalog_product', substr($this->fieldName, 9))->getSource(); $order = $this->bxDataHelper->getFieldSortOrder($this->fieldName); if ($order == 2) { $values = $attributeModel->getAllOptions(); $responseValues = $this->bxDataHelper->useValuesAsKeys($this->bxFacets->getFacetValues($this->fieldName)); $selectedValues = $this->bxDataHelper->useValuesAsKeys($this->bxFacets->getSelectedValues($this->fieldName)); foreach ($values as $value) { $label = is_array($value) ? $value['label'] : $value; if (isset($responseValues[$label])) { $facetValue = $responseValues[$label]; $selected = isset($selectedValues[$facetValue]) ? true : false; $paramValue = $this->is_bx_attribute ? $this->bxFacets->getFacetValueParameterValue($this->fieldName, $facetValue) : $attributeModel->getOptionId($this->bxFacets->getFacetValueParameterValue($this->fieldName, $facetValue)); $this->itemDataBuilder->addItemData($this->tagFilter->filter($this->bxFacets->getFacetValueLabel($this->fieldName, $facetValue)), $selected ? 0 : $paramValue, $this->bxFacets->getFacetValueCount($this->fieldName, $facetValue), $selected, 'flat'); } } } else { $selectedValues = $this->bxDataHelper->useValuesAsKeys($this->bxFacets->getSelectedValues($this->fieldName)); $responseValues = $this->bxFacets->getFacetValues($this->fieldName); foreach ($responseValues as $facetValue) { $selected = isset($selectedValues[$facetValue]) ? true : false; $paramValue = $this->is_bx_attribute ? $this->bxFacets->getFacetValueParameterValue($this->fieldName, $facetValue) : $attributeModel->getOptionId($this->bxFacets->getFacetValueParameterValue($this->fieldName, $facetValue)); $this->itemDataBuilder->addItemData($this->tagFilter->filter($this->bxFacets->getFacetValueLabel($this->fieldName, $facetValue)), $selected ? 0 : $paramValue, $this->bxFacets->getFacetValueCount($this->fieldName, $facetValue), $selected, 'flat'); } } } else { $count = 1; $facetValues = array(); $parentCategories = $this->bxFacets->getParentCategories(); $parentCount = count($parentCategories); $value = false; foreach ($parentCategories as $key => $parentCategory) { if ($count == 1) { $count++; $homeLabel = __("All Categories"); $this->itemDataBuilder->addItemData($this->tagFilter->filter($homeLabel), 2, $this->bxFacets->getParentCategoriesHitCount($key), $value, 'home parent'); continue; } if ($parentCount == $count++) { $value = true; } $this->itemDataBuilder->addItemData($this->tagFilter->filter($parentCategory), $key, $this->bxFacets->getParentCategoriesHitCount($key), $value, 'parent'); } $sortOrder = $this->bxDataHelper->getFieldSortOrder($this->fieldName); if ($sortOrder == 2) { $facetLabels = $this->bxFacets->getCategoriesKeyLabels(); $childId = explode('/', end($facetLabels))[0]; $childParentId = $this->categoryFactory->create()->load($childId)->getParentId(); end($parentCategories); $parentId = key($parentCategories); $id = $parentId == null ? 2 : ($parentId == $childParentId ? $parentId : $childParentId); $cat = $this->categoryFactory->create()->load($id); foreach ($cat->getChildrenCategories() as $category) { if (isset($facetLabels[$category->getName()])) { $facetValues[] = $facetLabels[$category->getName()]; } } } if ($facetValues == null) { $facetValues = $this->bxFacets->getCategories(); } foreach ($facetValues as $facetValue) { $id = $this->bxFacets->getFacetValueParameterValue($this->fieldName, $facetValue); if ($sortOrder == 2 || $this->categoryHelper->canShow($this->categoryFactory->create()->load($id))) { $this->itemDataBuilder->addItemData($this->tagFilter->filter($this->bxFacets->getFacetValueLabel($this->fieldName, $facetValue)), $id, $this->bxFacets->getFacetValueCount($this->fieldName, $facetValue), false, $value ? 'children' : 'home'); } } } return $this->itemDataBuilder->build(); }
/** * Convert category to array * * @param \Magento\Catalog\Model\Category $category * @param \Magento\Catalog\Model\Category $currentCategory * @return array */ private function getCategoryAsArray($category, $currentCategory) { return ['name' => $category->getName(), 'id' => 'category-node-' . $category->getId(), 'url' => $this->catalogCategory->getCategoryUrl($category), 'has_active' => in_array((string) $category->getId(), explode('/', $currentCategory->getPath()), true), 'is_active' => $category->getId() == $currentCategory->getId()]; }
/** * Generate category url key path * * @param \Magento\Catalog\Model\Category $category * @return string */ public function generateCategoryUrlKeyPath($category) { $parentPath = $this->categoryHelper->getCategoryUrlPath('', true, $category->getStoreId()); $urlKey = $category->getUrlKey() == '' ? $category->formatUrlKey($category->getName()) : $category->formatUrlKey($category->getUrlKey()); return $parentPath . $urlKey; }
/** * Get catagories of current store * * @return \Magento\Framework\Data\Tree\Node\Collection */ public function getStoreCategories() { return $this->_catalogCategory->getStoreCategories(); }
/** * Return Category Id for $category object * * @param $category * * @return string */ public function getCategoryUrl($category) { return $this->_categoryHelper->getCategoryUrl($category); }
/** * Retrieve Visitor/Customer Last Viewed URL * * @return string */ public function getLastViewedUrl() { $productId = $this->_catalogSession->getLastViewedProductId(); if ($productId) { $product = $this->_productFactory->create()->load($productId); /* @var $product \Magento\Catalog\Model\Product */ if ($this->_catalogProduct->canShow($product, 'catalog')) { return $product->getProductUrl(); } } $categoryId = $this->_catalogSession->getLastViewedCategoryId(); if ($categoryId) { $category = $this->_categoryFactory->create()->load($categoryId); /* @var $category \Magento\Catalog\Model\Category */ if (!$this->_catalogCategory->canShow($category)) { return ''; } return $category->getCategoryUrl(); } return ''; }
/** * @magentoConfigFixture current_store catalog/seo/category_canonical_tag 1 */ public function testCanUseCanonicalTag() { $this->assertEquals(1, $this->_helper->canUseCanonicalTag()); }
/** * Generate either id path, request path or target path for product and/or category * * For generating id or system path, either product or category is required * For generating request path - category is required * $parentPath used only for generating category path * * @param string $type * @param \Magento\Framework\Object $product * @param \Magento\Framework\Object $category * @param string $parentPath * @return string * @throws \Magento\Framework\Model\Exception */ public function generatePath($type = 'target', $product = null, $category = null, $parentPath = null) { if (!$product && !$category) { throw new \Magento\Framework\Model\Exception(__('Please specify either a category or a product, or both.')); } // generate id_path if ('id' === $type) { if (!$product) { return 'category/' . $category->getId(); } if ($category && $category->getLevel() > 1) { return 'product/' . $product->getId() . '/' . $category->getId(); } return 'product/' . $product->getId(); } // generate request_path if ('request' === $type) { // for category if (!$product) { if ($category->getUrlKey() == '') { $urlKey = $this->getCategoryModel()->formatUrlKey($category->getName()); } else { $urlKey = $this->getCategoryModel()->formatUrlKey($category->getUrlKey()); } $categoryUrlSuffix = $this->getCategoryUrlSuffix($category->getStoreId()); if (null === $parentPath) { $parentPath = $this->getResource()->getCategoryParentPath($category); } elseif ($parentPath == '/') { $parentPath = ''; } $parentPath = $this->_catalogCategory->getCategoryUrlPath($parentPath, true, $category->getStoreId()); return $this->getUnusedPath($category->getStoreId(), $parentPath . $urlKey . $categoryUrlSuffix, $this->generatePath('id', null, $category), $urlKey); } // for product & category if (!$category) { throw new \Magento\Framework\Model\Exception(__('A category object is required for determining the product request path.')); } if ($product->getUrlKey() == '') { $urlKey = $this->productUrl->formatUrlKey($product->getName()); } else { $urlKey = $this->productUrl->formatUrlKey($product->getUrlKey()); } $productUrlSuffix = $this->getProductUrlSuffix($category->getStoreId()); if ($category->getLevel() > 1) { // To ensure, that category has url path either from attribute or generated now $this->_addCategoryUrlPath($category); $categoryUrl = $this->_catalogCategory->getCategoryUrlPath($category->getUrlPath(), false, $category->getStoreId()); return $this->getUnusedPath($category->getStoreId(), $categoryUrl . '/' . $urlKey . $productUrlSuffix, $this->generatePath('id', $product, $category), $urlKey); } // for product only return $this->getUnusedPath($category->getStoreId(), $urlKey . $productUrlSuffix, $this->generatePath('id', $product), $urlKey); } // generate target_path if (!$product) { return 'catalog/category/view/id/' . $category->getId(); } if ($category && $category->getLevel() > 1) { return 'catalog/product/view/id/' . $product->getId() . '/category/' . $category->getId(); } return 'catalog/product/view/id/' . $product->getId(); }