Skip to main content
added 371 characters in body
Source Link
Black
  • 3.4k
  • 4
  • 40
  • 123

In the documentation it says:

"A plugin, or interceptor, is a class that modifies the behavior of public class functions by intercepting a function call and running code before, after, or around that function call. This allows you to substitute or extend the behavior of original, public methods for any class or interface."

But is this also true for API?

I try to write a afterSave Plugin for the class Magento\Quote\Api\CartItemRepositoryInterface to log something if the quote is getting saved.

But it does not work and no log is created.

app/code/Company/Checkout/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <type name="Magento\Quote\Api\CartItemRepositoryInterface">
        <plugin name="Company_Checkout_CartItemRepositoryInterface" type="Company\Checkout\Plugin\Quote\Api\CartItemRepositoryInterface" sortOrder="10"/>
    </type>
</config>

app/code/Company/Checkout/Plugin/Quote/Api/CartItemRepositoryInterface.php

<?php
namespace Company\Checkout\Plugin\Quote\Api;

class CartItemRepositoryInterface
{
    protected $_cartFactory;

    public function __construct(
        \Magento\Checkout\Model\CartFactory $cartFactory
    ) {
        $this->_cartFactory = $cartFactory;
    }

    public function afterSave(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
    {
        $cart = $this->_cartFactory->create();
        $quote = $cart->getQuote();

        \Eddcapone\ChromePhp\Helper\ChromePhp::log("Hello from afterSave Plugin (API: module-quote/api/CartItemRepositoryInterface/save");
        \Eddcapone\ChromePhp\Helper\ChromePhp::log("QuoteId = ". $quote->getId());
    }



}

Update:

The reason was, that I tried to log by using chromePhp, which does not seem to work at API Interface plugins... Now I used this approach to log it:

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/zend_debug.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info("afterSave test");

...and it works!

In the documentation it says:

"A plugin, or interceptor, is a class that modifies the behavior of public class functions by intercepting a function call and running code before, after, or around that function call. This allows you to substitute or extend the behavior of original, public methods for any class or interface."

But is this also true for API?

I try to write a afterSave Plugin for the class Magento\Quote\Api\CartItemRepositoryInterface to log something if the quote is getting saved.

But it does not work and no log is created.

app/code/Company/Checkout/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <type name="Magento\Quote\Api\CartItemRepositoryInterface">
        <plugin name="Company_Checkout_CartItemRepositoryInterface" type="Company\Checkout\Plugin\Quote\Api\CartItemRepositoryInterface" sortOrder="10"/>
    </type>
</config>

app/code/Company/Checkout/Plugin/Quote/Api/CartItemRepositoryInterface.php

<?php
namespace Company\Checkout\Plugin\Quote\Api;

class CartItemRepositoryInterface
{
    protected $_cartFactory;

    public function __construct(
        \Magento\Checkout\Model\CartFactory $cartFactory
    ) {
        $this->_cartFactory = $cartFactory;
    }

    public function afterSave(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
    {
        $cart = $this->_cartFactory->create();
        $quote = $cart->getQuote();

        \Eddcapone\ChromePhp\Helper\ChromePhp::log("Hello from afterSave Plugin (API: module-quote/api/CartItemRepositoryInterface/save");
        \Eddcapone\ChromePhp\Helper\ChromePhp::log("QuoteId = ". $quote->getId());
    }



}

In the documentation it says:

"A plugin, or interceptor, is a class that modifies the behavior of public class functions by intercepting a function call and running code before, after, or around that function call. This allows you to substitute or extend the behavior of original, public methods for any class or interface."

But is this also true for API?

I try to write a afterSave Plugin for the class Magento\Quote\Api\CartItemRepositoryInterface to log something if the quote is getting saved.

But it does not work and no log is created.

app/code/Company/Checkout/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <type name="Magento\Quote\Api\CartItemRepositoryInterface">
        <plugin name="Company_Checkout_CartItemRepositoryInterface" type="Company\Checkout\Plugin\Quote\Api\CartItemRepositoryInterface" sortOrder="10"/>
    </type>
</config>

app/code/Company/Checkout/Plugin/Quote/Api/CartItemRepositoryInterface.php

<?php
namespace Company\Checkout\Plugin\Quote\Api;

class CartItemRepositoryInterface
{
    protected $_cartFactory;

    public function __construct(
        \Magento\Checkout\Model\CartFactory $cartFactory
    ) {
        $this->_cartFactory = $cartFactory;
    }

    public function afterSave(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
    {
        $cart = $this->_cartFactory->create();
        $quote = $cart->getQuote();

        \Eddcapone\ChromePhp\Helper\ChromePhp::log("Hello from afterSave Plugin (API: module-quote/api/CartItemRepositoryInterface/save");
        \Eddcapone\ChromePhp\Helper\ChromePhp::log("QuoteId = ". $quote->getId());
    }



}

Update:

The reason was, that I tried to log by using chromePhp, which does not seem to work at API Interface plugins... Now I used this approach to log it:

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/zend_debug.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info("afterSave test");

...and it works!

added 51 characters in body
Source Link
Black
  • 3.4k
  • 4
  • 40
  • 123

In the documentation it says:

"A plugin, or interceptor, is a class that modifies the behavior of public class functions by intercepting a function call and running code before, after, or around that function call. This allows you to substitute or extend the behavior of original, public methods for any class or interface."

But is this also true for API?

I try to write a afterSave Plugin for the class Magento\Quote\Api\CartItemRepositoryInterface to log something if the quote is getting saved.

But it does not work and no log is created.

app/code/Company/Checkout/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <type name="Magento\Quote\Api\CartItemRepositoryInterface">
        <plugin name="Company_Checkout_CartItemRepositoryInterface" type="Company\Checkout\Plugin\Quote\Api\CartItemRepositoryInterface" sortOrder="10"/>
    </type>
</config>

app/code/Company/Checkout/Plugin/Quote/Api/CartItemRepositoryInterface.php

<?php
namespace Company\Checkout\Plugin\Quote\Api;

class CartItemRepositoryInterface
{
    protected $_cartFactory;

