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.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 trunk All 48 releases
fluent-cart / app / Hooks / Handlers / BlockEditors / Buttons / AddToCartButtonBlockEditor.php

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

125 lines 3.8 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\BlockEditors\Buttons;
4
5 use FluentCart\App\Helpers\Helper;
6 use FluentCart\App\Hooks\Handlers\BlockEditors\BlockEditor;
7 use FluentCart\App\Models\Product;
8 use FluentCart\App\Models\ProductVariation;
9 use FluentCart\App\Modules\Templating\AssetLoader;
10 use FluentCart\App\Services\Renderer\ProductRenderer;
11 use FluentCart\App\Services\Translations\TransStrings;
12 use FluentCart\App\Vite;
13 use FluentCart\Framework\Support\Arr;
14
15 class AddToCartButtonBlockEditor extends BlockEditor
16 {
17 protected static string $editorName = 'add-to-cart-button';
18
19
20
21 public function getScripts(): array
22 {
23 return [
24 [
25 'source' => 'admin/BlockEditor/Buttons/AddToCartButtonBlockEditor.jsx',
26 'dependencies' => ['wp-blocks', 'wp-components']
27 ]
28 ];
29 }
30
31 public function getStyles(): array
32 {
33 return [
34 'admin/BlockEditor/Buttons/style/button-block-editor.scss'
35 ];
36 }
37
38
39 public function supports(): array
40 {
41 return [
42 'html' => false,
43 'typography' => [
44 'fontSize' => true,
45 'lineHeight' => true
46 ],
47 'spacing' => [
48 'margin' => true,
49 'padding' => true
50 ],
51 'color' => [
52 'text' => true,
53 'background' => true,
54 ],
55 '__experimentalBorder' => [
56 'color' => true,
57 'radius' => true,
58 'style' => true,
59 'width' => true,
60 ],
61 'shadow' => true
62 ];
63 }
64
65 public function localizeData(): array
66 {
67 return [
68 $this->getLocalizationKey() => [
69 'slug' => $this->slugPrefix,
70 'name' => static::getEditorName(),
71 'title' => __('Add to Cart', 'fluent-cart'),
72 'description' => __('A custom button block with product selection and automatic link assignment.', 'fluent-cart'),
73 'placeholder_image' => Vite::getAssetUrl('images/placeholder.svg'),
74 ],
75 'fluent_cart_block_translation' => TransStrings::blockStrings(),
76 ];
77 }
78
79 public function render(array $shortCodeAttribute, $block = null)
80 {
81 AssetLoader::loadSingleProductAssets();
82 $variantIds = Arr::get($shortCodeAttribute, 'variant_ids', []);
83
84 $variantId = Arr::get($variantIds, 0);
85
86 if (!$variantId) {
87 if(Helper::isAdminUser()){
88 return '<p class="fct-admin-notice">' . esc_html__('No variant selected', 'fluent-cart') . '</p>';
89 }
90
91 return '';
92 }
93
94 $variant = ProductVariation::query()->find($variantId);
95
96 if (!$variant) {
97 if(Helper::isAdminUser()){
98 return '<p class="fct-admin-notice">' . esc_html__('Invalid variant', 'fluent-cart') . '</p>';
99 }
100
101 return '';
102 }
103
104 $product = Product::query()->find($variant->post_id);
105
106 if (!$product) {
107 if(Helper::isAdminUser()){
108 return '<p class="fct-admin-notice">' . esc_html__('Product not found', 'fluent-cart') . '</p>';
109 }
110
111 return '';
112 }
113
114 ob_start();
115 // Pass the block's selected variant as the renderer's default variation
116 // so the button's data-cart-id (and stock/price) reflect that variant.
117 // Without this the renderer falls back to the product's first/default
118 // variant, and the button always adds the wrong variant to the cart.
119 (new ProductRenderer($product, ['default_variation_id' => $variant->id]))
120 ->renderAddToCartButtonBlock($shortCodeAttribute);
121 return ob_get_clean();
122 }
123
124 }
125