| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Renderer; |
| 4 |
|
| 5 |
use FluentCart\App\Helpers\CartCheckoutHelper; |
| 6 |
use FluentCart\App\Helpers\Helper; |
| 7 |
use FluentCart\Framework\Support\Arr; |
| 8 |
|
| 9 |
class MiniCartRenderer |
| 10 |
{ |
| 11 |
protected $cartItems; |
| 12 |
protected $itemCount = 0; |
| 13 |
protected $countMode = 'distinct_products'; |
| 14 |
|
| 15 |
public function __construct($cartItems, $config = []) |
| 16 |
{ |
| 17 |
$this->cartItems = $cartItems; |
| 18 |
$this->itemCount = Arr::get($config, 'item_count') ?? count($this->cartItems); |
| 19 |
$this->countMode = Arr::get($config, 'count_mode', 'distinct_products'); |
| 20 |
} |
| 21 |
public function renderMiniCart($atts = []) |
| 22 |
{ |
| 23 |
$defaults = [ |
| 24 |
'is_shortcode' => false, |
| 25 |
]; |
| 26 |
|
| 27 |
$atts = wp_parse_args($atts, $defaults); |
| 28 |
|
| 29 |
$cartIcon = Arr::get($atts, 'cart_icon', 'cart'); |
| 30 |
$showItemCount = Arr::get($atts, 'show_item_count', 'has_items'); |
| 31 |
$showTotalPrice = filter_var(Arr::get($atts, 'show_total_price', true), FILTER_VALIDATE_BOOLEAN); |
| 32 |
$iconColor = Arr::get($atts, 'icon_color', ''); |
| 33 |
$priceColor = Arr::get($atts, 'price_color', ''); |
| 34 |
$productCountColor = Arr::get($atts, 'product_count_color', ''); |
| 35 |
$buttonClass = (string) Arr::get($atts, 'button_class', ''); |
| 36 |
|
| 37 |
|
| 38 |
// Build style attribute |
| 39 |
$iconStyle = $iconColor ? 'style="color: ' . esc_attr($iconColor) . ';"' : ''; |
| 40 |
$priceStyle = $priceColor ? 'style="color: ' . esc_attr($priceColor) . ';"' : ''; |
| 41 |
$countStyle = $productCountColor ? 'style="background-color: ' . esc_attr($productCountColor) . ';"' : ''; |
| 42 |
|
| 43 |
$isShortcode = Arr::get($atts, 'is_shortcode', false); |
| 44 |
$wrapperAttributes = ''; |
| 45 |
if(!$isShortcode){ |
| 46 |
$wrapperAttributes = RenderHelper::getBlockWrapperAttributes(); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
$subtotal = empty($this->cartItems) ? Helper::toDecimal(0, true) : CartCheckoutHelper::make()->getItemsAmountSubtotal(true, true); |
| 52 |
$aria_label = sprintf( |
| 53 |
/* translators: 1: Total price */ |
| 54 |
__('Total cart price: %1$s', 'fluent-cart'), |
| 55 |
esc_attr($subtotal) |
| 56 |
); |
| 57 |
|
| 58 |
$showBadge = false; |
| 59 |
if ($showItemCount === 'always') { |
| 60 |
$showBadge = true; |
| 61 |
} elseif ($showItemCount === 'has_items' && $this->itemCount > 0) { |
| 62 |
$showBadge = true; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
?> |
| 67 |
<div <?php echo $wrapperAttributes; ?>> |
| 68 |
<button class="fct-mini-cart-button <?php echo esc_attr($buttonClass); ?>" data-fluent-cart-cart-expand-button |
| 69 |
aria-label="<?php esc_attr_e('Open Shopping Cart', 'fluent-cart'); ?>"> |
| 70 |
<span class="fct-mini-cart-wrap" <?php echo $iconStyle; ?>> |
| 71 |
<?php $this->renderCartIcon($cartIcon); ?> |
| 72 |
|
| 73 |
<?php if ($showBadge) : ?> |
| 74 |
<span |
| 75 |
class="fct-mini-cart-badge" |
| 76 |
data-cart-badge-count |
| 77 |
data-cart-count-mode="<?php echo esc_attr($this->countMode); ?>" |
| 78 |
<?php echo $countStyle; ?> |
| 79 |
> |
| 80 |
<?php |
| 81 |
if ($this->itemCount > 0) { |
| 82 |
echo esc_html($this->itemCount); |
| 83 |
} else { |
| 84 |
echo '$0.00'; |
| 85 |
} |
| 86 |
?> |
| 87 |
</span> |
| 88 |
<?php endif; ?> |
| 89 |
</span> |
| 90 |
|
| 91 |
<?php if ($showTotalPrice) : ?> |
| 92 |
<span |
| 93 |
data-fluent-cart-cart-total-price |
| 94 |
class="fct-mini-cart-amount" |
| 95 |
aria-live="polite" |
| 96 |
aria-label="<?php echo esc_attr($aria_label); ?>" |
| 97 |
<?php echo $priceStyle; ?> |
| 98 |
> |
| 99 |
<?php echo esc_html($subtotal); ?> |
| 100 |
</span> |
| 101 |
<?php endif; ?> |
| 102 |
</button> |
| 103 |
</div> |
| 104 |
<?php |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
public function renderCartIcon(string $cartIcon = 'cart') |
| 110 |
{ |
| 111 |
if (filter_var($cartIcon, FILTER_VALIDATE_URL)) { |
| 112 |
echo '<img src="' . esc_url($cartIcon) . '" alt="' . esc_attr__('Cart', 'fluent-cart') . '" class="fct-mini-cart-icon-img" />'; |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
switch ($cartIcon) { |
| 117 |
case 'cart': |
| 118 |
$this->renderShoppingCartIcon(); |
| 119 |
break; |
| 120 |
case 'bag': |
| 121 |
$this->renderShoppingBagIcon(); |
| 122 |
break; |
| 123 |
case 'bag-alt': |
| 124 |
default: |
| 125 |
$this->renderShoppingBagAltIcon(); |
| 126 |
break; |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
public function renderShoppingCartIcon() |
| 132 |
{ |
| 133 |
?> |
| 134 |
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24"> |
| 135 |
<path fill="currentColor" |
| 136 |
d="M4.00488 16V4H2.00488V2H5.00488C5.55717 2 6.00488 2.44772 6.00488 3V15H18.4433L20.4433 7H8.00488V5H21.7241C22.2764 5 22.7241 5.44772 22.7241 6C22.7241 6.08176 22.7141 6.16322 22.6942 6.24254L20.1942 16.2425C20.083 16.6877 19.683 17 19.2241 17H5.00488C4.4526 17 4.00488 16.5523 4.00488 16ZM6.00488 23C4.90031 23 4.00488 22.1046 4.00488 21C4.00488 19.8954 4.90031 19 6.00488 19C7.10945 19 8.00488 19.8954 8.00488 21C8.00488 22.1046 7.10945 23 6.00488 23ZM18.0049 23C16.9003 23 16.0049 22.1046 16.0049 21C16.0049 19.8954 16.9003 19 18.0049 19C19.1095 19 20.0049 19.8954 20.0049 21C20.0049 22.1046 19.1095 23 18.0049 23Z"></path> |
| 137 |
</svg> |
| 138 |
<?php |
| 139 |
} |
| 140 |
|
| 141 |
public function renderShoppingBagIcon() |
| 142 |
{ |
| 143 |
?> |
| 144 |
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="24" height="24"> |
| 145 |
<path d="M6.50488 2H17.5049C17.8196 2 18.116 2.14819 18.3049 2.4L21.0049 6V21C21.0049 21.5523 20.5572 22 20.0049 22H4.00488C3.4526 22 3.00488 21.5523 3.00488 21V6L5.70488 2.4C5.89374 2.14819 6.19013 2 6.50488 2ZM19.0049 8H5.00488V20H19.0049V8ZM18.5049 6L17.0049 4H7.00488L5.50488 6H18.5049ZM9.00488 10V12C9.00488 13.6569 10.348 15 12.0049 15C13.6617 15 15.0049 13.6569 15.0049 12V10H17.0049V12C17.0049 14.7614 14.7663 17 12.0049 17C9.24346 17 7.00488 14.7614 7.00488 12V10H9.00488Z"></path> |
| 146 |
</svg> |
| 147 |
<?php |
| 148 |
} |
| 149 |
|
| 150 |
public function renderShoppingBagAltIcon() |
| 151 |
{ |
| 152 |
?> |
| 153 |
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" width="24" height="24"> |
| 154 |
<path d="M7.00488 7.99966V5.99966C7.00488 3.23824 9.24346 0.999664 12.0049 0.999664C14.7663 0.999664 17.0049 3.23824 17.0049 5.99966V7.99966H20.0049C20.5572 7.99966 21.0049 8.44738 21.0049 8.99966V20.9997C21.0049 21.5519 20.5572 21.9997 20.0049 21.9997H4.00488C3.4526 21.9997 3.00488 21.5519 3.00488 20.9997V8.99966C3.00488 8.44738 3.4526 7.99966 4.00488 7.99966H7.00488ZM7.00488 9.99966H5.00488V19.9997H19.0049V9.99966H17.0049V11.9997H15.0049V9.99966H9.00488V11.9997H7.00488V9.99966ZM9.00488 7.99966H15.0049V5.99966C15.0049 4.34281 13.6617 2.99966 12.0049 2.99966C10.348 2.99966 9.00488 4.34281 9.00488 5.99966V7.99966Z"></path> |
| 155 |
</svg> |
| 156 |
|
| 157 |
<?php |
| 158 |
} |
| 159 |
} |
| 160 |
|