Skip to content

Before Plugin asks for the class argument

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

class PluginSolution
{
    public function beforeSetName(MagentoCatalogModelProduct $subject=null,$name){
        return "Before Plugin : ". $name;
    }
}

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

 <type name="MagentoCatalogModelProduct">
        <plugin name="before_set_name"
            type="CloudWaysFirstModulePluginPluginSolution"
            sortOrder="10" />
    </type>

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

<?PHP
namespace  CloudWaysFirstModuleControllerIndex;


class Index extends Action{
     protected $productFactory;

     public function __construct(ProductFactory $productFactory){
                  $this->productFactory=$productFactory;
     }
        public function execute()
    {
        // $objectManager = MagentoFrameworkAppObjectManager::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)"} []