cancel
Showing results for 
Search instead for 
Did you mean: 

Where to copy vendor/magento/module-catalog-widget/Block/Product/ProductsList.php?

SOLVED

Where to copy vendor/magento/module-catalog-widget/Block/Product/ProductsList.php?

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:

 

  • /app/design/frontend/<vendor>/<namespace>/Magento_CatalogWidget/Block/Product
  • /app/code/<vendor>/Magento_CatalogWidget/Block/Product
  • /app/code/<vendor>/CatalogWidget/Block/Product
  • /app/code/<vendor>/CatalogWidget/Block/Product/override

Seems like this shouldn't be this hard... Smiley Indifferent

 

Thanks in advance for any help!

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Where to copy vendor/magento/module-catalog-widget/Block/Product/ProductsList.php?

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.

View solution in original post

4 REPLIES 4

Re: Where to copy vendor/magento/module-catalog-widget/Block/Product/ProductsList.php?

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.

Re: Where to copy vendor/magento/module-catalog-widget/Block/Product/ProductsList.php?

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;
}
}

Re: Where to copy vendor/magento/module-catalog-widget/Block/Product/ProductsList.php?


@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: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.


@yash7690 Perfection. Thank you so much for your help.

Re: Where to copy vendor/magento/module-catalog-widget/Block/Product/ProductsList.php?

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