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?
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 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.
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.
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?
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.
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.
Hi,
Does any one got solution for observer not firing for Rest Api.
Thanks,
I have custom module and my code structure is as per service contact.
app/code/[Vendor]/[Module]/Model/AccountManagement.php
Now i have a dispatch event in my model file
$this->_eventManager->dispatch('vendor_register_success', $eventParams);
As per REST API standard and as per the this solution, i added event in specific module where it comes from.
app/code/[Vendor]/[Module]/etc/webapi_rest/events.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="vendor_register_success"> <observer name="vendor_register_success" instance="[Vendor]\[Module]\Observer\VendorBrandRegister" /> </event> </config>
and My observer looks like this
app/code/[Vendor]/[Module]/Observer/VendorBrandRegister.php
public function execute(\Magento\Framework\Event\Observer $observer) { echo "In execute"; exit; }
but this event is not dispatching.
What am i missing ?
Because as per the solution, i need to add events in `webapi_rest` folder in `etc` folder.
I have tried in add to cart rest API but event is not working.