PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.5
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.5
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 / Buttons / DirectCheckoutShortcode.php

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

127 lines 3.7 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\Buttons;
4
5 use FluentCart\App\Hooks\Handlers\ShortCodes\ShortCode;
6 use FluentCart\App\Http\Routes\WebRoutes;
7 use FluentCart\App\Modules\Templating\AssetLoader;
8 use FluentCart\App\Helpers\Helper;
9 use FluentCart\App\Models\Product;
10 use FluentCart\App\Models\ProductVariation;
11 use FluentCart\App\Services\Renderer\ProductRenderer;
12 use FluentCart\Framework\Support\Arr;
13
14 class DirectCheckoutShortcode extends ShortCode
15 {
16 protected static string $shortCodeName = 'fluent_cart_checkout_button';
17
18 public function getStyles(): array
19 {
20 return [
21 'public/buttons/direct-checkout/style/style.scss'
22 ];
23 }
24
25 /*
26 * Example:
27 * [fluent_cart_checkout_button
28 * button_text="Buy Now"
29 * variation_id="123"
30 * instant_checkout="yes"
31 * ]
32 *
33 * Supported attributes:
34 * - button_text
35 * - variation_id
36 * - target
37 * - instant_checkout (yes|no)
38 */
39 public function render(?array $viewData = null)
40 {
41 $data = $viewData ?? $this->shortCodeAttributes ?? [];
42 $buttonText = Arr::get($data, 'button_text', __('Buy Now', 'fluent-cart'));
43 $variationId = (int) Arr::get($data, 'variation_id', 0);
44 $target = Arr::get($data, 'target', '');
45 $buttonClass = trim(Arr::get($data, 'class', ''));
46
47 $instantCheckout = Arr::get($data, 'instant_checkout', false);
48 if (is_string($instantCheckout)) {
49 $instantCheckout = in_array(strtolower($instantCheckout), ['1', 'true', 'yes', 'on'], true);
50 }
51 $instantCheckout = (bool)$instantCheckout;
52
53 if ($instantCheckout) {
54 add_action('wp_footer', function () {
55 WebRoutes::renderModalCheckout();
56 AssetLoader::loadModalCheckoutAssets();
57 });
58 }
59
60 if (!$variationId) {
61 return '';
62 }
63
64 AssetLoader::loadSingleProductAssets();
65
66 $rendererConfig = [];
67 $product = null;
68
69 // 1) Try treating variation_id as the actual variation primary ID
70 $variant = ProductVariation::query()->find($variationId);
71
72 // 2) Fallback: treat variation_id as variation post_id
73 if (!$variant) {
74 $variant = ProductVariation::query()
75 ->where('post_id', $variationId)
76 ->first();
77 }
78
79 if ($variant) {
80 /** @var Product|null $product */
81 $product = Product::query()->find($variant->post_id);
82
83 if ($product) {
84 $rendererConfig['default_variation_id'] = $variant->id;
85 }
86 }
87
88 if (!$product) {
89 if (Helper::isAdminUser()) {
90 return '<p class="fct-admin-notice">' . esc_html__('Invalid product/variant for Checkout button shortcode.', 'fluent-cart') . '</p>';
91 }
92
93 return '';
94 }
95
96 $atts = [
97 'text' => $buttonText,
98 'is_shortcode' => true,
99 'enable_modal_checkout'=> $instantCheckout,
100 'variant_ids' => [$variationId],
101 'class' => $buttonClass ?: 'fluent-cart-direct-checkout-button',
102 ];
103
104 if ($buttonClass) {
105 $atts['class'] = $buttonClass;
106 }
107
108 if ($target) {
109 $atts['target'] = $target;
110 }
111
112 return (new ProductRenderer($product, $rendererConfig))->renderBuyNowButtonBlock($atts);
113 }
114
115 protected function renderAttributes($atts = [])
116 {
117 foreach ($atts as $attr => $value) {
118 if ($value !== '') {
119 echo esc_attr($attr) . '="' . esc_attr((string)$value) . '" ';
120 } else {
121 echo esc_attr($attr) . ' ';
122 }
123 }
124 }
125
126 }
127