2

When trying to stop checkout process with observer on 'sales_order_place_before' event, process is stopped but observer Exception doesn't show up, rather Magento/Checkout/Model/PaymentInformationManagement message is showing up ('Unable to place order. Please try again later.')

Is it possible to have observer's message or at least both?

public function execute(Observer $observer) {
    $this->_logger->debug('SingleVendorOrderObs:sales_order_place_before');

    /** @var $order \Magento\Sales\Model\Order */
    $order = $observer->getOrder();

    $cartVendors = [];
    foreach($order->getAllVisibleItems() as $item) {
        $cartVendors[] = $item->getUdropshipVendor();
    }

    if(count($cartVendors) > 1) {
        /* dev debug */
        $this->_logger->debug('cnt: ' . count($cartVendors) . ' vendors: ' . json_encode($cartVendors));

        throw new LocalizedException(__("You can't order items from multiple vendors! "));
        // override savePaymentInformationAndPlaceOrder app/code/Magento/Checkout/Model/PaymentInformationManagement.php
    }
}
3

I used this code for exeption generation in event observer.

if(count($cartVendors) > 1) 
{
    $message = "You can't order items from multiple vendors!";
    throw new \Magento\Framework\Exception\LocalizedException(__($message));
    return;
}
6
  • No it is not working, still getting 'Unable to place order. Please try again later.' which comes from savePaymentInformationAndPlaceOrder() later on in chain. Sep 28 '16 at 8:01
  • Have your code get $cartVendors count > 1? Sep 28 '16 at 9:09
  • Yes, code gets into IF, and exception gets thrown, thing is that it is suppressed and next exception gets shown, which happens on savePaymentInformationAndPlaceOrder() Sep 28 '16 at 9:12
  • Can you try to replace "return" to "exit" in above code. Sep 28 '16 at 9:15
  • Nothing changes. If I change Magento/Checkout/Model/PaymentInformationManagement.php at line 71 throw new CouldNotSaveException(__($e->getMessage() . 'Unable to place order. Please try again later.'), $e); with "$e->getMessage()" I can get the previous error but it is really a wrong way to do it. Sep 28 '16 at 9:20
1

This is because of a try/catch in \Magento\Checkout\Model\PaymentInformationManagement

public function savePaymentInformationAndPlaceOrder(
    $cartId,
    \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
    \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
) {
    $this->savePaymentInformation($cartId, $paymentMethod, $billingAddress);
    try {
        $orderId = $this->cartManagement->placeOrder($cartId);
    } catch (\Exception $e) {
        throw new CouldNotSaveException(
            __('An error occurred on the server. Please try to place the order again.'),
            $e
        );
    }
    return $orderId;
}

If you want the real exception message to come through, you would need to remove the try/catch.

public function savePaymentInformationAndPlaceOrder(
    $cartId,
    \Magento\Quote\Api\Data\PaymentInterface $paymentMethod,
    \Magento\Quote\Api\Data\AddressInterface $billingAddress = null
) {
    $this->savePaymentInformation($cartId, $paymentMethod, $billingAddress);
    //try {
        $orderId = $this->cartManagement->placeOrder($cartId);
    // } catch (\Exception $e) {
    //     throw new CouldNotSaveException(
    //         __('An error occurred on the server. Please try to place the order again.'),
    //         $e
    //     );
    // }
    return $orderId;
}

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.