Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rescue from Spree::Order::InsufficientStock on frontend checkout flow #3023

Merged

Commits on Jan 12, 2019

  1. Rescue from InsufficientStock error on frontend checkout flow

    When two users try to purchase the last item remaining from a non-backordeable
    stock location at the same time then the last one will experience an unhandled
    error `Spree::Order::InsufficientStock`.
    
    This happens only if there is a second backorderable stock location for the
    product.
    
    When there is no backorderable stock location the controller `before_action`
    `ensure_sufficient_stock_lines` is enough to catch the issue in advance.
    
    The error is generated by this line in Spree::Order model:
    
    `before_transition to: :complete, do: :validate_line_item_availability`
    
    Generally, `ensure_sufficient_stock_lines` prevent customers to complete the
    checkout process when there is not enough stock availability, but the case
    above is not caught here.
    
    So, by using `rescue_from` the customer is now redirected to the checkout
    `address` page and shown an error message that suggests to repeat the
    checkout process.
    
    The order's shipments will be rebuilt using the backorderable stock location
    in the delivery step, allowing them to (hopefully!) complete the purchase.
    
    `rescue_from` is already used on the `api` and `backend` section in order to
    manage `InsufficientStock` errors, so this was a natural choice also on the
    `frontend`.
    spaghetticode committed Jan 12, 2019
    Copy the full SHA
    fb9a631 View commit details
    Browse the repository at this point in the history