Esempio n. 1
0
 /**
  * @param \Magento\Catalog\Model\Category $category
  * @param int $storeId
  * @return $this
  */
 public function getProductCollection(\Magento\Catalog\Model\Category $category, $storeId)
 {
     /** @var $layer \Magento\Catalog\Model\Layer */
     $layer = $this->catalogLayer->setStore($storeId);
     $collection = $category->getResourceCollection();
     $collection->addAttributeToSelect('url_key')->addAttributeToSelect('name')->addAttributeToSelect('is_anchor')->addAttributeToFilter('is_active', 1)->addIdFilter($category->getChildren())->load();
     /** @var $productCollection \Magento\Catalog\Model\ResourceModel\Product\Collection */
     $productCollection = $this->collectionFactory->create();
     $currentCategory = $layer->setCurrentCategory($category);
     $layer->prepareProductCollection($productCollection);
     $productCollection->addCountToCategories($collection);
     $category->getProductCollection()->setStoreId($storeId);
     $products = $currentCategory->getProductCollection()->addAttributeToSort('updated_at', 'desc')->setVisibility($this->visibility->getVisibleInCatalogIds())->setCurPage(1)->setPageSize(50);
     return $products;
 }
Esempio n. 2
0
 /**
  * Return child categories
  *
  * @param \Magento\Catalog\Model\Category $category
  * @return \Magento\Catalog\Model\Resource\Category\Collection
  */
 public function getChildrenCategories($category)
 {
     $collection = $category->getCollection();
     /* @var $collection \Magento\Catalog\Model\Resource\Category\Collection */
     $collection->addAttributeToSelect('url_key')->addAttributeToSelect('name')->addAttributeToSelect('all_children')->addAttributeToSelect('is_anchor')->addAttributeToFilter('is_active', 1)->addIdFilter($category->getChildren())->setOrder('position', \Magento\Framework\DB\Select::SQL_ASC)->joinUrlRewrite()->load();
     return $collection;
 }
Esempio n. 3
0
 /**
  * Render category to html
  *
  * @param Category $category
  * @param int $level Nesting level number
  * @param boolean $isLast Whether ot not this item is last, affects list item class
  * @param boolean $isFirst Whether ot not this item is first, affects list item class
  * @param boolean $isOutermost Whether ot not this item is outermost, affects list item class
  * @param string $outermostItemClass Extra class of outermost list items
  * @param string $childrenWrapClass If specified wraps children list in div with this class
  * @param boolean $noEventAttributes Whether ot not to add on* attributes to list item
  * @return string
  */
 protected function _renderCategoryMenuItemHtml($category, $level = 0, $isLast = false, $isFirst = false, $isOutermost = false, $outermostItemClass = '', $childrenWrapClass = '', $noEventAttributes = false)
 {
     if (!$category->getIsActive()) {
         return '';
     }
     // get all children
     if ($this->flatState->isAvailable()) {
         $children = (array) $category->getChildrenNodes();
     } else {
         $children = $category->getChildren();
     }
     // select active children
     $activeChildren = array();
     foreach ($children as $child) {
         if ($child->getIsActive()) {
             $activeChildren[] = $child;
         }
     }
     $activeChildrenCount = count($activeChildren);
     $hasActiveChildren = $activeChildrenCount > 0;
     // prepare list item html classes
     $classes = array();
     $classes[] = 'level' . $level;
     $classes[] = 'nav-' . $this->_getItemPosition($level);
     if ($this->isCategoryActive($category)) {
         $classes[] = 'active';
     }
     $linkClass = '';
     if ($isOutermost && $outermostItemClass) {
         $classes[] = $outermostItemClass;
         $linkClass = ' class="' . $outermostItemClass . '"';
     }
     if ($isFirst) {
         $classes[] = 'first';
     }
     if ($isLast) {
         $classes[] = 'last';
     }
     if ($hasActiveChildren) {
         $classes[] = 'parent';
     }
     // prepare list item attributes
     $attributes = array();
     if (count($classes) > 0) {
         $attributes['class'] = implode(' ', $classes);
     }
     if ($hasActiveChildren && !$noEventAttributes) {
         $attributes['onmouseover'] = 'toggleMenu(this,1)';
         $attributes['onmouseout'] = 'toggleMenu(this,0)';
     }
     // assemble list item with attributes
     $htmlLi = '<li';
     foreach ($attributes as $attrName => $attrValue) {
         $htmlLi .= ' ' . $attrName . '="' . str_replace('"', '\\"', $attrValue) . '"';
     }
     $htmlLi .= '>';
     $html = array();
     $html[] = $htmlLi;
     $html[] = '<a href="' . $this->getCategoryUrl($category) . '"' . $linkClass . '>';
     $html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
     $html[] = '</a>';
     // render children
     $htmlChildren = '';
     $j = 0;
     foreach ($activeChildren as $child) {
         $htmlChildren .= $this->_renderCategoryMenuItemHtml($child, $level + 1, $j == $activeChildrenCount - 1, $j == 0, false, $outermostItemClass, $childrenWrapClass, $noEventAttributes);
         $j++;
     }
     if (!empty($htmlChildren)) {
         if ($childrenWrapClass) {
             $html[] = '<div class="' . $childrenWrapClass . '">';
         }
         $html[] = '<ul class="level' . $level . '">';
         $html[] = $htmlChildren;
         $html[] = '</ul>';
         if ($childrenWrapClass) {
             $html[] = '</div>';
         }
     }
     $html[] = '</li>';
     $html = implode("\n", $html);
     return $html;
 }
 public function testGetChildren()
 {
     $this->_model->load(3);
     $this->assertEquals('4', $this->_model->getChildren());
 }
Esempio n. 5
0
 /**
  * {@inheritdoc}
  */
 public function getChildren()
 {
     $pluginInfo = $this->pluginList->getNext($this->subjectType, 'getChildren');
     if (!$pluginInfo) {
         return parent::getChildren();
     } else {
         return $this->___callPlugins('getChildren', func_get_args(), $pluginInfo);
     }
 }