Create Bestseller sorting for Magento2

Here's a more detailed explanation of the steps involved in creating a bestseller sorting plugin in Magento 2:

Step 1: Create a Custom Module Create the necessary files and directories for your module:

  1. Create the directory structure: app/code/CodesBug/BestsellerSorting.

  2. Create the registration file (registration.php) in the module directory (app/code/CodesBug/BestsellerSorting):


<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'CodesBug_BestsellerSorting', __DIR__ );
  1. Create the module configuration file (etc/module.xml) in the module directory (app/code/CodesBug/BestsellerSorting/etc):

<?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="CodesBug_BestsellerSorting" setup_version="1.0.0">
<sequence>
<module name="Magento_Catalog"/>
</sequence>
</module>
</config>

  1. Create the module declaration file (etc/di.xml) in the module directory (app/code/CodesBug/BestsellerSorting/etc):

<?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\ListProduct"> <plugin name="codesbug_bestseller_sort_plugin" type="CodesBug\BestsellerSorting\Plugin\Catalog\Product\ProductList\BestsellerSort" sortOrder="10"/> </type> </config>

Step 2: Create the Plugin Class Create the plugin class that will modify the sorting behavior:

  1. Create the plugin class file (BestsellerSort.php) in the directory app/code/CodesBug/BestsellerSorting/Plugin/Catalog/Product/ProductList:

<?php
namespace CodesBug\BestsellerSorting\Plugin\Catalog\Product\ProductList;
{
protected $_productCollectionFactory;
public function __construct( \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory )
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
)
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
) {
$this->_productCollectionFactory = $productCollectionFactory;
}
public function aroundGetProductCollection( \Magento\Catalog\Block\Product\ListProduct $subject, \Closure $proceed )
\Magento\Catalog\Block\Product\ListProduct $subject,
\Closure $proceed
)
\Magento\Catalog\Block\Product\ListProduct $subject,
\Closure $proceed
) {
$result = $proceed();
$result->addAttributeToSelect('*');
$result->setOrder('sales_count', 'desc'); // Sort by sales count in descending order
return $result;
}
}

class BestsellerSort

Step 3: Test and Deploy Enable the module in your Magento 2 instance and test the bestseller sorting functionality:

  1. Run the following command in the command line to enable the module:

bin/magento module:enable CodesBug_BestsellerSorting
  1. Run the following command to update the database schema:

bin/magento setup:upgrade
  1. Flush the cache:

bin/magento cache:flush

After following these steps, the getProductCollection method of the ListProduct block will be intercepted by the plugin, and the product collection will be sorted based on the sales count attribute in descending order.

Please make sure to adjust the code as needed based on your specific requirements and Magento 2 version.

Comments