PK

ADDRLIN : /home/questend/public_html/domains/evami.in/app/library/
FLL :
Current File : /home/questend/public_html/domains/evami.in/app/library/Shopcart.php

<?php
namespace app\library;

class Shopcart
{
    //CartShoping
    protected $cartName;

    // Maximum cart can added to cart, 0 = Unlimited
    protected $cartLimit = 0;
    
    // Maximum item can added to cart, 0 = Unlimited
    protected $cartMaxItem = 0;

    // Maximum quantity of a item can be added to cart, 0 = Unlimited
    protected $itemMaxQuantity = 0;

    // Do not use cookie, cart items will gone after browser closed
    protected $useCookie = false;

    private $items = [];

    public function __construct($options = [])
    {
        // Set default options and merge with provided options
        $defaultOptions = [
            'cartLimit' => 20, 
            'cartMaxItem' => 0, 
            'itemMaxQuantity' => 10, 
            'useCookie' => false
        ];
        
        $options = array_merge($defaultOptions, $options);
        
        if (!session_id()) {
            session_start();
        }
        
        if (isset($options['cartLimit']) && preg_match('/^\d+$/', (string)$options['cartLimit'])) {
            $this->cartLimit = (int)$options['cartLimit'];
        }

        if (isset($options['cartMaxItem']) && preg_match('/^\d+$/', (string)$options['cartMaxItem'])) {
            $this->cartMaxItem = (int)$options['cartMaxItem'];
        }

        if (isset($options['itemMaxQuantity']) && preg_match('/^\d+$/', (string)$options['itemMaxQuantity'])) {
            $this->itemMaxQuantity = (int)$options['itemMaxQuantity'];            
        }

        if (isset($options['useCookie']) && $options['useCookie']) {
            $this->useCookie = true;
        }

        $this->cartName = md5(($_SERVER['HTTP_HOST'] ?? 'SimpleCart')) . '_baklol';
        $this->loadCart();        
    }

    private function saveCart(): void
    {
        if ($this->useCookie) {
            setcookie($this->cartName, json_encode($this->items), time() + 604800, '/');
        } else {
            $_SESSION[$this->cartName] = json_encode($this->items);
        }
    }

    public function saveJsonCart($jsonData): void
    {
        if ($this->useCookie) {
            setcookie($this->cartName, json_encode($jsonData), time() + 604800, '/');
        } else {
            $_SESSION[$this->cartName] = json_encode($jsonData);
        }
    }      
    
    private function loadCart(): void
    {
        if ($this->useCookie) {
            $cookieData = $_COOKIE[$this->cartName] ?? '[]';
            $this->items = json_decode($cookieData, true) ?? [];
        } else {
            $sessionData = $_SESSION[$this->cartName] ?? '[]';
            $this->items = json_decode($sessionData, true) ?? [];
        }
        
        // Ensure items is always an array
        if (!is_array($this->items)) {
            $this->items = [];
        }
    } 
    
    private function ItemArray(array $data = []): array
    {
        // Set default values with proper type casting
        $productSalePrice = isset($data["product_sale_price"]) ? (float)$data["product_sale_price"] : 0.0;
        $quantity = isset($data["quantity"]) ? (int)$data["quantity"] : 0;
        
        return [
            "cartId" => $data["cartId"] ?? 'c' . md5(date('mdYhis', time())),
            "product_id" => $data["product_id"] ?? "",
            "product_name" => $data["product_name"] ?? "",
            "product_sale_price" => $productSalePrice,            
            "quantity" => $quantity,
            "component" => [
                "category_id" => $data["category_id"] ?? "",              
                "size_id" => $data["size_id"] ?? "",
                "color_id" => $data["color_id"] ?? "",
                "product_slug" => $data["product_slug"] ?? "",
            ],
            "option" => [
                "isdiscount" => isset($data["isdiscount"]) ? (int)$data["isdiscount"] : 0,
                "product_price" => isset($data["product_price"]) ? (float)$data["product_price"] : 0.0,
                "created_at" => date('Y-m-d H:i:s'),
            ],
            "images" => [
                "product_image" => $data["product_image"] ?? "",
            ],
            "total" => $productSalePrice * $quantity,
            'isdelete' => 1
        ];
    }
    
    public function addItemToCart(array $data = []): bool
    {
        if (!isset($data["quantity"], $data["product_id"], $data["size_id"])) {
            return false;
        }
        
        $quantity = preg_match('/^\d+$/', (string)$data["quantity"]) ? (int)$data["quantity"] : 1;
        $product_id = $data["product_id"];
        $size_id = $data["size_id"];
        
        if (is_numeric($product_id) && is_numeric($quantity)) {
            $cartList = $this->items;
            
            // Check if item already exists
            foreach ($cartList as $key => $value) {
                if (isset($cartList[$key]['product_id'], $cartList[$key]['component']['size_id']) && 
                    $cartList[$key]['product_id'] == $product_id && 
                    $cartList[$key]['component']['size_id'] == $size_id) {   
                    
                    $newQuantity = $cartList[$key]["quantity"] + (($quantity > $this->itemMaxQuantity && $this->itemMaxQuantity > 0) ? $this->itemMaxQuantity : $quantity);
                    $this->items[$key]["quantity"] = $newQuantity;
                    $this->items[$key]["total"] = (float)($cartList[$key]['product_sale_price'] * $newQuantity);
                    $this->saveCart();
                    return true;
                }
            }
            
            // Add new item
            if ($this->cartLimit > 0 && $this->cartLimit <= count($cartList)) {
                return false;
            } else {
                $item = $this->ItemArray($data);            
                $this->items[] = $item;           
                $this->saveCart();
                return true; 
            }
        }
        return false;
    }

