0

How I can Create CustomLog File When I Add a Product to a Cart in Magento 2 how I can create a customlog when I add a product into the cart and create all the available product custom log

I have no solution about

thanks

3 Answers 3

1

There is an event observer when a product is added to cart, please implement the event observer, the event name is - "checkout_cart_add_product_complete", please check out this link about this event

Link - https://developer.adobe.com/commerce/php/development/components/events-and-observers/event-list/#checkout_cart_add_product_complete

To implement an event in Magento 2, please create a basic module with registering it by creating a module.xml file and registration.php and enabling it to register it.

After that create an event observer by going through the below process:-

For creating the observer, please follow this code as an example, inside the execute you can modify the way you like to add more functionalities.

<?php

declare(strict_types=1);

namespace OX\ProductEventLogger\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Psr\Log\LoggerInterface;

class ProductAddEventLogger implements ObserverInterface
{

    private $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * @param Observer $observer
     */
    public function execute(Observer $observer): void
    {
        $this->logger->debug('Product is added to cart');
    }
}

This is the events.xml file you should create inside the directory etc/frontend/events.xml.

<?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_add_product_complete">
        <observer name="ox_checkout_cart_add_product_complete"
                  instance="OX\ProductEventLogger\Observer\ProductAddEventLogger"/>
    </event>
</config>

Overall this event observer is working in my system, this event observer will work when a product is added to the cart immediately.

If there is any doubt, you know what to do!

Thanks & Regards.

1

Let's split the requirement into 2 parts.

  1. Create Custom log file.

  2. Add Observer to catch the Add to cart event and use the custom log file in the observer

Create handler.php to define the path and custom log file.

Sathya/CustomLog/Logger/Handler.php

<?php

namespace Sathya\CustomLog\Logger;

use Magento\Framework\Logger\Handler\Base;
use Monolog\Logger;

class Handler extends Base
{
    protected $loggerType = Logger::INFO;

    protected $fileName = '/var/log/custom_log_file.log';
}

Next, create the Logger.php to inject into the Observer

Sathya/CustomLog/Logger/Logger.php

<?php

namespace Sathya\CustomLog\Logger;

class Logger extends \Monolog\Logger
{

}

Pass the necessary argument which supports the logger for file systems. Create di.xml

Sathya/CustomLog/etc/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="Sathya\CustomLog\Logger\Handler">
        <arguments>
            <argument name="filesystem" xsi:type="object">Magento\Framework\Filesystem\Driver\File</argument>
        </arguments>
    </type>
    <type name="Sathya\CustomLog\Logger\Logger">
        <arguments>
            <argument name="name" xsi:type="string">Sathya_CustomLog</argument>
            <argument name="handlers" xsi:type="array">
                <item name="system" xsi:type="object">Sathya\CustomLog\Logger\Handler</item>
            </argument>
        </arguments>
    </type>
</config>

Event And Observer

  1. Event to catch the Add to Cart

Create events.xml Sathya/CustomLog/etc/events.xml

<?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_log_file_observer"
                  instance="Sathya\CustomLog\Observer\AddToCart"/>
    </event>
</config>

In the Observer inject the logger and use the Custom Logger file & handle the logic what to happen when add to cart event triggered.

Create observer, AddTocart.php Sathya/CustomLog/Observer/AddToCart.php

<?php

namespace Sathya\CustomLog\Observer;


use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
use Sathya\CustomLog\Logger\Logger;

class AddToCart implements ObserverInterface
{

    public function __construct(
        Logger $logger

    ) {
        $this->logger = $logger;
    }

    public function execute(EventObserver $observer)
    {
        $this->logger->addInfo("Custom log file in the Observer");
        $quoteItems = $observer->getQuoteItem();
        // Logic what to do in the Observer

        $this->logger->addError("Custom log file in the Observer - Error");
    }
}

Hope it helps you to understand, please modify the module name as per your need.

0

To do this, you can use event-observer.

Please check my below answer.

events.xml : app\code\Vendor\Module\etc\events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

   <!-- add logic when item added to cart -->
    <event name="checkout_cart_product_add_after">
     <observer name="pf_checkout_cart_product_add_after" instance="Vendor\Module\Observer\CheckoutCartAddObserver" />
    </event>


</config>

CheckoutCartAddObserver: app\code\Vendor\Module\Observer\CheckoutCartAddObserver.php

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

/**
 * CheckoutCartAddObserver
 */
class CheckoutCartAddObserver implements ObserverInterface
{
    protected $logger;

    public function __construct(
       \Psr\Log\LoggerInterface $logger
    ) {
    $this->logger = $logger;
    }
    
    /**
     * execute
     *
     * @param EventObserver observer
     *
     * @return void
     */
    public function execute(EventObserver $observer)
    {
        $this->logger->info("this is custom log");
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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