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.


<?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"/>


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());



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->info("afterSave test");

...and it works!

1 Answer 1


The folder in which your di file is placed is key. In your example the plugin only get executed for the frontend area.

I recommend putting it without frontend so it is used everywhere. If this is not wanted use the needed API area: https://devdocs.magento.com/guides/v2.4/architecture/archi_perspectives/components/modules/mod_and_areas.html

  • I put it in MODULE/etc now, but it does still not work.
    – Black
    Commented Nov 4, 2020 at 8:59
  • I recommend setting up xdebug and set a breakpoint to make sure it is the problem and not the way you log it
    – CloudySi
    Commented Nov 4, 2020 at 9:11
  • The way I log it works 100%. It also works in other plugins. I think it is just impossible to define plugins for API Interfaces.
    – Black
    Commented Nov 4, 2020 at 9:15
  • You were right, my log method does not work in API Interface plugins, I think because they are not communicating with the browser... I logged to a file instead and now it showed the message :) so the plugin was triggered.
    – Black
    Commented Nov 4, 2020 at 10:55

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.