yoone-wc-product-bundles/includes/elementor/class-yoone-pb-elementor-wi...

80 lines
3.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Elementor Widget: Yoone Bundle Selector
* 在产品页或指定产品ID渲染混装产品选择与加购表单。
*/
defined('ABSPATH') || exit;
// 如果 Elementor 未加载,其父类不存在,避免在文件被提前引入时产生致命错误
if (class_exists('\\Elementor\\Widget_Base')) {
if (! class_exists('Yoone_PB_Elementor_Widget')) {
class Yoone_PB_Elementor_Widget extends \Elementor\Widget_Base {
public function get_name() { return 'yoone_pb_bundle_selector'; }
public function get_title() { return __('Yoone Bundle Selector', 'yoone-product-bundles'); }
public function get_icon() { return 'eicon-cart'; }
public function get_categories() { return array('general'); }
protected function _register_controls() {
$this->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' => __('指定产品IDMix 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 '<div class="yoone-pb-elementor-notice">' . esc_html__('请选择或切换到一个“Mix and Match (Yoone Bundle)”产品以显示表单。', 'yoone-product-bundles') . '</div>';
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 未加载*/ }
}
}
}