Hi all, I need to change the price of a product when there is an attribute during insertion of the product itself to the cart.
I realized that I can do this with the "Observer" events.
Below my code (for example), but when I add the product in cart it is not called the event:
In the directory: "app/code/MyVendor/Customprice/etc/frontend/events.xml" I included this:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="checkout_cart_product_add_after">
<observer name="custom_price" instance="MyVendor\Customprice\Model\CustompriceObserver" />
</event>
</config>
While in the directory: "app/code/MyVendor/Customprice/Model/Customprice.php" I included this:
<?php
namespace MyVendor\Customprice\Model;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Registry;
class CustompriceObserver implements ObserverInterface
{
protected $logger;
public function __construct(\Psr\Log\LoggerInterface $logger){
$this->logger = $logger;
}
public function execute(\Magento\Framework\Event\Observer $observer) {
$this->logger->debug('rrrrr');
}
}I disabled the cache and empty, why not release me anything in my debug.log files?
Thanks
Hello Vincenzondb
Move event.xml file from the directory: "app/code/MyVendor/Customprice/etc/frontend/events.xml" to "app/code/MyVendor/Customprice/etc/events.xml".
And if problem with logger then insert the following code inside your observer execute method
Create a log file 'mylog.log' inside var/log/ and add write permission to the log file
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/mylog.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info("Here your cmment");