| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Renderer; |
| 4 |
|
| 5 |
use FluentCart\App\Helpers\Helper; |
| 6 |
use FluentCart\Framework\Support\Arr; |
| 7 |
|
| 8 |
class ShippingMethodsRender |
| 9 |
{ |
| 10 |
protected $shippingMethods = []; |
| 11 |
|
| 12 |
protected $selectedId = null; |
| 13 |
|
| 14 |
public function __construct($shippingMethods = [], $selectedId = null) |
| 15 |
{ |
| 16 |
$this->shippingMethods = $shippingMethods; |
| 17 |
$this->selectedId = $selectedId; |
| 18 |
} |
| 19 |
|
| 20 |
public function render() |
| 21 |
{ |
| 22 |
$errorId = 'shipping-methods-error'; |
| 23 |
|
| 24 |
?> |
| 25 |
<div class="fct_shipping_methods" id="shipping_methods" data-fluent-cart-checkout-page-shipping-methods-wrapper> |
| 26 |
<div class="fct_checkout_form_section" aria-describedby="<?php echo esc_attr($errorId); ?>"> |
| 27 |
<div class="fct_form_section_header"> |
| 28 |
<h4 id="shipping-methods-title" class="fct_form_section_header_label"> |
| 29 |
<?php echo esc_html__('Shipping Options', 'fluent-cart') ?> |
| 30 |
</h4> |
| 31 |
</div> |
| 32 |
<div class="fct_form_section_body"> |
| 33 |
<?php $this->renderBody(); ?> |
| 34 |
</div> |
| 35 |
<span |
| 36 |
id="<?php echo esc_attr($errorId); ?>" |
| 37 |
data-fluent-cart-checkout-page-form-error="" |
| 38 |
class="fct_form_error" |
| 39 |
role="alert" |
| 40 |
aria-live="polite" |
| 41 |
></span> |
| 42 |
</div> |
| 43 |
</div> |
| 44 |
<?php |
| 45 |
} |
| 46 |
|
| 47 |
public function renderBody() |
| 48 |
{ |
| 49 |
if (is_wp_error($this->shippingMethods)) { |
| 50 |
$this->renderEmpty($this->shippingMethods->get_error_message()); |
| 51 |
} else if ($this->shippingMethods) { |
| 52 |
$this->renderMethods(); |
| 53 |
} else { |
| 54 |
$this->renderEmptyState(); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
public function renderEmptyState() |
| 59 |
{ |
| 60 |
?> |
| 61 |
<div class="fct-empty-state" role="alert"> |
| 62 |
<?php echo esc_html__('No shipping methods available for this address.', 'fluent-cart') ?> |
| 63 |
</div> |
| 64 |
<?php |
| 65 |
} |
| 66 |
|
| 67 |
public function renderMethods() |
| 68 |
{ |
| 69 |
if (is_wp_error($this->shippingMethods)) { |
| 70 |
return; |
| 71 |
} |
| 72 |
$errorId = 'shipping-methods-error'; |
| 73 |
?> |
| 74 |
<div |
| 75 |
class="fct_shipping_methods_list" |
| 76 |
data-fluent-cart-checkout-page-shipping-method-wrapper |
| 77 |
role="radiogroup" |
| 78 |
aria-labelledby="shipping-methods-title" |
| 79 |
aria-describedby="<?php echo esc_attr($errorId); ?>" |
| 80 |
> |
| 81 |
<?php $this->renderLoader(); ?> |
| 82 |
|
| 83 |
<input type="hidden" name="fc_selected_shipping_method" value="<?php echo esc_attr($this->selectedId); ?>"> |
| 84 |
<?php foreach ($this->shippingMethods as $shippingMethod) : ?> |
| 85 |
<div class="fct_shipping_methods_item"> |
| 86 |
<input |
| 87 |
type="radio" |
| 88 |
<?php echo checked($this->selectedId, $shippingMethod->id); ?> |
| 89 |
name="fc_shipping_method" |
| 90 |
id="shipping_method_<?php echo esc_attr($shippingMethod->id); ?>" |
| 91 |
value="<?php echo esc_attr($shippingMethod->id); ?>" |
| 92 |
/> |
| 93 |
<label for="shipping_method_<?php echo esc_attr($shippingMethod->id); ?>"> |
| 94 |
<?php |
| 95 |
$description = Arr::get($shippingMethod->meta, 'description', ''); |
| 96 |
?> |
| 97 |
<?php echo esc_html($shippingMethod->title); ?> |
| 98 |
<span class="shipping-method-amount" aria-label="<?php |
| 99 |
/* translators: %s charge amount */ |
| 100 |
printf(esc_attr__('Shipping cost: %s', 'fluent-cart'), |
| 101 |
esc_html(Helper::toDecimal($shippingMethod->charge_amount))); ?>" |
| 102 |
> |
| 103 |
<?php echo esc_html(Helper::toDecimal($shippingMethod->charge_amount)); ?> |
| 104 |
</span> |
| 105 |
<span class="fct-checkmark" aria-hidden="true"></span> |
| 106 |
<?php if (!empty($description)) : ?> |
| 107 |
<small class="fct_shipping_method_description"><?php echo esc_html($description); ?></small> |
| 108 |
<?php endif; ?> |
| 109 |
</label> |
| 110 |
</div> |
| 111 |
<?php endforeach; ?> |
| 112 |
</div> |
| 113 |
<?php |
| 114 |
} |
| 115 |
|
| 116 |
public function renderEmpty($message) |
| 117 |
{ |
| 118 |
?> |
| 119 |
<div class="fct-empty-state" role="alert"> |
| 120 |
<?php echo wp_kses_post($message); ?> |
| 121 |
</div> |
| 122 |
<?php |
| 123 |
} |
| 124 |
|
| 125 |
public function renderLoader() |
| 126 |
{ |
| 127 |
?> |
| 128 |
<div class="fct_shipping_methods_loader"> |
| 129 |
<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24"> |
| 130 |
<circle cx="12" cy="12" r="10" opacity="0.2" fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="2.5"></circle> |
| 131 |
|
| 132 |
<path d="m12,2c5.52,0,10,4.48,10,10" fill="none" stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2.5"> |
| 133 |
<animateTransform attributeName="transform" attributeType="XML" type="rotate" dur="0.5s" from="0 12 12" to="360 12 12" repeatCount="indefinite"></animateTransform> |
| 134 |
</path> |
| 135 |
</svg> |
| 136 |
</div> |
| 137 |
<?php |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Get inline SVG icon for a package type. |
| 142 |
*/ |
| 143 |
public static function getPackageTypeIcon($type) |
| 144 |
{ |
| 145 |
$style = 'width="14" height="14" viewBox="0 0 16 16" fill="currentColor" style="vertical-align: -2px; opacity: 0.45;"'; |
| 146 |
|
| 147 |
$icons = [ |
| 148 |
'box' => '<svg xmlns="http://www.w3.org/2000/svg" ' . $style . '><path fill-rule="evenodd" d="M5 7a1 1 0 0 0-1 1v3a1 1 0 0 0 1 1h4a1 1 0 0 0 1-1v-3a1 1 0 0 0-1-1zm.5 3.5v-2h3v2z"></path><path fill-rule="evenodd" d="M3.315 2.45a2.25 2.25 0 0 1 1.836-.95h5.796c.753 0 1.455.376 1.872 1.002l1.22 1.828c.3.452.461.983.461 1.526v6.894a1.75 1.75 0 0 1-1.75 1.75h-9.5a1.75 1.75 0 0 1-1.75-1.75v-6.863c0-.57.177-1.125.506-1.59l1.309-1.848Zm1.836.55a.75.75 0 0 0-.612.316l-.839 1.184h3.55v-1.5zm3.599 1.5h3.599l-.778-1.166a.75.75 0 0 0-.624-.334h-2.197zm4.25 1.5h-10v6.75c0 .138.112.25.25.25h9.5a.25.25 0 0 0 .25-.25z"></path></svg>', |
| 149 |
'envelope' => '<svg xmlns="http://www.w3.org/2000/svg" ' . $style . '><path fill-rule="evenodd" d="M3.75 2.5a2.75 2.75 0 0 0-2.75 2.75v5.5a2.75 2.75 0 0 0 2.75 2.75h8.5a2.75 2.75 0 0 0 2.75-2.75v-5.5a2.75 2.75 0 0 0-2.75-2.75zm-1.25 2.75c0-.69.56-1.25 1.25-1.25h8.5c.69 0 1.25.56 1.25 1.25v5.5c0 .69-.56 1.25-1.25 1.25h-8.5c-.69 0-1.25-.56-1.25-1.25zm2.067.32a.75.75 0 0 0-.634 1.36l3.538 1.651c.335.156.723.156 1.058 0l3.538-1.651a.75.75 0 0 0-.634-1.36l-3.433 1.602z"></path></svg>', |
| 150 |
'soft_package' => '<svg xmlns="http://www.w3.org/2000/svg" ' . $style . '><path d="M4 7.5a.75.75 0 0 0 0 1.5h1a.75.75 0 0 0 0-1.5z"></path><path d="M3.25 10.5a.75.75 0 0 1 .75-.75h3a.75.75 0 0 1 0 1.5h-3a.75.75 0 0 1-.75-.75"></path><path fill-rule="evenodd" d="M3.25 2a2.75 2.75 0 0 0-2.75 2.75v6.5a2.75 2.75 0 0 0 2.75 2.75h9.5a2.75 2.75 0 0 0 2.75-2.75v-6.5a2.75 2.75 0 0 0-2.75-2.75zm7.184 1.5h-7.184c-.69 0-1.25.56-1.25 1.25v6.5c0 .69.56 1.25 1.25 1.25h7.184l-.5-.273a2.75 2.75 0 0 1-1.434-2.414v-3.626c0-1.006.55-1.932 1.433-2.414zm3.566 7.552v-6.104a1 1 0 0 0-1.479-.878l-1.87 1.02a1.25 1.25 0 0 0-.651 1.097v3.626c0 .457.25.878.651 1.097l1.87 1.02a1 1 0 0 0 1.479-.878"></path></svg>', |
| 151 |
]; |
| 152 |
|
| 153 |
return $icons[$type] ?? $icons['box']; |
| 154 |
} |
| 155 |
} |
| 156 |
|