| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Hooks\Handlers\BlockEditors; |
| 4 |
|
| 5 |
use FluentCart\Api\Resource\FrontendResource\CartResource; |
| 6 |
use FluentCart\App\Helpers\CartHelper; |
| 7 |
use FluentCart\App\Helpers\Helper; |
| 8 |
use FluentCart\App\Hooks\Cart\CartLoader; |
| 9 |
use FluentCart\App\Models\Product; |
| 10 |
use FluentCart\App\Models\ProductVariation; |
| 11 |
use FluentCart\App\Modules\Templating\AssetLoader; |
| 12 |
use FluentCart\App\Services\Renderer\CartDrawerRenderer; |
| 13 |
use FluentCart\App\Services\Renderer\MiniCartRenderer; |
| 14 |
use FluentCart\App\Services\Renderer\ProductRenderer; |
| 15 |
use FluentCart\App\Services\Translations\TransStrings; |
| 16 |
use FluentCart\App\Vite; |
| 17 |
use FluentCart\Framework\Support\Arr; |
| 18 |
|
| 19 |
class MiniCartBlockEditor extends BlockEditor |
| 20 |
{ |
| 21 |
protected static string $editorName = 'mini-cart'; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public function getScripts(): array |
| 26 |
{ |
| 27 |
return [ |
| 28 |
[ |
| 29 |
'source' => 'admin/BlockEditor/Cart/MiniCartBlockEditor.jsx', |
| 30 |
'dependencies' => ['wp-blocks', 'wp-components'] |
| 31 |
] |
| 32 |
]; |
| 33 |
} |
| 34 |
|
| 35 |
public function getStyles(): array |
| 36 |
{ |
| 37 |
return [ |
| 38 |
'admin/BlockEditor/Cart/style/mini-cart-block-editor.scss' |
| 39 |
]; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
public function supports(): array |
| 44 |
{ |
| 45 |
return [ |
| 46 |
'html' => false, |
| 47 |
'typography' => [ |
| 48 |
'fontSize' => true, |
| 49 |
'lineHeight' => true |
| 50 |
], |
| 51 |
'spacing' => [ |
| 52 |
'margin' => true, |
| 53 |
'padding' => true |
| 54 |
], |
| 55 |
'color' => [ |
| 56 |
'text' => true, |
| 57 |
'background' => true, |
| 58 |
], |
| 59 |
'__experimentalBorder' => [ |
| 60 |
'color' => true, |
| 61 |
'radius' => true, |
| 62 |
'style' => true, |
| 63 |
'width' => true, |
| 64 |
], |
| 65 |
'shadow' => true |
| 66 |
]; |
| 67 |
} |
| 68 |
|
| 69 |
public function localizeData(): array |
| 70 |
{ |
| 71 |
return [ |
| 72 |
$this->getLocalizationKey() => [ |
| 73 |
'slug' => $this->slugPrefix, |
| 74 |
'name' => static::getEditorName(), |
| 75 |
'title' => __('Mini Cart', 'fluent-cart'), |
| 76 |
'description' => __('Display a button for shoppers to quickly view their cart.', 'fluent-cart'), |
| 77 |
'placeholder_image' => Vite::getAssetUrl('images/placeholder.svg'), |
| 78 |
], |
| 79 |
'fluent_cart_block_translation' => TransStrings::blockStrings(), |
| 80 |
]; |
| 81 |
} |
| 82 |
|
| 83 |
public function render(array $shortCodeAttribute, $block = null) |
| 84 |
{ |
| 85 |
(new CartLoader())->registerDependency(); |
| 86 |
AssetLoader::loadMiniCartAssets(); |
| 87 |
|
| 88 |
$itemCount = 0; |
| 89 |
$cartData = []; |
| 90 |
|
| 91 |
// Hide mini cart count during instant checkout to avoid confusion |
| 92 |
if (!CartHelper::doingInstantCheckout()) { |
| 93 |
$cart = CartHelper::getCart(null, false); |
| 94 |
if ($cart) { |
| 95 |
$cartData = $cart->cart_data ?? []; |
| 96 |
$itemCount = count($cartData); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
$miniCartRenderer = new MiniCartRenderer($cartData, [ |
| 102 |
'item_count' => $itemCount |
| 103 |
]); |
| 104 |
|
| 105 |
ob_start(); |
| 106 |
$miniCartRenderer->renderMiniCart($shortCodeAttribute); |
| 107 |
return ob_get_clean(); |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
} |
| 112 |
|