Giter VIP home page Giter VIP logo

shoppingcart's Introduction

ShoppingCart

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

a Simple ShoppingCart impelementaion for Laravel.

Install

Install the package through Composer.

	composer require BahaaAlhagar/ShoppingCart

If you're using Laravel 5.5, this is all there is to do. if you're using Laravel 5.4 or less version then you have the register the package service provider and the package alias in your app. open config/app.php file. Add a new line to the providers array:

	BahaaAlhagar\ShoppingCart\ShoppingCartServiceProvider::class,

And add a new line to the aliases array:

	'Cart' => BahaaAlhagar\ShoppingCart\Facades\Cart::class,

Now you're ready to start using the shoppingcart in your application.

If you want a quick real test or a first step for your app you may install the basic shop that comes with the ShoppingCart.

in your terminal

	php artisan make:shop

that will move the required files and migration files to your application, then we will have to migrate the required tables and seed some dummy data. first migrate the tables.

	php artisan migrate

then add the test products.

	php artisan db:seed --class=ShopSeeder

you may now visite the basic shop at

	http://yourAppUrl/shop

Usage

The ShoppingCart gives you the following methods to use:

Cart::add()

Adding an item to the cart is really simple, you just use the add() method, which accepts a variety of parameters.

In its most basic form you can specify the product Model, quantity ($qty) you'd like to add to the cart. note that the Product model must have a price column.

Cart::add($productModel, $qty);

if you didnt specify the $qty the cart will assume its 1 item.

As an optional third parameter you can pass it options, so you can add multiple items with the same id, but with (for instance) a different size or color for example.

Cart::add($productModel, $qty, ['size' => 'large', 'color' => 'black']);

The add() method will return CartItem array of the item you just added to the cart.

Cart::modify()

To modify an item in the cart, you'll first need the uinqueIndex of the item.

the uniqueIndex is the item offset in the cart Model items array.

Next you can use the modify() method to modify it.

If you simply want to modify the quantity, you'll pass the modify method the uinqueIndex and the new quantity:

$uinqueIndex = '20185a4530208f76b2ef3eb95307a021';

Cart::modify($uinqueIndex, 2); // Will modify the quantity

Cart::reduceOneItem()

to reduce certain Cart item quantity by 1, you'll first need the uinqueIndex of the item.

then

$uinqueIndex = '20185a4530208f76b2ef3eb95307a021';

Cart::reduceOneItem($uinqueIndex); // will reduce item quantity by 1

Cart::remove()

To remove an item for the cart, you'll again need the uniqueIndex. This uniqueIndex you simply pass to the remove() method and it will remove the item from the cart.

$uniqueIndex = '20185a4530208f76b2ef3eb95307a021';

Cart::remove($uniqueIndex);

Cart::getContent()

Of course you also want to get the carts content. This is where you'll use the getContent method. This method will return a Cart model which you can iterate over and show the content to your customers.

Cart::getContent();

Cart::destroy()

If you want to completely remove the content of a cart, you can call the destroy method on the cart. This will remove the cart from Session.

Cart::destroy();

Cart::total()

The total() method can be used to get the calculated total of all items in the cart, given there price and quantity.

Cart::total();

Cart::count()

If you want to know how many items there are in your cart, you can use the count() method. This method will return the total number of items in the cart. So if you've added 2 books and 1 shirt, it will return 3 items.

Cart::count();

Exceptions

The Cart package will throw exceptions if something goes wrong. This way it's easier to debug your code using the Cart package or to handle the error based on the type of exceptions. The Cart packages can throw the following exceptions:

Exception Reason
CartIsEmptyException When trying to modify or reduce a cart item and the cart already empty
InvalidUniqueIndexException When the uniqueIndex that got passed doesn't exists in the cart
InvalidQuantityException When you try to add negative or float quantity.

Events

The cart also has events build in. There are five events available for you to listen for.

Event Fired Parameter
cartItem.added When an item was added to the cart. The CartItem that was added.
cartItem.modified When an item in the cart was modified or reduced by 1. The CartItem that was modified.
cartItem.removed When an item is removed from the cart. The CartItem that was removed.
cart.destroyed When the content of a cart was destroyed. return the removed Cart Model.

Example

you may use the php artisan make:shop command for a quick code setup, and test the Cart.

Change log

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

shoppingcart's People

Contributors

bahaaalhagar avatar

Watchers

James Cloos avatar

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.