| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Handlers\BlockEditors; |
| 4 |
|
| 5 |
use FluentCart\App\Helpers\CurrenciesHelper; |
| 6 |
use FluentCart\App\Hooks\Handlers\ShortCodes\ProductCardShortCode; |
| 7 |
use FluentCart\App\Models\Product; |
| 8 |
use FluentCart\App\Services\Translations\TransStrings; |
| 9 |
use FluentCart\App\Vite; |
| 10 |
use FluentCart\Framework\Support\Arr; |
| 11 |
use FluentCart\App\Helpers\Helper; |
| 12 |
|
| 13 |
class ProductCardBlockEditor extends BlockEditor |
| 14 |
{ |
| 15 |
protected static string $editorName = 'product-card'; |
| 16 |
|
| 17 |
public function getScripts(): array |
| 18 |
{ |
| 19 |
return [ |
| 20 |
[ |
| 21 |
'source' => 'admin/BlockEditor/ProductCard/ProductCardBlockEditor.jsx', |
| 22 |
'dependencies' => ['wp-blocks', 'wp-components'] |
| 23 |
] |
| 24 |
]; |
| 25 |
} |
| 26 |
|
| 27 |
public function getStyles(): array |
| 28 |
{ |
| 29 |
return ['admin/BlockEditor/ProductCard/style/product-card-block-editor.scss']; |
| 30 |
} |
| 31 |
|
| 32 |
public function localizeData(): array |
| 33 |
{ |
| 34 |
$currencyCode = Helper::shopConfig('currency'); |
| 35 |
$currencySign = CurrenciesHelper::getCurrencySign($currencyCode); |
| 36 |
$currencyPosition = Helper::shopConfig('currency_position'); |
| 37 |
|
| 38 |
return [ |
| 39 |
$this->getLocalizationKey() => [ |
| 40 |
'slug' => $this->slugPrefix, |
| 41 |
'name' => static::getEditorName(), |
| 42 |
'title' => __('Product Card', 'fluent-cart'), |
| 43 |
'description' => __('This block will display the product card.', 'fluent-cart'), |
| 44 |
'placeholder_image' => Vite::getAssetUrl('images/placeholder.svg'), |
| 45 |
'currency_sign' => $currencySign, |
| 46 |
'currency_position' => $currencyPosition |
| 47 |
], |
| 48 |
'fluent_cart_block_translation' => TransStrings::blockStrings(), |
| 49 |
]; |
| 50 |
} |
| 51 |
|
| 52 |
public function render(array $shortCodeAttribute, $block = null): string |
| 53 |
{ |
| 54 |
$queryType = Arr::get($shortCodeAttribute, 'query_type', 'default'); |
| 55 |
|
| 56 |
$product = null; |
| 57 |
if ($queryType === 'default') { |
| 58 |
$product = fluent_cart_get_current_product(); |
| 59 |
// TODO: Check |
| 60 |
//$product && setup_postdata($product->ID); |
| 61 |
} else { |
| 62 |
if ($productId = Arr::get($shortCodeAttribute, 'product_id', false)) { |
| 63 |
$product = Product::query()->with(['variants'])->find($productId); |
| 64 |
if ($product) { |
| 65 |
setup_postdata($product->ID); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
if (!$product) { |
| 71 |
return ''; |
| 72 |
} |
| 73 |
|
| 74 |
$selectedProductId = 'product_id=' . $product->ID; |
| 75 |
|
| 76 |
$selectedVariantId = ''; |
| 77 |
if ($variationId = Arr::get($shortCodeAttribute, 'variant_id', false)) { |
| 78 |
$selectedVariantId = 'variant_id=' . $variationId; |
| 79 |
} |
| 80 |
|
| 81 |
$priceFormat = Arr::get($shortCodeAttribute, 'price_format', 'starts_from'); |
| 82 |
$cardWidth = Arr::get($shortCodeAttribute, 'card_width', 216); |
| 83 |
|
| 84 |
$shortcodeName = ProductCardShortCode::getShortCodeName(); |
| 85 |
return "[$shortcodeName $selectedVariantId $selectedProductId price_format=$priceFormat card_width=$cardWidth]"; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
} |
| 91 |
|