Giter Club home page Giter Club logo

Comments (1)

mattias-persson avatar mattias-persson commented on June 17, 2024 2

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

from laravel-shopr.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.