0

I am new to Magento and following a course, yet some things hold me back. I really want to learn it but my only duties are as a frontend developer. If you could guide me through this problem it would be great and also if you please tell me how to become the best in regards as to the best Frontend Developer for Magento. Thanks to everyone who will reply.

I have my PluginSolution.php as this:

<?php
namespace CloudWays\FirstModule\Plugin;

class PluginSolution
{
    public function beforeSetName(Magento\Catalog\Model\Product $subject=null,$name){
        return "Before Plugin : ". $name;
    }
}

In my di.xml file I wrote the agreement like this:

 <type name="Magento\Catalog\Model\Product">
        <plugin name="before_set_name"
            type="CloudWays\FirstModule\Plugin\PluginSolution"
            sortOrder="10" />
    </type>

In the index.php file, I wrote the code like this:

<?PHP
namespace  CloudWays\FirstModule\Controller\Index;


class Index extends Action{
     protected $productFactory;

     public function __construct(ProductFactory $productFactory){
                  $this->productFactory=$productFactory;
     }
        public function execute()
    {
        // $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
      
        $product = $this->productFactory->create()->load(1);
        $product->setName("Iphone 6");
        $productName=$product->getName();
        echo $productName;
        exit;
    }
}

But I still get the following error:

#28 {main} {"exception":"[object] (TypeError(code: 0): CloudWays\\FirstModule\\Plugin\\PluginSolution::beforeSetName(): Argument #1 ($subject) must be of type ?CloudWays\\FirstModule\\Plugin\\Magento\\Catalog\\Model\\Product, Magento\\Catalog\\Model\\Product\\Interceptor given, called in /home/hakuna/Magento_Dev/vendor/magento/framework/Interception/Interceptor.php on line 121 at /home/hakuna/Magento_Dev/app/code/CloudWays/FirstModule/Plugin/PluginSolution.php:6)"} []

2 Answers 2

0

In your PluginSolution.php file, you've defined a beforeSetName plugin method:

public function beforeSetName(Magento\Catalog\Model\Product $subject = null, $name) {
return "Before Plugin : " . $name;

}

However, the error message indicates that the $subject argument is of type Magento\Catalog\Model\Product\Interceptor, not Magento\Catalog\Model\Product. This issue usually arises when the Magento class you're trying to intercept has been generated with a proxy/interceptor.

To fix this issue, you should update your plugin method signature to accept the correct class type, which is the generated interceptor class. Here's how you can modify your PluginSolution.php file:

public function beforeSetName(\Magento\Catalog\Model\Product $subject, $name) {
return [$name];

}

removed the null default value for $subject, as it's not necessary and could potentially cause issues.

Also, please ensure that your di.xml file is correctly placed and has the correct namespace and module name. It should be in the app/code/CloudWays/FirstModule/etc directory and named di.xml.

After making these changes, clear the Magento cache, and try running your code again. If you still encounter issues, make sure you have properly registered your module and that your module is enabled. You can do this by running the following commands:

php bin/magento module:enable CloudWays_FirstModule

php bin/magento setup:upgrade php bin/magento cache:clean

2
  • Thank you so much. Could you please guide me in the direction for a better Magento Frontend Dev. I am still understanding the basic but would love any guide thrown at me. THANK YOU. Aug 22, 2023 at 6:24
  • @HakunaMatata sure
    – Prachi
    Aug 24, 2023 at 7:38
0
  • You need to add a backward slash () before class name else it will look into current directory!
  • No need to pass nullin parameter

So your plugin should be:

public function beforeSetName(\Magento\Catalog\Model\Product $subject,$name){
    return "Before Plugin : ". $name;
}
1
  • welcome @HakunaMatata
    – Pawan
    Aug 22, 2023 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.