| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Handlers\ShortCodes; |
| 4 |
|
| 5 |
use FluentCart\Api\StoreSettings; |
| 6 |
use FluentCart\App\App; |
| 7 |
use FluentCart\App\Helpers\Helper; |
| 8 |
use FluentCart\App\Models\ProductVariation; |
| 9 |
use FluentCart\App\Services\Translations\TransStrings; |
| 10 |
use FluentCart\App\Vite; |
| 11 |
use FluentCart\Framework\Support\Arr; |
| 12 |
use FluentCart\Framework\Support\Str; |
| 13 |
use FluentCart\App\Modules\Templating\AssetLoader; |
| 14 |
use FluentCart\App\Services\Renderer\PricingTableRenderer; |
| 15 |
|
| 16 |
class PricingTableShortCode extends ShortCode |
| 17 |
{ |
| 18 |
const SHORT_CODE = 'fluent_cart_pricing_table'; |
| 19 |
protected static string $shortCodeName = 'fluent_cart_pricing_table'; |
| 20 |
|
| 21 |
public static function register() |
| 22 |
{ |
| 23 |
parent::register(); |
| 24 |
add_action('wp_enqueue_scripts', function () { |
| 25 |
if (App::request()->get('action') === 'elementor') { |
| 26 |
return; |
| 27 |
} |
| 28 |
if (has_shortcode(get_the_content(), static::SHORT_CODE) || has_block('fluent-cart/product-pricing-table')) { |
| 29 |
(new static())->enqueueStyles(); |
| 30 |
} |
| 31 |
},10); |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
public function getScripts(): array |
| 36 |
{ |
| 37 |
return [ |
| 38 |
[ |
| 39 |
'source' => 'public/pricing-table/PricingTable.js', |
| 40 |
'dependencies' => [], |
| 41 |
'inFooter' => true |
| 42 |
] |
| 43 |
]; |
| 44 |
} |
| 45 |
|
| 46 |
public function getStyles(): array |
| 47 |
{ |
| 48 |
return [ |
| 49 |
'public/pricing-table/pricing-table.scss' |
| 50 |
]; |
| 51 |
} |
| 52 |
|
| 53 |
public function viewData(): ?array |
| 54 |
{ |
| 55 |
$shortcodeAttributes = $this->shortCodeAttributes; |
| 56 |
|
| 57 |
|
| 58 |
$variantIds = Arr::get($shortcodeAttributes, 'variant_ids', ''); |
| 59 |
$variantIds = explode(',', $variantIds); |
| 60 |
$variants = ProductVariation::query() |
| 61 |
->whereIn('id', $variantIds) |
| 62 |
->with('product.licensesMeta') |
| 63 |
->get() |
| 64 |
->map(function (ProductVariation $productVariation) { |
| 65 |
$productVariation = $productVariation->toArray(); |
| 66 |
$productVariation['price'] = $productVariation['formatted_total']; |
| 67 |
return $productVariation; |
| 68 |
}); |
| 69 |
|
| 70 |
$activeVariants = []; |
| 71 |
$activeVariantItems = array_filter(explode(',', Arr::get($shortcodeAttributes, 'active_variant', ''))); |
| 72 |
|
| 73 |
foreach ($activeVariantItems as $item) { |
| 74 |
if (Str::contains($item, '=')) { |
| 75 |
list($key, $value) = explode('=', $item); |
| 76 |
$activeVariants[trim($key)] = trim($value); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return [ |
| 81 |
'variants' => $variants->toArray(), |
| 82 |
'show_checkout_button' => Arr::get($shortcodeAttributes, 'show_checkout_button', 1), |
| 83 |
'show_cart_button' => Arr::get($shortcodeAttributes, 'show_cart_button', 1), |
| 84 |
'group_by' => Arr::get($shortcodeAttributes, 'group_by', 'repeat_interval'), |
| 85 |
'active_tab' => Arr::get($shortcodeAttributes, 'active_tab', 0), |
| 86 |
'active_variant' => $activeVariants, |
| 87 |
'badge' => Arr::get($shortcodeAttributes, 'badge', ''), |
| 88 |
'colors' => Arr::get($shortcodeAttributes, 'colors', ''), |
| 89 |
'product_per_row' => Arr::get($shortcodeAttributes, 'product_per_row', ''), |
| 90 |
'button_options' => Arr::get($shortcodeAttributes, 'button_options', ''), |
| 91 |
'url_params' => Arr::get($shortcodeAttributes, 'url_params', ''), |
| 92 |
'icon_visibility' => !empty(Arr::get($shortcodeAttributes, 'icon_visibility', 1)), |
| 93 |
]; |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
public function render(?array $viewData = null) |
| 99 |
{ |
| 100 |
AssetLoader::markFrontendAssetsRequired(); |
| 101 |
(new PricingTableRenderer($viewData))->render(); |
| 102 |
} |
| 103 |
|
| 104 |
public function localizeData(): array |
| 105 |
{ |
| 106 |
return [ |
| 107 |
'fluentcart_pricing_table_vars' => [ |
| 108 |
'trans' => TransStrings::singleProductPageString(), |
| 109 |
'cart_button_text' => (new StoreSettings())->get('cart_button_text', __('Add To Cart', 'fluent-cart')), |
| 110 |
'out_of_stock_button_text' => (new StoreSettings())->get('out_of_stock_button_text', __('Not Available', 'fluent-cart')), |
| 111 |
'in_stock_status' => Helper::IN_STOCK, |
| 112 |
'out_of_stock_status' => Helper::OUT_OF_STOCK, |
| 113 |
] |
| 114 |
]; |
| 115 |
} |
| 116 |
} |
| 117 |
|