0

I want to check if the products which are getting shipped are available in my warehouse and if these aren't available then i need to stop the further execution of shipment creation n throw an error message.

I am using "sales_order_shipment_save_before" event observer.

Please let me know what my execution function should return inorder to stop the shipment being created.

public function execute(Observer $observer)
{

}  
0

You can do something like this.

public function execute(Observer $observer)
{
    $items = $observer->getEvent()->getShipment()->getAllItems();
    if($items)
    {
        foreach ($items as $_item) {
            $product_id = $_item->getProductId();
            if(!$this->_isAvailableInWarehouse($product_id))
            {
                $message = $_item->getName().' is not available in warehouse';
                throw new \Magento\Framework\Exception\LocalizedException(__($message));
                return;
            }
        }
    }
}

protected function _isAvailableInWarehouse($product_id)
{
    // your custom condition code here.
    return false;
}
| improve this answer | |

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.