items();
if ($this->has($item_info)) {
$this->update($item_info);
} else {
$old_items[] = $item_info;
Session::put('cart_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 updateCart(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) {
$product_details = \App\Products::select(['id','sale_price'])->find( $item['id']);
$price_with_variant = 0;
if (isset($item['variant']) && !empty($item['variant'])) {
$variants = json_decode($item['variant']);
foreach($variants as $variants){
$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 * $item['quantity'];
$subtotal += $product_details->sale_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('cart_items');
}
private function session_update($items)
{
Session::put('cart_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 cartTable(): string
{
$output = '';
$all_cart_item = $this->items();
if (!is_null($all_cart_item)) {
$output = '
';
return $output;
}
return '' . __('No Item In Cart!') . '
';
}
public function is_tax_enable() : bool
{
return get_static_option('product_tax') && get_static_option('product_tax_system') === 'exclusive';
}
private function ajaxPreloader() : string
{
return <<
HTML;
}
private function cartFooter($colspan = 0) : string
{
$ajax_preloader = $this->ajaxPreloader();
$update_cart = __('Update Cart');
$coupon_code = __('Coupon Code');
$submit = __('Submit');
return <<
|
HTML;
}
private function cartThumbnail($single_product)
{
$image_markup = render_image_markup_by_attachment_id($single_product->image, '', 'thumb');
return <<
{$image_markup}
HTML;
}
private function cartTitle($single_product,$item)
{
$route = route('frontend.products.single', $single_product->slug);
$title = $single_product->title;
$variant_markup = '';
$price_with_variant = 0;
if (!empty($item['variant'])){
foreach(json_decode($item['variant']) as $variants){
$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) ?? [];
$terms = json_decode($variant->terms) ?? [];
$variant_markup .= ''.$variant->title.'
';
$variant_markup .= '- '.$terms[$index] ?? '' ;
if (isset($prices[$index]) && !empty($prices[$index])){
$variant_markup .= ' +'. amount_with_currency_symbol($prices[$index]) .'';
$price_with_variant += $prices[$index];
}
$variant_markup .= '
';
$variant_markup .= '
';
}
}
}
$markup = <<
{$variant_markup}
HTML;
return [
'markup' => $markup,
'price_with_variant' => $price_with_variant
];
}
private function cartTaxAmount($single_product,$price_with_variant,$quantity=1)
{
$output = '';
$tax_amount = 0;
$colspan = 0;
$final_price = !empty($price_with_variant) ? $price_with_variant + $single_product->sale_price : $single_product->sale_price ;
if ($this->is_tax_enable() && get_static_option('product_tax_type') === 'individual') {
$tax_amount = ($final_price / 100) * $single_product->tax_percentage;
$output = '' . amount_with_currency_symbol($tax_amount) . '(' . $single_product->tax_percentage . '%) | ';
$colspan = 8;
}
return [
'colspan' => $colspan,
'tax_amount' => $tax_amount,
'markup' => $output,
'final_price' => $final_price
];
}
private function cartSubtotal($final_price,$item,$tax_amount)
{
$subtotal = (get_static_option('product_tax_type') === 'individual') ? ($final_price + $tax_amount) * $item['quantity'] : $final_price * $item['quantity'];
return '' . amount_with_currency_symbol($subtotal) . ' | ';
}
private function cartAction($cart_index) : string
{
return'' . $this->ajaxPreloader() . ' | ';
}
public function cartSummery() : string
{
$output = '';
$car_total = $this->items();
if ($this->count() > 0) {
$output .= '' . __('Order Summery') . '
';
$output .= '
';
$output .= ' ' . __('Subtotal') . ' | ' . amount_with_currency_symbol($this->subtotal()) . ' |
'; // subtotal done
$output .= ' ' . __('Coupon Discount') . ' | -' . amount_with_currency_symbol($this->coupon()) . ' |
';
if ($this->is_tax_enable()) {
$tax_percentage = get_static_option('product_tax_type') === 'total' ? ' (' . get_static_option('product_tax_percentage') . '%)' : '';
$output .= ' ' . __('Tax') . $tax_percentage . ' | + ' . amount_with_currency_symbol($this->cartTax()) . ' |
';
}
if ($this->is_shipping_available()) {
$output .= ' ' . __('Shipping Cost') . ' | + ' . amount_with_currency_symbol($this->cartShipping()) . ' |
';
}
$output .= ' ' . __('Total') . ' | ' . amount_with_currency_symbol($this->total()) . ' |
';
$output .= '
';
$output .= '
' . __('Process To Checkout') . '';
}
return $output;
}
private function cartTax()
{
$tax_percentage = get_static_option('product_tax_percentage') ?: 0;
$taxable_amount = $this->subtotal() - $this->coupon();
$tax_amount = ($taxable_amount / 100) * (int)$tax_percentage;
if (get_static_option('product_tax_type') === 'individual') {
$all_cart_items = $this->items();
$all_individual_tax = [];
foreach ($all_cart_items as $item) {
$product_details = \App\Products::find($item['id']);
if (empty($product_details)) {
continue;
}
$price_with_variant = $this->cartTitle($product_details,$item)['price_with_variant'];
$cart_tax_amount = $this->cartTaxAmount($product_details,$price_with_variant)['tax_amount'];
$price = $cart_tax_amount * $item['quantity'];
$all_individual_tax[] = $price;
}
$tax_amount = array_sum($all_individual_tax);
}
return $tax_amount;
}
private function is_shipping_available(): bool
{
$all_cart_item = $this->items();
$return_val = true;
$cart_item_type = !empty($all_cart_item) ? array_unique(array_column($all_cart_item,'type')) : [];
if (count($cart_item_type) === 1 && in_array('digital', $cart_item_type, true)){
$return_val = false;
}
return $return_val;
}
public function cartShipping()
{
$get_shipping_charge = session()->get('shipping_charge');
$return_val = 0;
if (!empty($get_shipping_charge)) {
$shipping_details = \App\ProductShipping::where('id', $get_shipping_charge)->first();
if (!is_null($shipping_details)){
$return_val = $shipping_details->cost;
}
}
return $this->is_shipping_available() ? $return_val : 0;
}
public function cart_tax_for_mail_template($cart_items,$order_details){
$tax_percentage = get_static_option('product_tax_percentage') ?: 0;
$cart_sub_total = $order_details->subtotal;
$get_coupon_discount = $order_details->coupon_code;
$return_val = $cart_sub_total;
if (!empty($get_coupon_discount)) {
$return_val = $cart_sub_total - (int) $order_details->coupon_discount;
}
$tax_amount = ($return_val / 100) * (int) $tax_percentage;
if (get_static_option('product_tax_type') === 'individual') {
//write code for all individual tax amount and sum all of them
$all_cart_items = $cart_items;
$all_individual_tax = [];
foreach ($all_cart_items as $item) {
$product_details = \App\Products::find($item['id']);
if (empty($product_details)) {
continue;
}
$price = $product_details->sale_price * $item['quantity'];
$tax_percentage = ($price / 100) * $product_details->tax_percentage;
$all_individual_tax[] = $tax_percentage;
}
$tax_amount = array_sum($all_individual_tax);
}
return $tax_amount;
}
}