get_cart_contents_count() : 0; } public static function subtotal(): string { $cart = self::cart(); return $cart ? (string) $cart->get_cart_subtotal() : (function_exists('wc_price') ? (string) wc_price(0) : '0'); } public static function count_html(?int $count = null): string { $count = $count ?? self::count(); /* translators: %d: number of items in the cart */ $label = sprintf(_n('%d item', '%d items', $count, 'zadex'), $count); return '' . $count . ''; } public static function subtotal_html(?string $subtotal = null): string { return '' . wp_kses_post($subtotal ?? self::subtotal()) . ''; } /** @param array $fragments */ public static function fragments($fragments): array { $fragments = is_array($fragments) ? $fragments : []; $fragments['span.zx-menu-cart__count'] = self::count_html(); $fragments['span.zx-menu-cart__subtotal'] = self::subtotal_html(); return $fragments; } /** WooCommerce mini cart (the `div.widget_shopping_cart_content` fragment target). */ public static function mini_cart(): string { if (!function_exists('woocommerce_mini_cart') || !self::cart()) { return '
'; } ob_start(); woocommerce_mini_cart(); return '
' . ob_get_clean() . '
'; } /** @return \WC_Cart|null */ public static function cart(): ?object { if (!function_exists('WC')) { return null; } $wc = WC(); if (is_object($wc) && !isset($wc->cart) && Product::previewing() && function_exists('wc_load_cart')) { wc_load_cart(); // REST previews: WooCommerce only loads the cart for frontend requests } return is_object($wc) && isset($wc->cart) && is_object($wc->cart) ? $wc->cart : null; } }