-
Notifications
You must be signed in to change notification settings - Fork 56
7.0 Handling Cart.
Of course the carts table contain of simple things that indicate which actually product and which variation of it is added to the cart associated with the quantity that has been reserved.
Since we are mentioning out the stock again, that means we have the stock methods we explained earlier as the trait is also used here.
First things first, Let's start with creating a cart item.
Cart::create([
'quantity' => 10,
'product_id' => $product->id,
'product_variation_type_id' => $productTypeVariation->id
]);
Bear in mind, that's the normal create method that Eloquent provides, which has its own drawbacks in this particular situation, as the supplied quantity is a pseudo number, and we haven't check on the product_variation if it really has this number in stock.
To Avoid this, before creating ,you can use the canBeAdded method to check firstly if the quantity that user tries to pick is actually available.
if (Cart::canBeAdded($productTypeVariation->id, 10) {
Cart::create([
'quantity' => 10,
'product_id' => $product->id,
'product_variation_type_id' => $productTypeVariation->id
]);
}
The only drawback for this, that the product_variations stock isn't updated or decremented with the quantity that's added to the cart.
Alternatively , you can use the "add" Method which will do the work for you
- It will check if the quantity can be added or not , Exception will be thrown if stock is less than passed quantity.
- Decrement the Product_variations table's stock column with the passed quantity
- Increment the cart's record.
$type = ProductType::first();
Cart::add($type,3); // returns the cart.
You probably noticed that the cart item should exist in order to use this method, but you can tweak this around using the following method, or passing the third argument to the add method.
$type = ProductType::first(); // product_variation_type.
Cart::add($type,3, true); // returns the cart.
// OR
Cart::addOrCreate($type,3); // return the cart.
- Removing Cart Item
This method will remove the cart item, and increment the variation type's stock by the item's quantity.
// assuming this cart has quantity : 3
// assuming the product variation type's stock : 5
$id = Cart::first()->id;
Cart::remove($id); // returns boolean.
// product variation type's stock now : 8
- Clearing Cart
You might clear all of the cart for the authenticated user, you can achieve that by using the clearAll method, and it will increment each product variation that's corresponding to the cart item.
Cart::clearAll(); // returns the total quantities released.
- Clearing Cart for specific user
You can use the clearFor method, and pass the user you wish to clear his/her cart.
Cart::clearFor(User::find(10)); // returns the total quantities released.
The passed user should implement the UserInterface.
- Update Cart for specific user
The renew method takes care of the remaining stock of the product variation, and the current quantity that exist in the cart, It either increment or decrement the stock of the product variation depending on comparing between the passed quantity and the current quantity that exist in the cart
// assuming the current cart quantity is set to 5
// assuming the product variation stock is set to 10
// renewing the current cart quantity and set it to 10
// this will set the product variation stock to 5 , as we only needed 5 from there.
Cart::renew($cart,[
'quantity' => 10
]);
// the method decrement or increment depending on the comparison.
If you ever decided to pass along the quantity , the product_id or product_variation_type_id, this will emit the method to delete the current cart item, and create a new one with the passed ids, and of course it will take care of the stock.
Cart::renew($cart,[
'product_id' => $product->id,
'product_variation_type_id' => $productType->id
]);
// if the quantity isn't set , it will retrieve its quantity from the passed cart, and assign it.
- fetch Cart Item
you may need to fetch an item via an id , or via a specification, or the previous conditions combined. So far, this can be done easily
Cart::item(1); // returns an instance of the cart model, or throws an exception.
Cart::item(1, ['some-random-feature' => 'dummy-text']); // returns an instance of the cart model which contains these attributes , or exception is thrown.
Cart::item(null,['some-random-feature' => 'dummy-text']); // returns a collection of carts containing the passed attributes.
Cart::item(); // returns the authenticated user cart's items.
- Fetch authenticated user's cart items
Cart::items(); // returns the authenticated user's items.
- fetch total of the cart items.
We can fetch the total of the cart elegantly using the total method, and it will check automatically if the tax is enabled, then it will calculate the total after applying the tax, therefore you have to check on the tax within your config/market.php whether you need it enabled or not.
Cart::total(); // returns integer.
Optionally, you can pass coupons which the package will retrieve and apply their percentage on the total then returns the total after the coupons being applied.
$coupon = Coupon::get(); // collection of coupons.
Cart::total($coupons); // returns the total after applying coupons.
- fetch subtotal of the cart items
Don't worry, we haven't forgotten about the sales while calculating the total process, as the subtotal is being calculated first then we call for the total, and within the subtotal process, we take care of the sales and we check on the product's price whether it should be the price within the products table or within the products variation table.
Cart::subtotal(); // returns an integer after checking on price and sales.
- fetch subtotal of the cart after applying coupons
You can pass the coupons you want to apply on the subtotal through this method.
$coupons = Coupon::whereSomeRandomCondition()->get(); // collection
Cart::subtotalAfterCoupon($coupons); // returns the price after processing the coupons' percentage.
Don't get it twisted while calling the total method as it calls out for either subtotal method or subtotalAfterCoupon which basically depends on passing coupons.
If you are still confused, Let's check this example.
Cart::total();
Behind the scenes, the subtotal method is being called as there are no coupons passed, and within the subtotal method we will take care of the price and the sales.
Cart::total($coupons);
behind the scenes, the subtotalAfterCoupon method is being called as there are coupons passed, and within the subtotalAfterCoupon method, it actually waits for the subtotal method to finish then it applies the coupons on the returned value from the subtotal method, which again will take care of the price and sales.