Hello all. I'm rather frustrated - and feeling more than a little stupid - that I can't figure this out. I'm using Magento 2.3.3, and I'm making use of the Catalog Products List widget in some CMS static blocks. By default, the products are sorted by (I believe) the creation date, in descending order. After lots of Google searching and trying and failing with many other methods to change the default sort order, I discovered the following code snippet in vendor/magento/module-catalog-widget/Block/Product/ProductsList.php:
$collection = $this->_addProductAttributesAndPrices($collection) ->addStoreFilter() ->addAttributeToSort('created_at', 'desc') ->setPageSize($this->getPageSize()) ->setCurPage($this->getRequest()->getParam($this->getData('page_var_name'), 1));
I changed "created_at" to "name" and "desc" to "asc". It worked (yay!), but I had to temporarily overwrite the original file (a no-no, I realize, but again it's temporary), because I can't for the life of me figure out where to put the modified file to overwrite it. I tried the following, and none of them is the correct location apparently:
Seems like this shouldn't be this hard...
Thanks in advance for any help!
Solved! Go to Solution.
Hi @phil_geyer ,
You should create an extension to achieve this.
Lets assume following parameters.
Vendor Name: Phil
Extension Name: Widget
Create following files.
app/code/Phil/Widget/registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Phil_Widget', __DIR__ );
app/code/Phil/Widget/etc/module.xml
<?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="Phil_Widget" setup_version="1.0.0" /> </config>
app/code/Phil/Widget/etc/frontend/di.xml
<?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\CatalogWidget\Block\Product\ProductsList"> <plugin name="catalog_product_list_widget_name_sortorder" type="Phil\Widget\Plugin\CatalogProductList" /> </type> </config>
app/code/Phil/Widget/Plugin/CatalogProductList.php
<?php namespace Phil\Widget\Plugin; class CatalogProductList { public function afterCreateCollection($subject, $result) { $result->getSelect()->reset(\Zend_Db_Select::ORDER); $result->addAttributeToSort('name', 'asc'); return $result; } }
Then run following commands.
rm -rf generated/* php bin/magento setup:upgrade php bin/magento setup:di:compile php bin/magento setup:static-content:deploy -f php bin/magento cache:flush
After this, as long as your module is enabled, this will sort all product list widget with name asc order.
Try this and accept it as a solution with giving kudos if it works for you.
Hi @phil_geyer ,
You should create an extension to achieve this.
Lets assume following parameters.
Vendor Name: Phil
Extension Name: Widget
Create following files.
app/code/Phil/Widget/registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Phil_Widget', __DIR__ );
app/code/Phil/Widget/etc/module.xml
<?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="Phil_Widget" setup_version="1.0.0" /> </config>
app/code/Phil/Widget/etc/frontend/di.xml
<?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\CatalogWidget\Block\Product\ProductsList"> <plugin name="catalog_product_list_widget_name_sortorder" type="Phil\Widget\Plugin\CatalogProductList" /> </type> </config>
app/code/Phil/Widget/Plugin/CatalogProductList.php
<?php namespace Phil\Widget\Plugin; class CatalogProductList { public function afterCreateCollection($subject, $result) { $result->getSelect()->reset(\Zend_Db_Select::ORDER); $result->addAttributeToSort('name', 'asc'); return $result; } }
Then run following commands.
rm -rf generated/* php bin/magento setup:upgrade php bin/magento setup:di:compile php bin/magento setup:static-content:deploy -f php bin/magento cache:flush
After this, as long as your module is enabled, this will sort all product list widget with name asc order.
Try this and accept it as a solution with giving kudos if it works for you.
Create around plugin to change createCollelction().
`di.xml`
<?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\CatalogWidget\Block\Product\ProductsList"> <plugin name="around_catalog_product_list_widget_name_sortorder" type="vendorName\moduleName\Plugin\CatalogProductList" /> </type> </config>
`CatalogProductList.php`
<?php
// app/code/vendorName/moduleName namespace vendor\moduleName\Plugin; class CatalogProductList { public function aroundSave(\Magento\CatalogWidget\Block\Product\ProductsList $subject, callable $proceed) {
/** @var $collection \Magento\Catalog\Model\ResourceModel\Product\Collection */
$collection = $this->productCollectionFactory->create();
if ($this->getData('store_id') !== null) {
$collection->setStoreId($this->getData('store_id'));
}
$collection->setVisibility($this->catalogProductVisibility->getVisibleInCatalogIds());
$collection = $this->_addProductAttributesAndPrices($collection)
->addStoreFilter()
->addAttributeToSort('name', 'desc')
->setPageSize($this->getPageSize())
->setCurPage($this->getRequest()->getParam($this->getData('page_var_name'), 1));
$conditions = $this->getConditions();
$conditions->collectValidatedAttributes($collection);
$this->sqlBuilder->attachConditionToCollection($collection, $conditions);
/**
* Prevent retrieval of duplicate records. This may occur when multiselect product attribute matches
* several allowed values from condition simultaneously
*/
$collection->distinct(true);
return $collection;
}
}
@yash7690 wrote:Hi @phil_geyer ,
You should create an extension to achieve this.
Lets assume following parameters.
Vendor Name: Phil
Extension Name: Widget
Create following files.
app/code/Phil/Widget/registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Phil_Widget', __DIR__ );app/code/Phil/Widget/etc/module.xml
<?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="Phil_Widget" setup_version="1.0.0" /> </config>app/code/Phil/Widget/etc/frontend/di.xml
<?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\CatalogWidget\Block\Product\ProductsList"> <plugin name="catalog_product_list_widget_name_sortorder" type="Phil\Widget\Plugin\CatalogProductList" /> </type> </config>app/code/Phil/Widget/Plugin/CatalogProductList.php
<?php namespace Phil\Widget\Plugin; class CatalogProductList { public function afterCreateCollection($subject, $result) { $result->getSelect()->reset(\Zend_Db_Select::ORDER); $result->addAttributeToSort('name', 'asc'); return $result; } }Then run following commands.
rm -rf generated/* php bin/magento setup:upgrade php bin/magento setup:di:compile php bin/magento setup:static-content:deploy -f php bin/magento cache:flushAfter this, as long as your module is enabled, this will sort all product list widget with name asc order.
Try this and accept it as a solution with giving kudos if it works for you.
@yash7690 Perfection. Thank you so much for your help.
Hi, I have a Magento ver. 2.4.3-p1.
When I try to insert the "catalog product list" widget on a CMS page it gives me this error.
"Error filtering template: Invalid block type: Magento \ CatalogWidget \ Block \ Product \ ProductsList"
I'm going crazy I don't understand the reason for the mistake.
Thanks for your help