happypixels / laravel-shopr

A developer-friendly e-commerce foundation for your Laravel app
https://laravel-shopr.happypixels.se
MIT License
208 stars 19 forks source link

Facades for Cart and Coupons #10

Open mattias-persson opened 5 years ago

mattias-persson commented 5 years ago

Is your feature request related to a problem? Please describe. We currently only support using the REST API for dealing with the cart. This may be insufficient for more complex shops where custom functionality needs to be combined with it.

Describe the solution you'd like Implement facades and helper methods for managing the cart.

Additional context Possibly also allow configuring whether the REST api should be enabled at all.

Implement facade methods for

mattias-persson commented 5 years ago

Some suggestions for managing the cart:

use Happypixels\Shopr\Facades\Cart;

// Get the cart summary.
Cart::get();

// Get the cart count.
Cart::count();

// Clear the cart.
Cart::clear();

// Add a cart item, set the quantity of it, apply options, add sub items, ovverride the price.
Cart::add($shoppable)
    ->quantity($request->get('quantity'))
    ->withOptions($request->get('options'))
    ->withSubItems($request->get('sub_items'))
    ->overridePrice($request->get('price'))
    ->save();

// Update the cart item quantity.
Cart::find($id)->update(['quantity' => $request->get('quantity')]);

// Delete a cart item.
Cart::delete($id);

And for applying a discount coupon (I suggest we throw an exception for each validation rule so it's easy to handle each scenario if necessary):

try {
    Cart::addDiscount($coupon);
} catch (\Happypixels\Shopr\Exceptions\CartNotEmptyException $e) {
    // The cart isn't empty.
} catch (\Happypixels\Shopr\Exceptions\OnlyOneCouponPerOrderException $e) {
    // A coupon has already been applied.
} catch (\Happypixels\Shopr\Exceptions\CouponAlreadyAppliedException $e) {
    // This coupon has already been applied.
} catch (\Happypixels\Shopr\Exceptions\CouponNotFoundException $e) {
    // The coupon doesn't exist.
} catch (\Happypixels\Shopr\Exceptions\CouponTimespanInvalidException $e) {
    // The coupon is invalid due to it's timespan.
} catch (\Happypixels\Shopr\Exceptions\CartValueTooLowException $e) {
    // The cart value is below the coupon lower limit or value.
} catch (\Exception $e) {
    // Something else went wrong.
}