| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Handlers\BlockEditors; |
| 4 |
|
| 5 |
use FluentCart\App\Modules\Templating\AssetLoader; |
| 6 |
use FluentCart\App\Services\Renderer\ProductRenderer; |
| 7 |
use FluentCart\App\Services\Translations\TransStrings; |
| 8 |
use FluentCart\App\Vite; |
| 9 |
use FluentCart\Framework\Support\Arr; |
| 10 |
use FluentCart\App\Models\Product; |
| 11 |
use FluentCart\App\Helpers\Helper; |
| 12 |
use FluentCart\App\App; |
| 13 |
use FluentCart\Api\StoreSettings; |
| 14 |
|
| 15 |
class BuySectionBlockEditor extends BlockEditor |
| 16 |
{ |
| 17 |
protected static string $editorName = 'buy-section'; |
| 18 |
|
| 19 |
public function getScripts(): array |
| 20 |
{ |
| 21 |
return [ |
| 22 |
[ |
| 23 |
'source' => 'admin/BlockEditor/BuySection/BuySectionBlockEditor.jsx', |
| 24 |
'dependencies' => ['wp-blocks', 'wp-components'] |
| 25 |
] |
| 26 |
]; |
| 27 |
} |
| 28 |
|
| 29 |
public function getStyles(): array |
| 30 |
{ |
| 31 |
return [ |
| 32 |
'admin/BlockEditor/BuySection/style/buy-section-block-editor.scss' |
| 33 |
]; |
| 34 |
} |
| 35 |
|
| 36 |
public function localizeData(): array |
| 37 |
{ |
| 38 |
return [ |
| 39 |
$this->getLocalizationKey() => [ |
| 40 |
'slug' => $this->slugPrefix, |
| 41 |
'name' => static::getEditorName(), |
| 42 |
'title' => __('Buy Section', 'fluent-cart'), |
| 43 |
'description' => __('This block will display the buy section.', 'fluent-cart'), |
| 44 |
'placeholder_image' => Vite::getAssetUrl('images/placeholder.svg'), |
| 45 |
], |
| 46 |
'fluent_cart_block_translation' => TransStrings::blockStrings(), |
| 47 |
]; |
| 48 |
} |
| 49 |
|
| 50 |
public function render(array $shortCodeAttribute, $block = null) |
| 51 |
{ |
| 52 |
AssetLoader::loadSingleProductAssets(); |
| 53 |
$product = null; |
| 54 |
$insideProductInfo = Arr::get($shortCodeAttribute, 'inside_product_info', 'no'); |
| 55 |
$queryType = Arr::get($shortCodeAttribute, 'query_type', 'default'); |
| 56 |
|
| 57 |
if ($insideProductInfo === 'yes' || $queryType === 'default') { |
| 58 |
$product = fluent_cart_get_current_product(); |
| 59 |
} else { |
| 60 |
$productId = Arr::get($shortCodeAttribute, 'product_id', false); |
| 61 |
if ($productId) { |
| 62 |
$product = Product::query()->with(['variants'])->find($productId); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
if (!$product) { |
| 69 |
return ''; |
| 70 |
} |
| 71 |
|
| 72 |
// The Buy Section renders the single-product purchase UI, including |
| 73 |
// Pro's advanced-variation selector — fire the asset hook so Pro |
| 74 |
// enqueues the selector CSS/JS (without it the selector is unstyled and |
| 75 |
// its price/variant JS never wires up). Pro dedupes by enqueue handle. |
| 76 |
do_action('fluent_cart/advanced_variation/enqueue_assets'); |
| 77 |
|
| 78 |
ob_start(); |
| 79 |
(new ProductRenderer($product))->renderBuySection(); |
| 80 |
return ob_get_clean(); |
| 81 |
} |
| 82 |
} |
| 83 |
|