laravel-shopr icon indicating copy to clipboard operation
laravel-shopr copied to clipboard

Facades for Cart and Coupons

Open mattias-persson opened this issue 5 years ago • 1 comments

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

  • [x] Getting the cart summary
  • [x] Getting the current cart count
  • [x] Clearing the cart
  • [x] Adding a cart item
  • [x] Updating the quantity of a cart item
  • [x] Removing a cart item
  • [x] Applying a discount coupon

mattias-persson avatar Dec 22 '18 11:12 mattias-persson

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.
}

mattias-persson avatar Dec 23 '18 09:12 mattias-persson