| 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 |
|