items(); if ($this->has($item_info)) { $this->update($item_info); } else { $old_items[] = $item_info; Session::put('wishlist_items',$old_items); } return $item_info; } private function update(array $item,$quantity =1) { $single = $this->existing_item_index($item); $existing_items = $this->items(); $existing_item = $existing_items[$single]; $existing_items[$single]['quantity'] = $existing_item['quantity'] + $quantity; $this->session_update($existing_items); } public function updateWishlist(array $item_index,array $quantity) { $existing_items = $this->items(); foreach ($item_index as $index => $item){ if (isset($existing_items[$item])){ $existing_items[$item]['quantity'] = $quantity[$index]; } } $this->session_update($existing_items); } public function remove($index) { $existing_items = $this->items(); unset($existing_items[$index]); $this->session_update($existing_items); } public function subtotal() { $all_items = $this->items(); $subtotal = 0; foreach ($all_items as $item) { $price_with_variant = 0; if (isset($item['variant']) && !empty($item['variant'])) { $variants = current(json_decode($item['variant'])); $variant = get_product_variant_list_by_id($variants->variantID); if (!empty($variant)) { $index = array_search($variants->term, json_decode($variant->terms, true)); $prices = json_decode($variant->price) ?? []; if (isset($prices[$index]) && !empty($prices[$index])) { $price_with_variant = $prices[$index]; } } } $subtotal += $price_with_variant; $subtotal += $item['price'] * $item['quantity']; } return $subtotal; } public function total() { $total_amount = $this->subtotal(); $total_amount -= $this->coupon(); if ($this->is_tax_enable()) { $total_amount += $this->cartTax(); } if ($this->is_shipping_available()) { $total_amount += $this->cartShipping(); } return $total_amount; } public function tax() { } public function shipping() { } public function coupon() { $get_coupon_discount = session()->get('coupon_discount'); $return_val = 0; if (!empty($get_coupon_discount)) { $coupon_details = \App\ProductCoupon::where('code', $get_coupon_discount)->first(); if ($coupon_details->discount_type === 'percentage') { $return_val = ($this->subtotal() / 100 ) * $coupon_details->discount; } elseif ($coupon_details->discount_type === 'amount') { $return_val = (float) $coupon_details->discount; } } return $return_val; } private function has($item) { if (is_null($this->items())){ return false; } $result = array_filter($this->items(),function ($cart_item) use ($item){ if (isset($item['variant']) && !empty($item['variant'])){ return $item['id'] === $cart_item['id'] && $item['variant'] === $cart_item['variant']; } return $item['id'] === $cart_item['id']; }); return $result; } public function count(): int { $items = is_null($this->items()) ? [] : $this->items(); return array_sum(array_column($items, 'quantity')); } public function items() { return Session::get('wishlist_items'); } private function session_update($items) { Session::put('wishlist_items',$items ); } private function existing_item_index(array $item) { foreach ($this->items() as $index => $cart_item){ if (isset($item['variant']) && !empty($item['variant']) && $item['id'] === $cart_item['id']){ $old_terms = json_decode($cart_item['variant'],true); $new_terms = json_decode($item['variant'],true); if (current($old_terms)['term'] === current($new_terms)['term']){ return $index; } }elseif ($item['id'] === $cart_item['id'] && empty($item['variant'])){ return $index; } } } public function wishlistTable(): string { $output = ''; $all_cart_item = $this->items(); if (!is_null($all_cart_item)) { $output = '
'; return $output; } return '