| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Handlers\BlockEditors; |
| 4 |
|
| 5 |
use FluentCart\App\Modules\Templating\AssetLoader; |
| 6 |
use FluentCart\App\Services\Renderer\ProductCardRender; |
| 7 |
use FluentCart\App\Services\Renderer\ProductRenderer; |
| 8 |
use FluentCart\App\Services\TemplateService; |
| 9 |
use FluentCart\App\Services\Translations\TransStrings; |
| 10 |
use FluentCart\App\Vite; |
| 11 |
use FluentCart\Framework\Support\Arr; |
| 12 |
use FluentCart\App\Models\Product; |
| 13 |
|
| 14 |
class PriceRangeBlockEditor extends BlockEditor |
| 15 |
{ |
| 16 |
protected static string $editorName = 'price-range'; |
| 17 |
|
| 18 |
public function ancestor(): array |
| 19 |
{ |
| 20 |
return [ |
| 21 |
'fluent-cart/products', |
| 22 |
'fluent-cart/product-carousel', |
| 23 |
'fluent-cart/product-info', |
| 24 |
'fluent-cart/related-product', |
| 25 |
]; |
| 26 |
} |
| 27 |
|
| 28 |
public function supports(): array |
| 29 |
{ |
| 30 |
return [ |
| 31 |
'html' => false, |
| 32 |
'align' => true, |
| 33 |
'typography' => [ |
| 34 |
'fontSize' => true, |
| 35 |
'lineHeight' => true, |
| 36 |
'__experimentalFontFamily' => true, |
| 37 |
'__experimentalFontWeight' => true, |
| 38 |
'__experimentalFontStyle' => true, |
| 39 |
'__experimentalTextTransform' => true, |
| 40 |
'__experimentalTextDecoration' => true, |
| 41 |
'__experimentalLetterSpacing' => true, |
| 42 |
'__experimentalDefaultControls' => [ |
| 43 |
'fontSize' => true, |
| 44 |
'lineHeight' => true, |
| 45 |
'fontWeight' => true, |
| 46 |
], |
| 47 |
], |
| 48 |
'color' => [ |
| 49 |
'text' => true, |
| 50 |
'background' => true, |
| 51 |
'link' => true, |
| 52 |
'gradients' => true, |
| 53 |
], |
| 54 |
'spacing' => [ |
| 55 |
'margin' => true, |
| 56 |
'padding' => true, |
| 57 |
], |
| 58 |
'__experimentalBorder' => [ |
| 59 |
'color' => true, |
| 60 |
'radius' => true, |
| 61 |
'style' => true, |
| 62 |
'width' => true, |
| 63 |
], |
| 64 |
'shadow' => true, |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
public function getScripts(): array |
| 69 |
{ |
| 70 |
return [ |
| 71 |
[ |
| 72 |
'source' => 'admin/BlockEditor/PriceRange/PriceRangeBlockEditor.jsx', |
| 73 |
'dependencies' => ['wp-blocks', 'wp-components'] |
| 74 |
] |
| 75 |
]; |
| 76 |
} |
| 77 |
|
| 78 |
public function getStyles(): array |
| 79 |
{ |
| 80 |
return [ |
| 81 |
'admin/BlockEditor/PriceRange/style/price-range-block-editor.scss' |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
public function localizeData(): array |
| 86 |
{ |
| 87 |
return [ |
| 88 |
$this->getLocalizationKey() => [ |
| 89 |
'slug' => $this->slugPrefix, |
| 90 |
'name' => static::getEditorName(), |
| 91 |
'title' => __('Price Range', 'fluent-cart'), |
| 92 |
'description' => __('This block will display the price range.', 'fluent-cart'), |
| 93 |
'placeholder_image' => Vite::getAssetUrl('images/placeholder.svg') |
| 94 |
], |
| 95 |
'fluent_cart_block_translation' => TransStrings::blockStrings(), |
| 96 |
]; |
| 97 |
} |
| 98 |
|
| 99 |
public function useContext() |
| 100 |
{ |
| 101 |
return ['fluent-cart/price_format']; |
| 102 |
} |
| 103 |
|
| 104 |
public function render(array $shortCodeAttribute, $block = null) |
| 105 |
{ |
| 106 |
AssetLoader::loadSingleProductAssets(); |
| 107 |
$product = null; |
| 108 |
$insideProductInfo = Arr::get($shortCodeAttribute, 'inside_product_info', 'no'); |
| 109 |
$queryType = Arr::get($shortCodeAttribute, 'query_type', 'default'); |
| 110 |
|
| 111 |
if ($insideProductInfo === 'yes' || $queryType === 'default') { |
| 112 |
$product = fluent_cart_get_current_product(); |
| 113 |
} |
| 114 |
|
| 115 |
if (!$product) { |
| 116 |
$productId = Arr::get($shortCodeAttribute, 'product_id', false); |
| 117 |
if ($productId) { |
| 118 |
$product = Product::query()->with(['detail', 'variants'])->find($productId); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
if (!$product) { |
| 123 |
return ''; |
| 124 |
} |
| 125 |
|
| 126 |
$defaultPriceFormat = 'starts_from'; |
| 127 |
if (is_singular('fluent-products') && TemplateService::getCurrentFcPageType() === 'single_product') { |
| 128 |
$defaultPriceFormat = 'range'; |
| 129 |
} |
| 130 |
// Context from parent (ShopApp) takes priority, then block's own attribute, then default |
| 131 |
$attrPriceFormat = Arr::get($shortCodeAttribute, 'price_format', $defaultPriceFormat); |
| 132 |
$priceFormat = $block ? Arr::get($block->context ?? [], 'fluent-cart/price_format', $attrPriceFormat) : $attrPriceFormat; |
| 133 |
|
| 134 |
$wrapper_attributes = get_block_wrapper_attributes([ |
| 135 |
'class' => 'fct-product-card-prices', |
| 136 |
]); |
| 137 |
|
| 138 |
$render = new ProductCardRender($product, [ |
| 139 |
'price_format' => $priceFormat, |
| 140 |
]); |
| 141 |
ob_start(); |
| 142 |
$render->renderPrices($wrapper_attributes); |
| 143 |
return ob_get_clean(); |
| 144 |
} |
| 145 |
} |
| 146 |
|