Image for post
Image for post
Photo by Mein Deal on Unsplash

Laravel Shopr: How to add shipping to your checkout

Laravel Shopr is a package for integrating e-commerce into your Laravel app. It gives you a shopping cart with discount coupons and a full checkout flow, just to mention a few of the useful features included.
In this guide we won’t discuss how to install or get started with the package, since it’s covered in detail in the documentation.

Conceptual overview

Adding shipping alternatives to your Shopr checkout is actually very simple. We’ll treat the shipping options as a Shoppable just like the other purchasable models, which makes it easy to add it to the cart and order. This is very similar to how discount coupons are handled.

Basically, the flow will look like this:

  1. On the checkout page, the customer can select one of the available shipping options.
  2. Before making the request to pay for the cart and convert it to an order, we’ll add the selected shipping option to the cart as a regular item.
  3. In the confirmation templates, we can easily customize how the shipping option is displayed.

That’s it!

Adding shipping options to your checkout

First of all, we create a migration for the shipping_options table.

And the model, which extends the Shoppable class instead of the default Model class.

Next, we’ll create an endpoint for retrieving the available shipping options on the checkout page. This will be called to present the options to the customer.

Don’t forget to also add a route for it.

You then present a few radio buttons or similar which allows the customer to select their desired shipping method.
In this example we’ll use Stripe to process the payments, so we want to add the shipping option to the cart after retrieving the Stripe token but before making the charge request. For more details on the Stripe checkout flow, check out the demo implementation.

We have now added the selected shipping method to our order. The price is automatically added to the order total and everything should work. However, we want to exclude it from the order items table in the confirmation template. This is done by filtering the order items by their shoppable_type (the model).

To make it more testable and keep the logics away from your template, you might want to use custom models for your order and add a `printableItems` method or something similar to it which filters out the shipping option row. Then, just call the `$order->printableItems` in the confirmation template. To access the shipping method, add another method which does the opposite (only returns the shipping method).
Read more about how to use custom models for your orders in the documentation.

All done!

Bonus: using a dynamic price for a shipping option

Imagine you want a shipping method to be free when placing an order worth $100 or more. This can be achieved by setting the free_level-column in the database to 100, then modifying the getPrice method on the ShippingMethod model to check the cart value before returning the price:

Don’t forget to make sure your frontend displays the correct price for the shipping method as well.

Written by

Owner of Happy Pixels. Certified Laravel developer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store