| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Services\Renderer; |
| 4 |
|
| 5 |
use FluentCart\Api\CurrencySettings; |
| 6 |
use FluentCart\Api\StoreSettings; |
| 7 |
use FluentCart\App\Helpers\Helper; |
| 8 |
use FluentCart\App\Models\Product; |
| 9 |
use FluentCart\App\Models\ProductVariation; |
| 10 |
use FluentCart\App\Vite; |
| 11 |
use FluentCart\Framework\Support\Arr; |
| 12 |
use FluentCart\App\App; |
| 13 |
use FluentCart\Framework\Support\Str; |
| 14 |
use FluentCart\App\Helpers\CurrenciesHelper; |
| 15 |
use FluentCart\App\Hooks\Handlers\ShortCodes\Buttons\AddToCartShortcode; |
| 16 |
use FluentCart\App\Hooks\Handlers\ShortCodes\Buttons\DirectCheckoutShortcode; |
| 17 |
|
| 18 |
class PricingTableRenderer |
| 19 |
{ |
| 20 |
protected $viewData; |
| 21 |
|
| 22 |
protected $variants; |
| 23 |
|
| 24 |
protected $storeSettings; |
| 25 |
|
| 26 |
protected $sign; |
| 27 |
|
| 28 |
protected $repeatInterval = null; |
| 29 |
|
| 30 |
protected $itemPrice = null; |
| 31 |
|
| 32 |
protected $comparePrice = null; |
| 33 |
|
| 34 |
protected $paymentInfo = ''; |
| 35 |
|
| 36 |
protected $setupFeeInfo = ''; |
| 37 |
|
| 38 |
protected $featureItems = []; |
| 39 |
|
| 40 |
protected $showCartButton = 1; |
| 41 |
|
| 42 |
protected $paymentType = ''; |
| 43 |
|
| 44 |
protected $licenseEnable = 'no'; |
| 45 |
|
| 46 |
|
| 47 |
public function __construct($viewData) |
| 48 |
{ |
| 49 |
$this->viewData = $viewData; |
| 50 |
$this->variants = $this->viewData['variants']; |
| 51 |
$this->storeSettings = new StoreSettings(); |
| 52 |
|
| 53 |
$signName = $this->storeSettings->get('currency'); |
| 54 |
|
| 55 |
$this->sign = CurrenciesHelper::getCurrencySign($signName); |
| 56 |
|
| 57 |
$this->showCartButton = Arr::get($this->viewData, 'show_cart_button', 1); |
| 58 |
|
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
public function render() |
| 63 |
{ |
| 64 |
|
| 65 |
?> |
| 66 |
<div class="fluent-cart-pricing-table" data-fluent-cart-pricing-table role="region" |
| 67 |
aria-label="<?php esc_attr_e('Pricing table', 'fluent-cart'); ?>"> |
| 68 |
<div class="fluent-cart-pricing-table-variants-iterator" role="list"> |
| 69 |
<?php $this->renderVariant(); ?> |
| 70 |
</div> |
| 71 |
</div> |
| 72 |
<?php |
| 73 |
} |
| 74 |
|
| 75 |
public function renderVariant() |
| 76 |
{ |
| 77 |
|
| 78 |
|
| 79 |
foreach ($this->variants as $index => $variant) { |
| 80 |
$this->repeatInterval = Arr::get($variant, 'other_info.repeat_interval', null); |
| 81 |
|
| 82 |
$this->itemPrice = esc_html(Helper::toDecimal(Arr::get($variant, 'item_price', 0), false, false, false)); |
| 83 |
|
| 84 |
$this->comparePrice = esc_html(Helper::toDecimal(Arr::get($variant, 'compare_price', 0), false, false, false, false)); |
| 85 |
|
| 86 |
$otherInfo = Arr::get($variant, 'other_info', null); |
| 87 |
|
| 88 |
$this->paymentInfo = Helper::generateSubscriptionInfo($otherInfo, $this->itemPrice); |
| 89 |
|
| 90 |
$this->setupFeeInfo = Helper::generateSetupFeeInfo($otherInfo); |
| 91 |
|
| 92 |
$featureDescription = Arr::get($variant, 'other_info.description', ''); |
| 93 |
|
| 94 |
$this->featureItems = explode("\n", $featureDescription); |
| 95 |
|
| 96 |
$this->paymentType = Arr::get($variant, 'payment_type'); |
| 97 |
|
| 98 |
$licenseMeta = Arr::get($variant, 'product.licenses_meta.meta_value', null); |
| 99 |
|
| 100 |
$this->licenseEnable = Arr::get($licenseMeta, 'enabled', 'no'); |
| 101 |
|
| 102 |
?> |
| 103 |
|
| 104 |
<div class="fluent-cart-pricing-table-variant" data-fluent-cart-pricing-table-variant role="listitem"> |
| 105 |
|
| 106 |
<div class="fluent-cart-pricing-table-variant-contents"> |
| 107 |
<div class="fluent-cart-pricing-table-variant-title"> |
| 108 |
<?php echo esc_html(Arr::get($variant, 'product.post_title', '')); ?> |
| 109 |
</div> |
| 110 |
|
| 111 |
<div class="fluent-cart-pricing-table-variant-sub-title"> |
| 112 |
<?php echo esc_html(Arr::get($variant, 'variation_title', '')); ?> |
| 113 |
</div> |
| 114 |
|
| 115 |
<div class="variant-divider" aria-hidden="true"></div> |
| 116 |
|
| 117 |
<div class="fluent-cart-pricing-table-variant-price-wrap"> |
| 118 |
<div |
| 119 |
class="fluent-cart-pricing-table-variant-price" |
| 120 |
aria-label="<?php |
| 121 |
printf( |
| 122 |
/* translators: 1: Currency sign, 2: Item price, 3: Billing interval or unit */ |
| 123 |
esc_attr__('Price: %1$s%2$s per %3$s', 'fluent-cart'), |
| 124 |
esc_attr($this->sign), |
| 125 |
esc_attr($this->itemPrice), |
| 126 |
esc_attr($this->repeatInterval ?? __('unit', 'fluent-cart')) |
| 127 |
); |
| 128 |
?>"> |
| 129 |
<sup><?php echo esc_html($this->sign) ?></sup> |
| 130 |
|
| 131 |
<?php echo esc_html($this->itemPrice) ?> |
| 132 |
|
| 133 |
<?php if (isset($this->repeatInterval)): ?> |
| 134 |
<span class="repeat-interval"> |
| 135 |
/<?php echo esc_html($this->repeatInterval) ?> |
| 136 |
</span> |
| 137 |
<?php endif; ?> |
| 138 |
|
| 139 |
</div> |
| 140 |
|
| 141 |
<div class="fluent-cart-pricing-table-variant-compare-price"> |
| 142 |
<span><?php echo esc_html($this->sign) ?></span> |
| 143 |
<?php |
| 144 |
$aria_label = sprintf( |
| 145 |
/* translators: 1: Currency sign, 2: Compare price */ |
| 146 |
esc_attr__('Compare at: %1$s%2$s', 'fluent-cart'), |
| 147 |
$this->sign, |
| 148 |
$this->comparePrice |
| 149 |
); |
| 150 |
?> |
| 151 |
<span aria-label="<?php echo esc_attr($aria_label); ?>"> |
| 152 |
<del><?php echo esc_html($this->comparePrice); ?></del> |
| 153 |
</span> |
| 154 |
</div> |
| 155 |
</div> |
| 156 |
|
| 157 |
<div class="fluent-cart-pricing-table-variant-payment-type"> |
| 158 |
<span> <?php echo esc_html($this->paymentInfo); ?> </span> |
| 159 |
<span class="setup-fee" |
| 160 |
aria-label="<?php esc_attr_e('Setup fee information', 'fluent-cart'); ?>"> |
| 161 |
<?php echo esc_html($this->setupFeeInfo); ?> |
| 162 |
</span> |
| 163 |
</div> |
| 164 |
|
| 165 |
<div class="fluent-cart-pricing-table-variant-description"> |
| 166 |
<?php $this->renderFeatureItems(); ?> |
| 167 |
</div> |
| 168 |
|
| 169 |
</div> |
| 170 |
|
| 171 |
<div class="fluent-cart-pricing-table-variant-buttons"> |
| 172 |
<?php |
| 173 |
if ( |
| 174 |
$this->showCartButton == 1 && |
| 175 |
$this->paymentType !== 'subscription' && |
| 176 |
$this->licenseEnable !== 'yes' |
| 177 |
) { |
| 178 |
$this->renderCartButton($variant); |
| 179 |
} |
| 180 |
|
| 181 |
if (Arr::get($this->viewData, 'show_checkout_button', 1) == 1) { |
| 182 |
$this->renderDirectCheckoutButton($variant); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
?> |
| 187 |
</div> |
| 188 |
|
| 189 |
|
| 190 |
</div> |
| 191 |
|
| 192 |
<?php }; |
| 193 |
} |
| 194 |
|
| 195 |
public function renderFeatureItems() |
| 196 |
{ |
| 197 |
|
| 198 |
?> |
| 199 |
<ul class="fluent-cart-pricing-table-variant-features" role="list"> |
| 200 |
<?php |
| 201 |
foreach ($this->featureItems as $index => $featureItem) { |
| 202 |
if (!empty($featureItem)) : ?> |
| 203 |
|
| 204 |
<li class="fluent-cart-pricing-table-variant-feature" role="listitem"> |
| 205 |
<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 18 18" fill="none" |
| 206 |
aria-hidden="true"> |
| 207 |
<path d="M17.3332 9.00004C17.3332 4.39767 13.6022 0.666706 8.99984 0.666706C4.39746 0.666706 0.666504 4.39767 0.666504 9.00004C0.666504 13.6024 4.39746 17.3334 8.99984 17.3334C13.6022 17.3334 17.3332 13.6024 17.3332 9.00004Z" |
| 208 |
fill="currentColor"></path> |
| 209 |
<path d="M5.6665 9.62502C5.6665 9.62502 6.99984 10.3855 7.6665 11.5C7.6665 11.5 9.6665 7.12502 12.3332 5.66669" |
| 210 |
stroke="white" stroke-width="1.5" stroke-linecap="round" |
| 211 |
stroke-linejoin="round"></path> |
| 212 |
</svg> |
| 213 |
|
| 214 |
<?php echo esc_html($featureItem); ?> |
| 215 |
</li> |
| 216 |
|
| 217 |
<?php endif; |
| 218 |
} |
| 219 |
?> |
| 220 |
</ul> |
| 221 |
|
| 222 |
<?php |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
public function renderCartButton($variant) |
| 227 |
{ |
| 228 |
$buttonOptions = Arr::get($this->viewData, 'button_options', ''); |
| 229 |
$buttonValues = self::stringToAssocArray($buttonOptions); |
| 230 |
$buttonText = !empty($buttonValues['cartButtonText']) ? $buttonValues['cartButtonText'] : __('Add To Cart', 'fluent-cart'); |
| 231 |
|
| 232 |
if ($variant['stock_status'] === Helper::IN_STOCK) { |
| 233 |
$data = [ |
| 234 |
'add_to_cart_button_text' => $buttonText, |
| 235 |
'product_id' => Arr::get($variant, 'id', ''), |
| 236 |
'quantity' => 1, |
| 237 |
'variation_type' => Arr::get($variant, 'variation_type', ''), |
| 238 |
'payment_type' => Arr::get($variant, 'payment_type', ''), |
| 239 |
]; |
| 240 |
|
| 241 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in view template |
| 242 |
echo AddToCartShortcode::make()->enqueueAssets()->render($data); |
| 243 |
} else { |
| 244 |
$this->renderOutOfStockButton(); |
| 245 |
} |
| 246 |
|
| 247 |
} |
| 248 |
|
| 249 |
public function renderOutOfStockButton() |
| 250 |
{ |
| 251 |
?> |
| 252 |
<button |
| 253 |
class="fct-out-of-stock-button" |
| 254 |
disabled |
| 255 |
aria-disabled="true" |
| 256 |
aria-label="<?php |
| 257 |
/* translators: Button aria-label shown when a product is out of stock */ |
| 258 |
esc_attr_e('This item is out of stock and cannot be added to cart', 'fluent-cart'); |
| 259 |
?>" |
| 260 |
> |
| 261 |
<?php |
| 262 |
echo esc_html( |
| 263 |
$this->storeSettings->get( |
| 264 |
'out_of_stock_button_text', |
| 265 |
__('Not Available', 'fluent-cart') |
| 266 |
) |
| 267 |
); |
| 268 |
?> |
| 269 |
</button> |
| 270 |
<?php |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
public function renderDirectCheckoutButton($variant) |
| 275 |
{ |
| 276 |
$directCheckoutButtonText = $this->storeSettings->get('direct_checkout_button_text', __('Buy Now', 'fluent-cart')); |
| 277 |
|
| 278 |
$buttonOptions = Arr::get($this->viewData, 'button_options', ''); |
| 279 |
$buttonValues = self::stringToAssocArray($buttonOptions); |
| 280 |
$buttonText = !empty($buttonValues['text']) ? $buttonValues['text'] : $directCheckoutButtonText; |
| 281 |
|
| 282 |
$checkoutUrl = add_query_arg([ |
| 283 |
'fluent-cart' => 'instant_checkout' |
| 284 |
], site_url()); |
| 285 |
|
| 286 |
if ($variant['stock_status'] === Helper::IN_STOCK) { |
| 287 |
|
| 288 |
// Handle product parameters |
| 289 |
$productId = Arr::get($variant, 'id'); |
| 290 |
$quantity = Arr::get($this->viewData, 'quantity', 1); |
| 291 |
|
| 292 |
// Build base parameters |
| 293 |
$params = [ |
| 294 |
'item_id' => $productId, |
| 295 |
'quantity' => $quantity, |
| 296 |
]; |
| 297 |
|
| 298 |
// Merge in any additional params |
| 299 |
$extraParams = Arr::get($this->viewData, 'url_params', ''); |
| 300 |
if (!empty($extraParams)) { |
| 301 |
// If it's a query string like "foo=bar&baz=qux" |
| 302 |
parse_str($extraParams, $extraArray); |
| 303 |
$params = array_merge($params, $extraArray); |
| 304 |
} |
| 305 |
|
| 306 |
$checkoutUrl = add_query_arg($params, $checkoutUrl); |
| 307 |
|
| 308 |
$data = [ |
| 309 |
'direct_checkout_button_text' => $buttonText, |
| 310 |
'checkout_url' => $checkoutUrl, |
| 311 |
'product_id' => Arr::get($variant, 'id'), |
| 312 |
'is_pricing_table' => true, |
| 313 |
'quantity' => 1, |
| 314 |
'variation_type' => Arr::get($variant, 'variation_type', ''), |
| 315 |
'stock_availability' => Arr::get($variant, 'stock_status', ''), |
| 316 |
]; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
private static function stringToAssocArray($string): array |
| 321 |
{ |
| 322 |
$result = []; |
| 323 |
|
| 324 |
if (!empty($string)) { |
| 325 |
$pairs = explode(', ', $string); |
| 326 |
|
| 327 |
foreach ($pairs as $pair) { |
| 328 |
// Check if $pair contains '=' using Str::contains() before splitting |
| 329 |
if (Str::contains($pair, '=')) { |
| 330 |
list($key, $value) = explode('=', $pair); |
| 331 |
$result[trim($key)] = trim($value); |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
return $result; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
} |
| 341 |
|