| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Renderer; |
| 4 |
|
| 5 |
use FluentCart\Api\StoreSettings; |
| 6 |
use FluentCart\App\Helpers\CartCheckoutHelper; |
| 7 |
use FluentCart\Framework\Support\Arr; |
| 8 |
use FluentCart\App\Vite; |
| 9 |
|
| 10 |
class CartDrawerRenderer |
| 11 |
{ |
| 12 |
protected $cartItems; |
| 13 |
|
| 14 |
protected $storeSettings; |
| 15 |
|
| 16 |
protected $itemCount = 0; |
| 17 |
|
| 18 |
protected $cartRenderer = null; |
| 19 |
|
| 20 |
protected $defaultOpenCart = false; |
| 21 |
|
| 22 |
protected $isAdminBarEnabled = false; |
| 23 |
|
| 24 |
|
| 25 |
public function __construct($cartItems, $config = []) |
| 26 |
{ |
| 27 |
$this->storeSettings = new StoreSettings(); |
| 28 |
$this->cartItems = $cartItems; |
| 29 |
$this->itemCount = Arr::get($config, 'item_count') ?? 0; |
| 30 |
$this->defaultOpenCart = Arr::get($config, 'open_cart') ?? false; |
| 31 |
|
| 32 |
$this->isAdminBarEnabled = Arr::get($config, 'is_admin_bar_enabled') || is_admin_bar_showing() ?? false; |
| 33 |
|
| 34 |
|
| 35 |
$this->cartRenderer = new CartRenderer($cartItems); |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
public function render() |
| 40 |
{ |
| 41 |
?> |
| 42 |
|
| 43 |
<div class="fct-cart-drawer-container" data-fluent-cart-cart-drawer-container role="complementary" |
| 44 |
aria-label="<?php esc_attr_e('Shopping Cart Drawer', 'fluent-cart'); ?>"> |
| 45 |
<div |
| 46 |
class="fct-cart-drawer-overlay <?php echo esc_attr($this->defaultOpenCart ? 'active' : '') ?>" |
| 47 |
data-fluent-cart-cart-drawer-overlay |
| 48 |
tabindex="-1" |
| 49 |
aria-hidden="<?php echo $this->defaultOpenCart ? 'false' : 'true'; ?>" |
| 50 |
></div> |
| 51 |
|
| 52 |
<div |
| 53 |
class="fct-cart-drawer <?php echo esc_attr($this->defaultOpenCart ? 'open' : '') ?> <?php echo esc_attr($this->isAdminBarEnabled ? 'admin_bar_enabled' : '') ?>" |
| 54 |
data-fluent-cart-cart-drawer |
| 55 |
role="dialog" |
| 56 |
aria-modal="true" |
| 57 |
aria-label="<?php esc_attr_e('Shopping Cart', 'fluent-cart'); ?>" |
| 58 |
> |
| 59 |
<?php $this->renderCartLoader(); ?> |
| 60 |
|
| 61 |
<?php $this->renderHeader(); ?> |
| 62 |
|
| 63 |
<?php $this->cartRenderer->renderItems(); ?> |
| 64 |
|
| 65 |
<?php $this->renderFooter(); ?> |
| 66 |
|
| 67 |
|
| 68 |
</div> |
| 69 |
|
| 70 |
|
| 71 |
<?php $this->renderCartIconButton(); ?> |
| 72 |
|
| 73 |
|
| 74 |
</div> |
| 75 |
|
| 76 |
<?php |
| 77 |
} |
| 78 |
|
| 79 |
public function renderCartLoader() |
| 80 |
{ |
| 81 |
?> |
| 82 |
<div class="fct-loader-wrap" data-fluent-cart-cart-drawer-loader aria-hidden="true"> |
| 83 |
<div class="fct-loader-spinner"></div> |
| 84 |
</div> |
| 85 |
<?php |
| 86 |
} |
| 87 |
|
| 88 |
public function renderHeader() |
| 89 |
{ |
| 90 |
?> |
| 91 |
|
| 92 |
<div class="fct-cart-drawer-header"> |
| 93 |
<h5 class="title"> |
| 94 |
<?php esc_html_e('Shopping Cart', 'fluent-cart'); ?> |
| 95 |
<span> |
| 96 |
( |
| 97 |
<span data-fluent-cart-cart-total-item |
| 98 |
class="fluent-cart-cart-item-counter" |
| 99 |
aria-live="polite"> |
| 100 |
<?php echo esc_html($this->itemCount); ?> |
| 101 |
</span> |
| 102 |
<?php esc_html_e('Items', 'fluent-cart'); ?> |
| 103 |
) |
| 104 |
</span> |
| 105 |
</h5> |
| 106 |
|
| 107 |
<button data-fluent-cart-cart-collapse-button |
| 108 |
class="fct-cart-drawer-close-button" |
| 109 |
aria-label="<?php esc_attr_e('Close Cart', 'fluent-cart'); ?>"> |
| 110 |
<svg xmlns="http://www.w3.org/2000/svg" |
| 111 |
width="16" height="16" |
| 112 |
viewBox="0 0 16 16" |
| 113 |
aria-hidden="true" focusable="false"> |
| 114 |
<path d="M15 1L1 15M1 1L15 15" |
| 115 |
stroke="currentColor" |
| 116 |
stroke-width="1.5" |
| 117 |
stroke-linecap="round" |
| 118 |
stroke-linejoin="round"> |
| 119 |
</path> |
| 120 |
</svg> |
| 121 |
</button> |
| 122 |
</div> |
| 123 |
|
| 124 |
<?php |
| 125 |
} |
| 126 |
|
| 127 |
public function renderCartIconButton($skipIfLoaded = true) |
| 128 |
{ |
| 129 |
$showCartIconInBody = (new StoreSettings())->get('show_cart_icon_in_body'); |
| 130 |
static $loadedOnce = false; |
| 131 |
|
| 132 |
if ($loadedOnce && !$skipIfLoaded) { |
| 133 |
return ''; |
| 134 |
} |
| 135 |
|
| 136 |
if ($showCartIconInBody !== 'yes') { |
| 137 |
return ''; |
| 138 |
} |
| 139 |
?> |
| 140 |
<button class="fct-cart-drawer-open-btn <?php echo esc_attr($this->itemCount > 0 ? '' : 'is-hidden') ?>" |
| 141 |
data-fluent-cart-cart-expand-button |
| 142 |
aria-label="<?php esc_attr_e('Open Shopping Cart', 'fluent-cart'); ?>"> |
| 143 |
<img src="<?php echo esc_url(Vite::getAssetUrl('images/cart.svg')); ?>" |
| 144 |
alt="<?php esc_attr_e('Cart Icon', 'fluent-cart'); ?>"/> |
| 145 |
|
| 146 |
<?php $this->renderItemCount(); ?> |
| 147 |
</button> |
| 148 |
<?php |
| 149 |
} |
| 150 |
|
| 151 |
public function renderFooter() |
| 152 |
{ |
| 153 |
?> |
| 154 |
|
| 155 |
<div class="fct-cart-drawer-footer"> |
| 156 |
<?php $this->cartRenderer->renderTotal(); ?> |
| 157 |
|
| 158 |
<div class="fct-cart-drawer-footer-actions"> |
| 159 |
<?php |
| 160 |
$this->cartRenderer->renderCheckoutButton(); |
| 161 |
$this->cartRenderer->renderViewCartButton(); |
| 162 |
?> |
| 163 |
</div> |
| 164 |
</div> |
| 165 |
|
| 166 |
<?php |
| 167 |
} |
| 168 |
|
| 169 |
public function renderItemCount() |
| 170 |
{ |
| 171 |
?> |
| 172 |
<div class="fct-cart-badge-count" aria-live="polite"> |
| 173 |
<?php |
| 174 |
if ($this->itemCount > 0) { |
| 175 |
echo esc_html($this->itemCount); |
| 176 |
} |
| 177 |
?> |
| 178 |
</div> |
| 179 |
<?php |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
} |
| 188 |
|