Trong cửa hàng của bạn có lẽ sẽ có nhiều sản phẩm khác nhau và việc sắp xếp chúng đã được Magento 2 hỗ trợ. Trong bài viết này tôi sẽ hướng dẫn các bạn thêm custom sorting trong trang category.

Bước 1. Đăng ký module ViMagento_Sort
ViMagento/Sort/etc/module.xml
1 2 3 4 5 6 7 8 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="ViMagento_Sort" setup_version="1.0.0"> <sequence> <module name="Magento_Catalog"/> </sequence> </module> </config> |
ViMagento/Sort/registration.php
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'ViMagento_Sort', __DIR__ ); |
Sau đó các bạn chạy câu lệnh php bin/magento setup:upgrade để đăng ký module.
Bước 2: Tạo file di.xml
ViMagento/Sort/etc/frontend/di.xml
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Catalog\Block\Product\ProductList\Toolbar"> <plugin name="vimagento_custom_block_toolbar" type="ViMagento\Sort\Plugin\Catalog\Block\Toolbar"/> </type> <type name="Magento\Catalog\Model\Config"> <plugin name="vimagento_catalog_model_config" type="ViMagento\Sort\Plugin\Catalog\Model\Config"/> </type> </config> |
Bước 3: Tạo plugin
1 |
ViMagento/Sort/Plugin/Catalog/Model/Config.php |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php namespace ViMagento\Sort\Plugin\Catalog\Model; class Config { public function afterGetAttributeUsedForSortByArray( \Magento\Catalog\Model\Config $catalogConfig, $options ) { unset($options['price']); unset($options['name']); $options = []; $options['position'] = __('Độ ưu tiên'); $options['price_desc'] = __('Giá giảm dần'); $options['price_asc'] = __('Giá tăng dần'); $options['name_desc'] = __('Tên từ Z - A'); $options['name_asc'] = __('Tên từ A - Z'); return $options; } } |
1 |
ViMagento/Sort/Plugin/Catalog/Block/Toolbar.php |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php namespace ViMagento\Sort\Plugin\Catalog\Block; class Toolbar { public function aroundSetCollection( \Magento\Catalog\Block\Product\ProductList\Toolbar $subject, \Closure $proceed, $collection ) { $currentOrder = $subject->getCurrentOrder(); $result = $proceed($collection); if ($currentOrder) { if ($currentOrder == 'price_asc') { $subject->getCollection()->setOrder('price', 'asc'); } elseif ($currentOrder == 'name_asc') { $subject->getCollection()->setOrder('name', 'asc'); } elseif ($currentOrder == 'price_desc') { $subject->getCollection()->setOrder('price', 'desc'); } elseif ($currentOrder == 'name_desc') { $subject->getCollection()->setOrder('name', 'desc'); } }else{ $subject->getCollection()->setOrder('position', 'desc'); } return $result; } } |
Tiếp theo các bạn xóa cache của Magento bằng câu lệnh php bin/magento cache:clean để áp dụng thay đổi mà bạn vừa thực hiện.

Kết luận
Vừa rồi tôi đã hướng dẫn các bạn thêm custom sorting trong trang category. Hy vọng bài viết hữu ích và có thể giúp đỡ phần nào cho các bạn. Thanks!