cancel
Showing results for 
Search instead for 
Did you mean: 

REST api - dispatch events?

REST api - dispatch events?

Digging through source code it looks like the REST api does not dispatch events.  How are observers run when interacting via the REST api?   

 

We want to add to cart via REST but have an observer that we want to run.  We need to change the price of the item being added based on properties of it.  Any way to do this?  

 

7 REPLIES

Re: REST api - dispatch events?

@jschneller,

 

What about using plugins instead observers (which I understand are not possible to be used in this case).

You'll find here the oficial documentation about plugins.

--
If you've found one of my answers useful, please give "Kudos" or "Accept as Solution"

Re: REST api - dispatch events?

If you want to observe events which are dispatched in API calls, you can use the etc/webapi_rest/events.xml file. It is also possible with the etc/events.xml file which works for all areas.

 

I got that from Vinai Kopp: https://twitter.com/VinaiKopp/status/810897855527485440 including ans answer for the soap api.

Andreas von Studnitz, Trainer, Consultant, Developer and CEO of integer_net, Germany.

Re: REST api - dispatch events?

This would be great if it worked.  2 things.

 

1) Where do I find the etc/webapi_rest/events.xml file.

2) I already created my own observer and it has an events.xml at the etc/ level.  If it should work at that level then the event should be picked up it is not.

 

Digging into the actual code the webapi code never fires the event when an item is added.  With out the event being fired by the native Magento code the event listeners will never know about it.

 

I am new to Magento (and PHP) but come from a Java world where everything is very structured.  The Magento code base appears to be disorganized chaos to me with code and config files thrown all over the place with zero standards followed and documentation that doesn't match the code base.

 

 

Re: REST api - dispatch events?

1) Just create the directory webapi_rest below etc/ in your module and create the events.xml file there.

2) It worked for me with etc/events.xml and etc/webapi_rest/events.xml (modifying the address format in the cart). Are you sure you got the right event?

Andreas von Studnitz, Trainer, Consultant, Developer and CEO of integer_net, Germany.

Re: REST api - dispatch events?

1) Ok.  That makes sense.  however #2

2) the event is never fired.  Here is the event that I am listening for:

checkout_cart_product_add_after

Here is the endpoint being called (snippet taken from webapi.xml):

<route url="/V1/carts/:quoteId/items" method="POST">
<service class="Magento\Quote\Api\CartItemRepositoryInterface" method="save"/>
<resources>
<resource ref="Magento_Cart::manage" />
</resources>
</route> 

 This is the save method:

public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
{
/** @var \Magento\Quote\Model\Quote $quote */
$cartId = $cartItem->getQuoteId();
$quote = $this->quoteRepository->getActive($cartId);

$quoteItems = $quote->getItems();
$quoteItems[] = $cartItem;
$quote->setItems($quoteItems);
$this->quoteRepository->save($quote);
$quote->collectTotals();
return $quote->getLastAddedItem();
}


This is the quoteRepository->save method.  No where is an event fired:

public function save(\Magento\Quote\Api\Data\CartInterface $quote)
{
if ($quote->getId()) {
$currentQuote = $this->get($quote->getId(), [$quote->getStoreId()]);

foreach ($currentQuote->getData() as $key => $value) {
if (!$quote->hasData($key)) {
$quote->setData($key, $value);
}
}
}

$this->getSaveHandler()->save($quote);
unset($this->quotesById[$quote->getId()]);
unset($this->quotesByCustomerId[$quote->getCustomerId()]);
}

 

The only place in the entire code base where the event is fired is via the addProduct method in the vendor/magento/module-checkout/Model/Cart.php which only appears to be called from the front-end.

 

 

Re: REST api - dispatch events?

Does the API call in fact add a product? It looks as if it just saves the quote without adding anything...
An event you might observe there would be sales_quote_save_after or sales_quote_save_before. Haven't tested it though.

Andreas von Studnitz, Trainer, Consultant, Developer and CEO of integer_net, Germany.

Re: REST api - dispatch events?

Hi,

 

Does any one got solution for observer not firing for Rest Api.

 

 

Thanks,