start_controls_section('section_settings', array('label' => __('设置', 'yoone-product-bundles'))); $this->add_control('use_current_product', array( 'label' => __('使用当前产品页面', 'yoone-product-bundles'), 'type' => \Elementor\Controls_Manager::SWITCHER, 'label_on' => __('是', 'yoone-product-bundles'), 'label_off' => __('否', 'yoone-product-bundles'), 'return_value' => 'yes', 'default' => 'yes', )); $this->add_control('product_id', array( 'label' => __('指定产品ID(Mix and Match 类型)', 'yoone-product-bundles'), 'type' => \Elementor\Controls_Manager::NUMBER, 'default' => 0, 'condition' => array('use_current_product!' => 'yes'), )); $this->end_controls_section(); } protected function render() { $settings = $this->get_settings_for_display(); $use_current = isset($settings['use_current_product']) && $settings['use_current_product'] === 'yes'; $product_id = absint(isset($settings['product_id']) ? $settings['product_id'] : 0); // 确定产品对象 $product = null; if ($use_current && is_singular('product')) { global $post; if ($post) $product = wc_get_product($post->ID); } elseif ($product_id > 0) { $product = wc_get_product($product_id); } if (! $product || $product->get_type() !== Yoone_Product_Bundles::TYPE) { echo '
' . esc_html__('请选择或切换到一个“Mix and Match (Yoone Bundle)”产品以显示表单。', 'yoone-product-bundles') . '
'; return; } // 前端资源 wp_enqueue_style('yoone-pb-frontend'); wp_enqueue_script('yoone-pb-frontend'); // 渲染模板 wc_get_template('global/yoone-bundle-form.php', array(), '', YOONE_PB_PATH . 'templates/'); } public static function register() { if (! class_exists('Elementor\\Plugin')) return; add_action('elementor/widgets/register', function($widgets_manager) { $widgets_manager->register(new self()); }); } } } } else { // 定义一个降级的空壳类,至少保证调用 register() 不会报错 if (! class_exists('Yoone_PB_Elementor_Widget')) { class Yoone_PB_Elementor_Widget { public static function register() { /* noop: Elementor 未加载*/ } } } }