Skip to content

7.1 Handling Wishlist cart.

Security Theater edited this page Oct 18, 2018 · 4 revisions

Surprisingly the Wish list methods are typically the same as Cart method, just with a little modifications we are about to explore.

NOTE: we are only going to discover the modifications , any other method isn't mentioned here but mentioned within the Cart methods, it works perfectly here as well.

  • Adding To Cart

While working out with this method, please bear in mind that it doesn't decrement the stock, which in some sense if you don't validate properly, user may add to his wish list items with amount that actually doesn't exist.

That's not actually that terrifying , as once the user decides to transform the wish list into an actual cart, we check on the available stock and check if it can be actually transformed.

  • Getting Total Of Wish list
Wishlist::total();

Bear in mind that this method doesn't calculate the tax

  • ClearAll Wish list for User
Wishlist::clearAll(); // returns the number of the detached items.

Remember it returns the released stock of each item within the cart, unlike the wish list.

  • Pushing the wish list to an actual cart

This method transforms the wish list into an actual cart.

// $wishlist could be an instance of wishlist model, or an id that the method will retrieve later.
Wishlist::pushWishToCart($wishlist); // returns the cart that's recently created.

This method actually will detach the passed wish list and attach it to the cart if the stock is only available , unless that insufficient product exception is thrown, and the wish list remains exist as nothing happened.

Optionally, you can pass the user that you wish to push the wish list to his/her cart.

$user = User::find(1); // or auth()->user();
Wishlist::pushWishToCart($wishlist, $user);