PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.28
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.28
1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 All 49 releases
fluent-cart / app / Hooks / Handlers / ShortCodes / PricingTableShortCode.php

PricingTableShortCode.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.28, at app/Hooks/Handlers/ShortCodes/PricingTableShortCode.php

121 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $getActiveVariants = explode(',', Arr::get($shortcodeAttributes, 'active_variant'));
72
73 foreach ($getActiveVariants as $item) {
74 if (empty($item)) {
75 continue;
76 }
77 // Check if the item contains an '=' sign using Str::contains()
78 if (Str::contains($item, '=')) {
79 list($key, $value) = explode('=', $item);
80 $activeVariants[trim($key)] = trim($value);
81 }
82 }
83
84 return [
85 'variants' => $variants->toArray(),
86 'show_checkout_button' => Arr::get($shortcodeAttributes, 'show_checkout_button', 1),
87 'show_cart_button' => Arr::get($shortcodeAttributes, 'show_cart_button', 1),
88 'group_by' => Arr::get($shortcodeAttributes, 'group_by', 'repeat_interval'),
89 'active_tab' => Arr::get($shortcodeAttributes, 'active_tab', 0),
90 'active_variant' => $activeVariants,
91 'badge' => Arr::get($shortcodeAttributes, 'badge', ''),
92 'colors' => Arr::get($shortcodeAttributes, 'colors', ''),
93 'product_per_row' => Arr::get($shortcodeAttributes, 'product_per_row', ''),
94 'button_options' => Arr::get($shortcodeAttributes, 'button_options', ''),
95 'url_params' => Arr::get($shortcodeAttributes, 'url_params', ''),
96 'icon_visibility' => !empty(Arr::get($shortcodeAttributes, 'icon_visibility', 1)),
97 ];
98
99 }
100
101
102 public function render(?array $viewData = null)
103 {
104 AssetLoader::markFrontendAssetsRequired();
105 (new PricingTableRenderer($viewData))->render();
106 }
107
108 public function localizeData(): array
109 {
110 return [
111 'fluentcart_pricing_table_vars' => [
112 'trans' => TransStrings::singleProductPageString(),
113 'cart_button_text' => (new StoreSettings())->get('cart_button_text', __('Add To Cart', 'fluent-cart')),
114 'out_of_stock_button_text' => (new StoreSettings())->get('out_of_stock_button_text', __('Not Available', 'fluent-cart')),
115 'in_stock_status' => Helper::IN_STOCK,
116 'out_of_stock_status' => Helper::OUT_OF_STOCK,
117 ]
118 ];
119 }
120 }
121