| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Cart; |
| 4 |
|
| 5 |
use FluentCart\Api\CurrencySettings; |
| 6 |
use FluentCart\Api\Resource\FrontendResource\CartResource; |
| 7 |
use FluentCart\Api\StoreSettings; |
| 8 |
use FluentCart\App\App; |
| 9 |
use FluentCart\App\Helpers\CartHelper; |
| 10 |
use FluentCart\App\Helpers\Helper; |
| 11 |
use FluentCart\App\Helpers\UtmHelper; |
| 12 |
use FluentCart\App\Modules\Templating\AssetLoader; |
| 13 |
use FluentCart\App\Vite; |
| 14 |
use FluentCart\Framework\Support\Arr; |
| 15 |
use FluentCart\App\Services\Renderer\CartRenderer; |
| 16 |
use FluentCart\App\Services\Renderer\CartDrawerRenderer; |
| 17 |
|
| 18 |
class CartLoader |
| 19 |
{ |
| 20 |
public function register() |
| 21 |
{ |
| 22 |
add_action('wp_footer', [$this, 'init']); |
| 23 |
} |
| 24 |
|
| 25 |
public function init(): void |
| 26 |
{ |
| 27 |
static $loadedOnce = false; |
| 28 |
|
| 29 |
if ($loadedOnce) { |
| 30 |
return; |
| 31 |
} |
| 32 |
|
| 33 |
// Stop rendering when loaded inside iframe (frontend iframe checkout, quick-view, etc.) |
| 34 |
if ( |
| 35 |
isset($_SERVER['HTTP_SEC_FETCH_DEST']) && |
| 36 |
$_SERVER['HTTP_SEC_FETCH_DEST'] === 'iframe' |
| 37 |
) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
if (!AssetLoader::shouldLoadGlobalFrontendAssets()) { |
| 42 |
return; |
| 43 |
} |
| 44 |
|
| 45 |
$this->registerDependency(); |
| 46 |
|
| 47 |
$loadedOnce = true; |
| 48 |
|
| 49 |
$enableNavFloatingButton = apply_filters('fluent_cart/buttons/enable_floating_cart_button', true, []); |
| 50 |
|
| 51 |
if (!$enableNavFloatingButton) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
if (self::shouldHideCartDrawer()) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
static::initCartDrawer(); |
| 60 |
|
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
public static function initCartDrawer(): void |
| 65 |
{ |
| 66 |
static $loadedOnce = false; |
| 67 |
if ($loadedOnce) { |
| 68 |
return; |
| 69 |
} |
| 70 |
$loadedOnce = true; |
| 71 |
$cart = CartHelper::getCart(null, false); |
| 72 |
$itemCount = 0; |
| 73 |
|
| 74 |
if ($cart) { |
| 75 |
$itemCount = count($cart->cart_data ?? []); |
| 76 |
} |
| 77 |
|
| 78 |
$cartItems = Arr::get(CartResource::getStatus(), 'cart_data', []); |
| 79 |
|
| 80 |
if (empty($cartItems)) { |
| 81 |
return; |
| 82 |
} |
| 83 |
|
| 84 |
(new CartDrawerRenderer($cartItems, [ |
| 85 |
'item_count' => $itemCount |
| 86 |
]))->render(); |
| 87 |
} |
| 88 |
|
| 89 |
public function registerDependency(): void |
| 90 |
{ |
| 91 |
AssetLoader::loadCartAssets(); |
| 92 |
} |
| 93 |
|
| 94 |
public function enqueueStyle() |
| 95 |
{ |
| 96 |
$app = fluentCart(); |
| 97 |
$slug = $app->config->get('app.slug'); |
| 98 |
|
| 99 |
Vite::enqueueStyle( |
| 100 |
$slug . '-fluentcart-drawer', |
| 101 |
'public/cart-drawer/cart-drawer.scss', |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
public static function shouldHideCartDrawer() |
| 106 |
{ |
| 107 |
global $post; |
| 108 |
$currentPageId = $post->ID ?? null; |
| 109 |
$storeSettings = new StoreSettings(); |
| 110 |
$cartPageId = $storeSettings->getCheckoutPageId(); |
| 111 |
$receiptPageId = $storeSettings->getReceiptPageId(); |
| 112 |
|
| 113 |
if (!CartHelper::doingInstantCheckout() && $cartPageId != $currentPageId && $receiptPageId != $currentPageId) { |
| 114 |
return false; |
| 115 |
} |
| 116 |
return true; |
| 117 |
} |
| 118 |
} |
| 119 |
|