Example #1
0
 /**
  * Add product to cart.
  *
  * @param App\Models\Product  $product
  * @return Cartalyst\Cart\Collections\ItemCollection
  */
 protected function addToCart($product)
 {
     // Set the global conditions order
     $this->cart->setConditionsOrder(['discount', 'other', 'tax', 'shipping', 'coupon']);
     // Set the items conditions order
     $this->cart->setItemsConditionsOrder(['discount', 'other', 'tax', 'shipping']);
     // Item conditions
     $condition1 = $this->createCondition('VAT (17.5%)', 'tax', 'subtotal', ['value' => '17.50%']);
     $condition2 = $this->createCondition('VAT (23%)', 'tax', 'subtotal', ['value' => '23%']);
     $condition3 = $this->createCondition('Discount (7.5%)', 'discount', 'subtotal', ['value' => '-7.5%']);
     $condition4 = $this->createCondition('Item Based Shipping', 'shipping', 'subtotal', ['value' => '20.00']);
     // Add the item to the cart
     $item = $this->cart->add(['id' => $product->id, 'name' => $product->name, 'price' => $product->price, 'quantity' => 1, 'conditions' => [$condition1, $condition2, $condition3, $condition4]]);
     // Global conditions
     $condition1 = $this->createCondition('Global Tax (12.5%)', 'tax', 'subtotal', ['value' => '12.50%']);
     $condition2 = $this->createCondition('Global discount (5%)', 'tax', 'subtotal', ['value' => '-5%']);
     $condition3 = $this->createCondition('Global Shipping', 'shipping', 'subtotal', ['value' => '20.00%']);
     // Set the global conditions
     $this->cart->condition([$condition1, $condition2, $condition3]);
     return $item;
 }
 /**
  * Add product to wishlist.
  *
  * @param App\Models\Product  $product
  * @return Cartalyst\Cart\Collections\ItemCollection
  */
 protected function addToWishlist($product)
 {
     return $this->wishlist->add(['id' => $product->id, 'name' => $product->name, 'price' => $product->price, 'quantity' => 1]);
 }
 /**
  * Store or update the item on local storage.
  *
  * @param  \Cartalyst\Cart\Collections\ItemCollection  $item
  * @param  \Cartalyst\Cart\Cart  $cart
  * @return \App\Models\CartItem
  */
 protected function storeItem(ItemCollection $item, Cart $cart)
 {
     // Get the product that was added to the shopping cart
     $product = Product::find($item->get('id'));
     // Get the cart from storage that belongs to the instance
     $_cart = $this->cart($cart->getInstance());
     // Does the product exist on storage?
     if (!($_item = $_cart->items()->whereProductId($product->id)->first())) {
         $_item = $_cart->items()->create(['product_id' => $product->id, 'quantity' => $item->get('quantity')]);
     } else {
         $_item->update(['quantity' => $item->get('quantity')]);
     }
     return $_item;
 }