    public function __construct(
        \Magento\Checkout\Model\CartFactory $cartFactory
    ) {
        $this->_cartFactory = $cartFactory;
    }

    public function afterSave(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
    {
        $cart = $this->_cartFactory->create();
        $quote = $cart->getQuote();

        \Eddcapone\ChromePhp\Helper\ChromePhp::log("Hello from afterSave Plugin (API: module-quote/api/CartItemRepositoryInterface/save");
        \Eddcapone\ChromePhp\Helper\ChromePhp::log("QuoteId = ". $quote->getId());
    }



}

In the documentation it says:

"A plugin, or interceptor, is a class that modifies the behavior of public class functions by intercepting a function call and running code before, after, or around that function call. This allows you to substitute or extend the behavior of original, public methods for any class or interface."

But is this also true for API?

I try to write a afterSave Plugin for the class Magento\Quote\Api\CartItemRepositoryInterface to log something if the quote is getting saved.

But it does not work and no log is created.

app/code/Company/Checkout/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <type name="Magento\Quote\Api\CartItemRepositoryInterface">
        <plugin name="Company_Checkout_CartItemRepositoryInterface" type="Company\Checkout\Plugin\Quote\Api\CartItemRepositoryInterface" sortOrder="10"/>
    </type>
</config>

app/code/Company/Checkout/Plugin/Quote/Api/CartItemRepositoryInterface.php

<?php
namespace Company\Checkout\Plugin\Quote\Api;

class CartItemRepositoryInterface
{
    protected $_cartFactory;

    public function __construct(
        \Magento\Checkout\Model\CartFactory $cartFactory
    ) {
        $this->_cartFactory = $cartFactory;
    }

    public function afterSave()
    {
        $cart = $this->_cartFactory->create();
        $quote = $cart->getQuote();

        \Eddcapone\ChromePhp\Helper\ChromePhp::log("Hello from afterSave Plugin (API: module-quote/api/CartItemRepositoryInterface/save");
        \Eddcapone\ChromePhp\Helper\ChromePhp::log("QuoteId = ". $quote->getId());
    }



}

In the documentation it says:

"A plugin, or interceptor, is a class that modifies the behavior of public class functions by intercepting a function call and running code before, after, or around that function call. This allows you to substitute or extend the behavior of original, public methods for any class or interface."

But is this also true for API?

I try to write a afterSave Plugin for the class Magento\Quote\Api\CartItemRepositoryInterface to log something if the quote is getting saved.

But it does not work and no log is created.

app/code/Company/Checkout/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <type name="Magento\Quote\Api\CartItemRepositoryInterface">
        <plugin name="Company_Checkout_CartItemRepositoryInterface" type="Company\Checkout\Plugin\Quote\Api\CartItemRepositoryInterface" sortOrder="10"/>
    </type>
</config>

app/code/Company/Checkout/Plugin/Quote/Api/CartItemRepositoryInterface.php

<?php
namespace Company\Checkout\Plugin\Quote\Api;

class CartItemRepositoryInterface
{
    protected $_cartFactory;

    public function __construct(
        \Magento\Checkout\Model\CartFactory $cartFactory
    ) {
        $this->_cartFactory = $cartFactory;
    }

    public function afterSave(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
    {
        $cart = $this->_cartFactory->create();
        $quote = $cart->getQuote();

        \Eddcapone\ChromePhp\Helper\ChromePhp::log("Hello from afterSave Plugin (API: module-quote/api/CartItemRepositoryInterface/save");
        \Eddcapone\ChromePhp\Helper\ChromePhp::log("QuoteId = ". $quote->getId());
    }



}
Source Link
Black
  • 3.4k
  • 4
  • 40
  • 123

Magento 2 - Write plugin for API interface

In the documentation it says:

"A plugin, or interceptor, is a class that modifies the behavior of public class functions by intercepting a function call and running code before, after, or around that function call. This allows you to substitute or extend the behavior of original, public methods for any class or interface."

But is this also true for API?

I try to write a afterSave Plugin for the class Magento\Quote\Api\CartItemRepositoryInterface to log something if the quote is getting saved.

But it does not work and no log is created.

app/code/Company/Checkout/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <type name="Magento\Quote\Api\CartItemRepositoryInterface">
        <plugin name="Company_Checkout_CartItemRepositoryInterface" type="Company\Checkout\Plugin\Quote\Api\CartItemRepositoryInterface" sortOrder="10"/>
    </type>
</config>

app/code/Company/Checkout/Plugin/Quote/Api/CartItemRepositoryInterface.php

<?php
namespace Company\Checkout\Plugin\Quote\Api;

class CartItemRepositoryInterface
{
    protected $_cartFactory;

    public function __construct(
        \Magento\Checkout\Model\CartFactory $cartFactory
    ) {
        $this->_cartFactory = $cartFactory;
    }

    public function afterSave()
    {
        $cart = $this->_cartFactory->create();
        $quote = $cart->getQuote();

        \Eddcapone\ChromePhp\Helper\ChromePhp::log("Hello from afterSave Plugin (API: module-quote/api/CartItemRepositoryInterface/save");
        \Eddcapone\ChromePhp\Helper\ChromePhp::log("QuoteId = ". $quote->getId());
    }



}