PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.7.0
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.7.0
1.7.0 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 All 50 releases
fluent-cart / app / Hooks / Handlers / BlockEditors / Buttons / AddToCartButtonBlockEditor.php

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

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