0

I want to show specific brands products on the top of the category page. How can I do this in magento 2?

Suppose, I have 5 options in the manufacturer attribute: apple, dell, lg, samsung, htc. Random products are showing on the listing page. But, I want to display dell products first on the listing page.

  • Have you added brand attribute to product or you mean manufacturer? – Stepan Furman Mar 3 '18 at 7:22
  • yes, I have manufacturer attribute. – Vinod Kumar Mar 3 '18 at 8:01
0

The way I would do it is, I would get a hold of your product collection before it is rendered, this can be in the product list block but the collection has to be loaded. Get it as an array as so : $items = $collection->getItems() the pass that through a custom sorting function which brings the dell products to the top, like so :

usort($items, function ($a, $b) {
    if ($a->getBrand() == 'dell') return -1;
    if ($b->getBrand() == 'dell') return 1;
    return 0;
});

Then in the template loop through that array as normal and display the products. Here is an example of the sorting algorithm working : https://3v4l.org/qTRGm

Note: I am using $a->getBrand() in the example but you might need to call something else depending on your attribute´s name, but the point is to get the brand name.

0

For doing this you can override of pluginizate \Magento\Catalog\Model\ResourceModel\Product\Collection::addAttributeToSort function adding something like this:

if ($attribute == 'manufacturer') {
    $this->getSelect()->order(new \Zend_Db_Expr("(`manufacturer` = 'dell') DESC"));
}

I have added this expression to the core file just for testing, that it works. Please don't do like this in your project:

    public function addAttributeToSort($attribute, $dir = self::SORT_ORDER_ASC)
{
    if ($attribute == 'position') {
        if (isset($this->_joinFields[$attribute])) {
            $this->getSelect()->order($this->_getAttributeFieldName($attribute) . ' ' . $dir);
            return $this;
        }
        if ($this->isEnabledFlat()) {
            $this->getSelect()->order("cat_index_position {$dir}");
        }
        // optimize if using cat index
        $filters = $this->_productLimitationFilters;
        if (isset($filters['category_id']) || isset($filters['visibility'])) {
            $this->getSelect()->order('cat_index.position ' . $dir);
        } else {
            $this->getSelect()->order('e.entity_id ' . $dir);
        }

        return $this;
    } elseif ($attribute == 'is_saleable') {
        $this->getSelect()->order("is_saleable " . $dir);
        return $this;
    } elseif ($attribute == 'manufacturer') {
// here is custom code
        $this->getSelect()->order(new \Zend_Db_Expr("(`manufacturer` = 'dell') DESC"));
    }

    $storeId = $this->getStoreId();
    if ($attribute == 'price' && $storeId != 0) {
        $this->addPriceData();
        if ($this->_productLimitationFilters->isUsingPriceIndex()) {
            $this->getSelect()->order("price_index.min_price {$dir}");
            return $this;
        }
    }

    if ($this->isEnabledFlat()) {
        $column = $this->getEntity()->getAttributeSortColumn($attribute);

        if ($column) {
            $this->getSelect()->order("e.{$column} {$dir}");
        } elseif (isset($this->_joinFields[$attribute])) {
            $this->getSelect()->order($this->_getAttributeFieldName($attribute) . ' ' . $dir);
        }

        return $this;
    } else {
        $attrInstance = $this->getEntity()->getAttribute($attribute);
        if ($attrInstance && $attrInstance->usesSource()) {
            $attrInstance->getSource()->addValueSortToCollection($this, $dir);
            return $this;
        }
    }

    return parent::addAttributeToSort($attribute, $dir);
}

Hope it will help.

0
  1. Create an attribute as text field for the specific brand and assign to the attribute set.

  2. Add numbers to this field for sorting the products.

  3. Sort product collection.

    $collection->joinField('stk', 'cataloginventory_stock_item', 'qty', 'product_id=entity_id', 'qty>=0', 'left')->setOrder('brand_sort_order', 'DESC')->setOrder('stk','desc');

Note: Here we have sorted products according to the brand and stock quantity. You can modify sorting according to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.