<?php
/**
* @Description : This section advances the shopping cart process
* @Author : Salar izadi
*/
final class MCE_Cart {
public string $table = "mce_cart";
/**
* @return void
* @throws Exception
*/
public function init () {
$this->ipToMember();
}
/**
* @param array $where
* @return false|mixed
* @throws ReflectionException
* @throws Exception
*/
public function get (array $where = []) {
global $ufo, $db;
$safe_where = [];
foreach ($db->columns($this->table, [], false) as $column) {
if (isset($where[$column]))
$safe_where[$column] = $where[$column];
}
if (isset($safe_where["attributes"])) {
$attributes = $safe_where["attributes"];
unset($safe_where["attributes"]);
}
$member = $ufo->get_member()["uid"] ?? false;
$safe_where = $ufo->default([
($member ? "mid" : "ip") => $member["uid"] ?? $ufo->viewer_ip()
], $safe_where);
if (!empty($attributes)) {
if ($ufo->is_json($attributes)) {
$safe_where['attributes'] = $attributes;
}
}
$rows = $db->get($this->table, $safe_where);
if (!isset($where["id"]) && !empty($attributes) && is_array($attributes)) {
$row = false;
foreach ($rows as &$item) {
if ($ufo->is_json($item["attributes"], $item["attributes"])) {
foreach ($attributes as $key => $property) {
if (isset($item["attributes"][$key])) {
if ($ufo->equal($item["attributes"][$key], $property)) {
$row = $item;
break 2;
}
}
}
}
}
$rows = $row;
}
return $rows[0] ?? $rows;
}
/**
* @param int|string $list
* @return array
* @throws Exception
*/
public function list ($list = 0): array {
global $ufo, $db;
// Prevent printing of (exerts) or (works)
ob_start(); ob_clean();
$where = [
"list" => $list
];
if ($ufo->check_login_member())
$where["mid"] = $ufo->get_member()["uid"];
else
$where["ip"] = $ufo->viewer_ip();
$products = new MCE_Products();
$rows = $db->get("mce_cart", $where);
$carts = [];
$count = 0;
foreach ($rows as $k => &$item) {
$product = $products->get((int) $item["product"]);
$remove = false;
$ufo->is_json($item["attributes"], $item["attributes"]);
$color = $item["attributes"]["color"] ?? 0;
if ($product) {
if ($product["inventory"] > 0 || ($product["type"] == 1 && $product["inventory"] == -1)) {
/** Get color */
if ($color != 0) {
foreach ($product["colors"] as $kc => $vc) {
if ($color == $vc["id"]) {
if ($vc["inv"] < $item["count"])
$remove = true;
else {
$product["color"] = array_merge(
$vc, $products->get_color($vc["id"])
);
$product["inventory"] = $vc["inv"];
}
break;
}
}
}
/** Attributes **/
$product["product_attrs"] = $product["attributes"];
$product["attributes"] = $item["attributes"];
$product["readable_attrs"] = [];
foreach ($product["attributes"] as $key => &$attr) {