| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Handlers\ShortCodes; |
| 4 |
|
| 5 |
use FluentCart\Api\StoreSettings; |
| 6 |
use FluentCart\App\App; |
| 7 |
use FluentCart\App\Models\Product; |
| 8 |
use FluentCart\App\Modules\Templating\AssetLoader; |
| 9 |
use FluentCart\App\Services\Renderer\ProductCardRender; |
| 10 |
use FluentCart\Framework\Support\Arr; |
| 11 |
|
| 12 |
class ProductCardShortCode extends ShortCode |
| 13 |
{ |
| 14 |
const SHORT_CODE = 'fluent_cart_product_card'; |
| 15 |
protected static string $shortCodeName = 'fluent_cart_product_card'; |
| 16 |
|
| 17 |
public static function register() |
| 18 |
{ |
| 19 |
parent::register(); |
| 20 |
add_action('wp_enqueue_scripts', function () { |
| 21 |
if (App::request()->get('action') === 'elementor') { |
| 22 |
return; |
| 23 |
} |
| 24 |
if (has_shortcode(get_the_content(), static::SHORT_CODE) || has_block('fluent-cart/product-card')) { |
| 25 |
AssetLoader::loadSingleProductAssets(); |
| 26 |
} |
| 27 |
}, 10); |
| 28 |
} |
| 29 |
|
| 30 |
public function viewData(): ?array |
| 31 |
{ |
| 32 |
$productId = Arr::get($this->shortCodeAttributes, 'product_id'); |
| 33 |
$priceFormat = Arr::get($this->shortCodeAttributes, 'price_format', 'starts_from'); |
| 34 |
$cardWidth = Arr::get($this->shortCodeAttributes, 'card_width', 216); |
| 35 |
$renderAsAnchor = Arr::get($this->shortCodeAttributes, 'render_as_anchor', false); |
| 36 |
|
| 37 |
$product = Product::query() |
| 38 |
->with([ |
| 39 |
'detail', |
| 40 |
'variants' => function ($query) { |
| 41 |
$query->with(['media']) |
| 42 |
->orderBy('serial_index', 'ASC'); |
| 43 |
} |
| 44 |
]) |
| 45 |
->where('ID', $productId)->first(); |
| 46 |
|
| 47 |
return [ |
| 48 |
'placeholder_image' => '', |
| 49 |
'cart_id' => $productId, |
| 50 |
'product' => $product, |
| 51 |
'store_settings' => new StoreSettings(), |
| 52 |
'price_format' => $priceFormat, |
| 53 |
'card_width' => $cardWidth, |
| 54 |
'wrapper_class' => 'single-product-card', |
| 55 |
'render_as_anchor' => $renderAsAnchor, |
| 56 |
]; |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
public function render(?array $viewData = null) |
| 61 |
{ |
| 62 |
//ProductCardRender:: |
| 63 |
$product = Arr::get($viewData, 'product'); |
| 64 |
if (empty($product)) { |
| 65 |
echo '<p>' . esc_html__('no content', 'fluent-cart') . '</p>'; |
| 66 |
} else { |
| 67 |
(new ProductCardRender($product, [ |
| 68 |
'price_format' => Arr::get($viewData, 'price_format'), |
| 69 |
'card_width' => Arr::get($viewData, 'card_width'), |
| 70 |
'wrapper_class' => Arr::get($viewData, 'wrapper_class'), |
| 71 |
'render_as_anchor' => Arr::get($viewData, 'render_as_anchor'), |
| 72 |
]))->render(); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
} |
| 77 |
|