    public function plusItemToCart(string $cartId, int $quantity = 1): bool
    {
        if (!empty($cartId) && is_numeric($quantity)) {         
            foreach ($this->items as $key => $value) {
                if (isset($this->items[$key]['cartId']) && $this->items[$key]['cartId'] == $cartId) {
                    $newQuantity = $this->items[$key]["quantity"] + (($quantity > $this->itemMaxQuantity && $this->itemMaxQuantity > 0) ? $this->itemMaxQuantity : $quantity);
                    $this->items[$key]["quantity"] = $newQuantity;
                    $this->items[$key]["total"] = (float)($this->items[$key]["product_sale_price"] * $newQuantity);
                    $this->saveCart();
                    return true;
                }
            }
        }
        return false;
    }

    public function setCountForItem(string $cartId, int $quantity = 1): bool
    {
        if (!empty($cartId) && is_numeric($quantity)) {
            foreach ($this->items as $key => $value) {
                if (isset($this->items[$key]['cartId']) && $this->items[$key]['cartId'] == $cartId) {
                    $this->items[$key]["quantity"] = ($this->itemMaxQuantity > 0 && $quantity > $this->itemMaxQuantity) ? $this->itemMaxQuantity : $quantity;
                    $this->items[$key]["total"] = (float)($this->items[$key]["product_sale_price"] * $this->items[$key]["quantity"]);
                    $this->saveCart();
                    return true;
                }
            }
        }
        return false;
    }

    public function removeItemFromCart(string $cartId): bool
    {
        foreach ($this->items as $key => $value) {
            if (isset($this->items[$key]['cartId']) && $this->items[$key]['cartId'] == $cartId) {
                $this->items[$key]["quantity"]--;
                $this->items[$key]["total"] = (float)($this->items[$key]["product_sale_price"] * $this->items[$key]["quantity"]);
                
                if ($this->items[$key]["quantity"] <= 0) {
                    unset($this->items[$key]);
                    // Re-index array after unset
                    $this->items = array_values($this->items);
                }
                
                $this->saveCart();
                return true;
            }
        }
        return false;
    }
       
    public function removeItemFromCartAll(string $cartId): bool
    {
        foreach ($this->items as $key => $value) {
            if (isset($this->items[$key]['cartId']) && $this->items[$key]['cartId'] == $cartId) {
                unset($this->items[$key]);
                // Re-index array after unset
                $this->items = array_values($this->items);
                $this->saveCart();
                return true;
            }
        }
        return false;
    }

    public function getSelectedCount(string $cartId): array
    {
        foreach ($this->items as $key => $value) {
            if (isset($this->items[$key]['cartId']) && $this->items[$key]['cartId'] == $cartId) {
                $count = (int)($this->items[$key]["quantity"] ?? 0);
                $amount = (float)(($this->items[$key]["product_sale_price"] ?? 0) * $count);
                return ['count' => $count, 'amount' => $amount];
            }            
        }
        return ['count' => 0, 'amount' => 0];
    }

    public function getSelectedAmount(string $cartId): float
    {
        foreach ($this->items as $key => $value) {
            if (isset($this->items[$key]['cartId']) && $this->items[$key]['cartId'] == $cartId) {
                $count = (int)($this->items[$key]["quantity"] ?? 0);
                return (float)(($this->items[$key]["product_sale_price"] ?? 0) * $count);
            }           
        }
        return 0.0;
    }

    public function clearCart(): bool
    {
        $this->items = [];
        if ($this->useCookie) {
            setcookie($this->cartName, '', time() - 3600, '/');
        } else {
            unset($_SESSION[$this->cartName]);
        }
        return true;
    }

    public function cartlItem(): int
    {        
        return count($this->items);
    }
   
    public function countCart(): int
    {
        $totalCount = 0;
        foreach ($this->items as $item) {
            $totalCount += (int)($item["quantity"] ?? 0);
        }
        return $totalCount;
    }
   
    public function totalCart(): float
    {
        $totalCost = 0.0;
        foreach ($this->items as $item) {
            $price = (float)($item["product_sale_price"] ?? 0);
            $quantity = (int)($item["quantity"] ?? 0);
            $totalCost += ($price * $quantity);
        }
        return round($totalCost, 2);
    }

    public function listCart(bool $type = false)
    {
        $cartCopy = [];
        foreach ($this->items as $item) {
            $itemCopy = $item;
            $price = (float)($item["product_sale_price"] ?? 0);
            $quantity = (int)($item["quantity"] ?? 0);
            $itemCopy["total"] = round($price * $quantity, 2); 
            $cartCopy[] = $itemCopy;
        }
        
        return $type ? json_encode($cartCopy) : $cartCopy;
    }
    
    public function getItems(): array
    {
        return $this->items;
    }
    
    public function p($q): void
    {
        echo "<pre>";
        print_r($q);
        echo "</pre>";
    }
}


PK 99
E-SHOP || DASHBOARD
404

Page Not Found

It looks like you found a glitch in the matrix...

← Back to Home