skip to Main Content

I have created a module in which i am overwriting core functionality of product listing widget. I can achieve this using preference but i want to understand plugin method..
Below is my code for 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="MagentoCatalogWidgetBlockProductProductsList">
        <plugin name="widget_product_listing_add_attribute" type="TestCatalogWidgetPluginBlockProductProductsListAddAttributePlugin" sortOrder="1"/>
    </type>
</config>

and below is my code for plugin block.

namespace TestCatalogWidgetPluginBlockProduct;

class ProductsListAddAttributePlugin
{
    public function beforeCreateCollection(MagentoCatalogWidgetBlockProductProductsList $subject, MagentoCatalogModelResourceModelProductCollection $result)
    {
        /**
        * @var MagentoCatalogWidgetBlockProductProductsList $subject
        * @var MagentoCatalogModelResourceModelProductCollection $result
        */

        die('ProductsListAddAttributePlugin before....');
        
    }
}

after installing module and running di:compile.. when i reload the page i am getting below error.

Fatal error: Uncaught ArgumentCountError: Too few arguments to
function
TestCatalogWidgetPluginBlockProductProductsListAddAttributePlugin::beforeCreateCollection(),
1 passed in
C:xampphtdocsprojectshellovendormagentoframeworkInterceptionInterceptor.php
on line 121 and exactly 2 expected in
C:xampphtdocsprojectshelloappcodeTestCatalogWidgetPluginBlockProductProductsListAddAttributePlugin.php:6
Stack trace: #0
C:xampphtdocsprojectshellovendormagentoframeworkInterceptionInterceptor.php(121):
TestCatalogWidgetPluginBlockProductProductsListAddAttributePlugin->beforeCreateCollection(Object(MagentoCatalogWidgetBlockProductProductsListInterceptor))

1 C:xampphtdocsprojectshellovendormagentoframeworkInterceptionInterceptor.php(153):

MagentoCatalogWidgetBlockProductProductsListInterceptor->MagentoFrameworkInterception{closure}()

2 C:xampphtdocsprojectshellogeneratedcodeMagentoCatalogWidgetBlockProductProductsListInterceptor.php(26):

Mag in
C:xampphtdocsprojectshelloappcodeTestCatalogWidgetPluginBlockProductProductsListAddAttributePlugin.php
on line 6

2

Answers


  1. Have you run setup:di:compile and clear the cache? after updating the beforeCreateCollection method?

    It looks like you’re correctly passing two arguments through, but it might be referring to an older DI.

    Login or Signup to reply.
  2. Try to remove the generated code
    I think you working under Windows you can run the following command on your prompt terminal.
    Make sure first that you are under developer mode

    php bin/magento deploy:mode:set developer
    
    php rm -rf generated*
    php bin/magento cache:flush
    php bin/magento cache:clean
    php bin/magento setup:upgrade
    bin/magento setup:di:compile
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search