$productId, "QUANTITY" => $quantity], ["LID" => Config::basketSiteId()])->isSuccess(); } if ($quantity === 0) { $item->delete(); } elseif (!$item->setField("QUANTITY", $quantity)->isSuccess()) { return false; } return $basket->save()->isSuccess(); } public static function addMany(BasketBase $basket, array $items): bool { $ok = true; foreach ($items as $productId => $quantity) { $item = self::findItem($basket, (int)$productId); $current = $item === null ? 0 : (int)$item->getQuantity(); $ok = self::setQuantity($basket, (int)$productId, $current + (int)$quantity) && $ok; } return $ok; } public static function parseShare(string $code): array { if (!preg_match('/^\d{1,9}-\d{1,2}(\.\d{1,9}-\d{1,2}){0,' . (self::SHARE_MAX_ITEMS - 1) . '}$/', $code)) { return []; } $items = []; foreach (explode(".", $code) as $pair) { [$productId, $quantity] = array_map("intval", explode("-", $pair)); if ($productId > 0 && $quantity > 0) { $items[$productId] = min(self::MAX_QUANTITY, ($items[$productId] ?? 0) + $quantity); } } return $items; } public static function shared(array $items): array { $productIds = array_keys($items); $products = Related::summariesById($productIds); $prices = Prices::optimal(array_keys($products), Config::basketSiteId()); $meta = SearchMeta::get($productIds); $images = Media::thumbs($productIds, self::THUMB_WIDTH, self::THUMB_HEIGHT); $list = []; $sum = 0.0; $currency = ""; foreach ($items as $productId => $quantity) { if (!isset($products[$productId], $prices[$productId])) { continue; } $price = $prices[$productId]; $currency = $price["currency"]; $sum += $price["price"] * $quantity; $list[] = [ "id" => $productId, "name" => $products[$productId]["name"], "url" => $products[$productId]["url"], "brand" => (string)($meta[$productId]["BRAND"] ?? ""), "image" => (string)($images[$productId] ?? ""), "quantity" => $quantity, "sum" => Money::format($price["price"] * $quantity, $currency), ]; } return [ "items" => $list, "code" => implode(".", array_map(static fn(array $row): string => $row["id"] . "-" . $row["quantity"], $list)), "total" => $list ? Money::format($sum, $currency) : "", ]; } public static function clear(BasketBase $basket): bool { foreach ($basket as $item) { if (!$item->isDelay()) { $item->delete(); } } return $basket->save()->isSuccess(); } public static function addCoupon(string $code): bool { DiscountCouponsManager::init(); return DiscountCouponsManager::add($code); } public static function removeCoupon(string $code): bool { DiscountCouponsManager::init(); return DiscountCouponsManager::delete($code); } public static function state(BasketBase $basket): array { $items = []; $count = 0; foreach ($basket as $item) { if (!$item->canBuy() || $item->isDelay()) { continue; } $productId = (int)$item->getProductId(); $quantity = (int)$item->getQuantity(); $items[$productId] = ($items[$productId] ?? 0) + $quantity; $count += $quantity; } return ["count" => $count, "items" => (object)$items]; } public static function view(BasketBase $basket, bool $refresh = true): array { DiscountCouponsManager::init(); if ($refresh) { $basket->refresh(RefreshFactory::create(RefreshFactory::TYPE_FULL)); $basket->save(); } self::applyDiscounts($basket); $productIds = []; foreach ($basket as $item) { if (!$item->isDelay()) { $productIds[] = (int)$item->getProductId(); } } $products = Related::summariesById($productIds); $meta = SearchMeta::get($productIds); $images = Media::thumbs($productIds, self::THUMB_WIDTH, self::THUMB_HEIGHT); $currency = ""; $items = []; $count = 0; $lines = 0; $sum = 0.0; $base = 0.0; foreach ($basket as $item) { if ($item->isDelay()) { continue; } $productId = (int)$item->getProductId(); $quantity = (int)$item->getQuantity(); $price = (float)$item->getPrice(); $basePrice = max($price, (float)$item->getBasePrice()); $currency = (string)$item->getCurrency(); $available = $item->canBuy(); if ($available) { $lines++; $count += $quantity; $sum += $price * $quantity; $base += $basePrice * $quantity; } $items[] = [ "id" => $productId, "name" => (string)($products[$productId]["name"] ?? $item->getField("NAME")), "url" => (string)($products[$productId]["url"] ?? ""), "meta" => (string)($meta[$productId]["META"] ?? ""), "brand" => (string)($meta[$productId]["BRAND"] ?? ""), "image" => (string)($images[$productId] ?? ""), "quantity" => $quantity, "available" => $available, "price" => Money::format($price, $currency), "basePrice" => $basePrice > $price ? Money::format($basePrice, $currency) : "", "sum" => Money::format($price * $quantity, $currency), ]; } if ($currency === "") { $currency = Loader::includeModule("currency") ? (string)CurrencyManager::getBaseCurrency() : ""; } return [ "count" => $count, "summary" => Loc::getMessagePlural("VINO_CART_LINES", $lines, ["#COUNT#" => $lines]) . ", " . Loc::getMessagePlural("VINO_CART_BOTTLES", $count, ["#COUNT#" => $count]), "items" => $items, "base" => Money::format($base, $currency), "discount" => $base > $sum ? "\u{2212}" . Money::format($base - $sum, $currency) : "", "total" => Money::format($sum, $currency), "coupons" => self::coupons(), ]; } private static function coupons(): array { $coupons = []; foreach ((array)DiscountCouponsManager::get(true, [], true, true) as $coupon) { $coupons[] = [ "code" => (string)$coupon["COUPON"], "applied" => (int)$coupon["STATUS"] === DiscountCouponsManager::STATUS_APPLYED, ]; } return $coupons; } private static function applyDiscounts(BasketBase $basket): void { $discount = Discount::buildFromBasket($basket, new FuserContext((int)$basket->getFUserId(true))); if ($discount === null) { return; } $result = $discount->calculate(); if ($result->isSuccess()) { $data = $result->getData(); if (isset($data["BASKET_ITEMS"])) { $basket->applyDiscount($data["BASKET_ITEMS"]); } } } private static function findItem(BasketBase $basket, int $productId): ?BasketItemBase { foreach ($basket as $item) { if ((int)$item->getProductId() === $productId && !$item->isDelay()) { return $item; } } return null; } }