/** * Apply category filter to layer * * @param \Zend_Controller_Request_Abstract $request * @return $this */ public function apply(\Zend_Controller_Request_Abstract $request) { $filter = (int) $request->getParam($this->getRequestVar()); if (!$filter) { return $this; } $this->_categoryId = $filter; $this->_coreRegistry->register('current_category_filter', $this->getCategory(), true); $this->_appliedCategory = $this->_categoryFactory->create()->setStoreId($this->_storeManager->getStore()->getId())->load($filter); if ($this->_isValidCategory($this->_appliedCategory)) { $this->getLayer()->getProductCollection()->addCategoryFilter($this->_appliedCategory); $this->getLayer()->getState()->addFilter($this->_createItem($this->_appliedCategory->getName(), $filter)); } return $this; }
/** * Generate category url key * * @param \Magento\Catalog\Model\Category $category * @return string */ public function generateUrlKey($category) { $urlKey = $category->getUrlKey(); return $category->formatUrlKey($urlKey === '' || $urlKey === null ? $category->getName() : $urlKey); }
/** * Get current category * * @param \Magento\Catalog\Model\Category $category * * @return Category */ public function isActive($category) { $activeCategory = $this->_coreRegistry->registry('current_category'); $activeProduct = $this->_coreRegistry->registry('current_product'); if (!$activeCategory) { // Check if we're on a product page if ($activeProduct !== null) { return in_array($category->getId(), $activeProduct->getCategoryIds()); } return false; } // Check if this is the active category if ($this->categoryFlatConfig->isFlatEnabled() && $category->getUseFlatResource() and $category->getId() == $activeCategory->getId()) { return true; } // Check if a subcategory of this category is active $childrenIds = $category->getAllChildren(true); if (!is_null($childrenIds) and in_array($activeCategory->getId(), $childrenIds)) { return true; } // Fallback - If Flat categories is not enabled the active category does not give an id return $category->getName() == $activeCategory->getName() ? true : false; }
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; }
public function testGetName() { $this->assertNull($this->_model->getName()); $this->_model->setData('name', 'test'); $this->assertEquals('test', $this->_model->getName()); }
public function getCategoryQueryTerm(Category $category, $store = null) { $queryType = $this->helper->getCategoryQueryType($store); if ($queryType == CategoryQueryType::NAME) { return $category->getName(); } $parents = $category->getParentCategories(); $parentIds = array_intersect($category->getParentIds(), array_keys($parents)); switch ($queryType) { case CategoryQueryType::FULL_PATH: break; case CategoryQueryType::NAME_AND_PARENT_NAME: $parentId = $category->getParentId(); $parentIds = in_array($parentId, $parentIds) ? [$parentId] : []; break; case CategoryQueryType::NAME_AND_ROOT_NAME: $parentIds = array_slice($parentIds, 0, 1); break; } $names = array_map(function ($id) use($parents) { return $parents[$id]->getName(); }, $parentIds); $names[] = $category->getName(); return implode(' ', $names); }
/** * 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; }
/** * {@inheritdoc} */ public function getName() { $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getName'); if (!$pluginInfo) { return parent::getName(); } else { return $this->___callPlugins('getName', func_get_args(), $pluginInfo); } }
/** * 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()]; }