Create an observer file using this event.


public function execute(EventObserver $observer)
        /** @var \Magento\Sales\Model\Order\Item $item */
        $item = $observer->getEvent()->getItem();
        $children = $item->getChildrenItems();
        echo $qty = $item->getQtyOrdered() - max($item->getQtyShipped(), $item->getQtyInvoiced()) - $item->getQtyCanceled();
        if ($item->getId() && $item->getProductId() && empty($children) && $qty) {
            $this->flag = "cancel";
            $this->cancelOrder = $item->getOrder()->getIncrementId();
            $this->stockManagement->backItemQty($item->getProductId(), $qty, $item->getStore()->getWebsiteId());
        return $this;

How to get $this->flag and $this->cancelOrder values in observer file using this event.


if($this->flag == "cancel")
    $orderId = $this->cancelOrder;

I guess that you want to set two variables at sales_order_item_cancel observer class then get that value to cataloginventory_stock_item_save_after's observer.

In this situation, you can use registry \Magento\Framework\Registry . https://www.mageplaza.com/magento-2-registry-register.html.

Create two registry variable at sales_order_item_cancel 's observer and get that avriable value to cataloginventory_stock_item_save_after

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.