PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.1
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.1
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / app / Hooks / Handlers / BlockEditors / StockBlock.php

StockBlock.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.1, at app/Hooks/Handlers/BlockEditors/StockBlock.php

100 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Hooks\Handlers\BlockEditors;
4
5
6 use FluentCart\App\Models\Product;
7 use FluentCart\App\Services\Renderer\ProductRenderer;
8 use FluentCart\App\Services\Translations\TransStrings;
9 use FluentCart\App\Vite;
10 use FluentCart\Framework\Support\Arr;
11
12 class StockBlock extends BlockEditor
13 {
14 protected static string $editorName = 'stock';
15
16 public function supports(): array
17 {
18 return [
19 'html' => false,
20 'align' => ['left', 'center', 'right'],
21 'typography' => [
22 'fontSize' => true,
23 'lineHeight' => true
24 ],
25 'spacing' => [
26 'margin' => true
27 ],
28 'color' => [
29 'text' => true,
30 ]
31 ];
32 }
33
34 public function getScripts(): array
35 {
36 return [
37 [
38 'source' => 'admin/BlockEditor/Stock/StockBlock.jsx',
39 'dependencies' => ['wp-blocks', 'wp-components', 'wp-data', 'wp-block-editor', 'wp-element']
40 ]
41 ];
42 }
43
44 public function getStyles(): array
45 {
46 // 'admin/BlockEditor/Stock/style/stock-block-editor.scss'
47 return [];
48 }
49
50
51 public function localizeData(): array
52 {
53 return [
54 $this->getLocalizationKey() => [
55 'slug' => $this->slugPrefix,
56 'name' => static::getEditorName(),
57 'title' => __('Stock', 'fluent-cart'),
58 'description' => __('This block will display the stock.', 'fluent-cart'),
59 'placeholder_image' => Vite::getAssetUrl('images/placeholder.svg'),
60 'supports' => $this->supports()
61 ],
62 'fluent_cart_block_translation' => TransStrings::blockStrings(),
63 ];
64 }
65
66 public function render(array $shortCodeAttribute, $block = null)
67 {
68 $product = null;
69 $insideProductInfo = Arr::get($shortCodeAttribute, 'inside_product_info', 'no');
70 $queryType = Arr::get($shortCodeAttribute, 'query_type', 'default');
71
72 if ($insideProductInfo === 'yes' || $queryType === 'default') {
73 $product = fluent_cart_get_current_product();
74
75 } else {
76 $productId = Arr::get($shortCodeAttribute, 'product_id', false);
77 if ($productId) {
78 $product = Product::query()->with(['variants'])->find($productId);
79 }
80 }
81
82 if (!$product) {
83 return '';
84 }
85
86 $wrapper_attributes = get_block_wrapper_attributes(
87 [
88 'class' => 'fct-product-stock wc-block-grid__product-stock',
89 ]
90 );
91
92 ob_start();
93 (new ProductRenderer($product))
94 ->renderStockAvailability($wrapper_attributes);
95
96 return ob_get_clean();
97 }
98
99 }
100