← All changes
|
includes/helpers/Woo_Builder/Abstract_Single_Widget.php
+36
-4
51.1.44
→
51.1.87
View file →
| @@ -7,8 +7,9 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace King_Addons; |
| 9 | 9 | |
| 10 | 10 | use Elementor\Widget_Base; |
| 11 | +use King_Addons\Woo_Builder\Context as Woo_Context; | |
| 11 | 12 | use WC_Product; |
| 12 | 13 | |
| 13 | 14 | if (!defined('ABSPATH')) { |
| 14 | 15 | exit; |
| @@ -20,8 +21,9 @@ | ||
| 20 | 21 | abstract class Abstract_Single_Widget extends Widget_Base |
| 21 | 22 | { |
| 22 | 23 | /** |
| 23 | 24 | * Get current product object. |
| 25 | + * In editor mode, will return a preview product if no product is set. | |
| 24 | 26 | * |
| 25 | 27 | * @return WC_Product|null |
| 26 | 28 | */ |
| 27 | 29 | protected function get_product(): ?WC_Product |
| @@ -27,17 +29,33 @@ | ||
| 27 | 29 | protected function get_product(): ?WC_Product |
| 28 | 30 | { |
| 29 | 31 | global $product, $post; |
| 30 | 32 | |
| 33 | + if (!function_exists('wc_get_product') || !class_exists('WC_Product')) { | |
| 34 | + return null; | |
| 35 | + } | |
| 36 | + | |
| 37 | + // If global product is already set, use it | |
| 31 | 38 | if ($product instanceof WC_Product) { |
| 32 | 39 | return $product; |
| 33 | 40 | } |
| 34 | 41 | |
| 42 | + // Try to get from post | |
| 35 | 43 | if (!empty($post->ID) && 'product' === get_post_type($post->ID)) { |
| 36 | 44 | $prod = wc_get_product($post->ID); |
| 37 | - return $prod instanceof WC_Product ? $prod : null; | |
| 45 | + if ($prod instanceof WC_Product) { | |
| 46 | + return $prod; | |
| 47 | + } | |
| 38 | 48 | } |
| 39 | 49 | |
| 50 | + // In editor mode (including AJAX render), try to set up a preview product | |
| 51 | + if (class_exists('King_Addons\\Woo_Builder\\Context')) { | |
| 52 | + $preview = Woo_Context::setup_preview_product(); | |
| 53 | + if ($preview instanceof WC_Product) { | |
| 54 | + return $preview; | |
| 55 | + } | |
| 56 | + } | |
| 57 | + | |
| 40 | 58 | return null; |
| 41 | 59 | } |
| 42 | 60 | |
| 43 | 61 | /** |
| @@ -46,17 +64,31 @@ | ||
| 46 | 64 | * @return void |
| 47 | 65 | */ |
| 48 | 66 | protected function render_missing_product_notice(): void |
| 49 | 67 | { |
| 50 | - if (!\Elementor\Plugin::$instance->editor->is_edit_mode()) { | |
| 68 | + $is_editor = false; | |
| 69 | + if (class_exists('King_Addons\\Woo_Builder\\Context')) { | |
| 70 | + $is_editor = Woo_Context::is_editor(); | |
| 71 | + } | |
| 72 | + | |
| 73 | + $is_elementor_edit = false; | |
| 74 | + if (class_exists('\Elementor\Plugin') && \Elementor\Plugin::$instance && isset(\Elementor\Plugin::$instance->editor)) { | |
| 75 | + $is_elementor_edit = \Elementor\Plugin::$instance->editor->is_edit_mode(); | |
| 76 | + } | |
| 77 | + | |
| 78 | + if (!$is_editor && !$is_elementor_edit) { | |
| 51 | 79 | return; |
| 52 | 80 | } |
| 53 | 81 | |
| 82 | + // Check if we're editing a Single Product template but just don't have any products | |
| 83 | + if (class_exists('King_Addons\\Woo_Builder\\Context') && Woo_Context::is_editing_template_type('single_product')) { | |
| 84 | + echo '<div class="king-addons-woo-builder-notice">' . esc_html__('No products found. Please create at least one WooCommerce product to preview this widget.', 'king-addons') . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 85 | + return; | |
| 86 | + } | |
| 87 | + | |
| 54 | 88 | echo '<div class="king-addons-woo-builder-notice">' . esc_html__('This widget works only in a Single Product context.', 'king-addons') . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 55 | 89 | } |
| 56 | 90 | } |
| 57 | - | |
| 58 | - | |
| 59 | 91 | |
| 60 | 92 | |
| 61 | 93 | |
| 62 | 94 | |