82 lines
3.7 KiB
PHP
82 lines
3.7 KiB
PHP
<?php
|
||
/**
|
||
* Plugin Name: Yoone Product Bundles
|
||
* Description: Adds a "Mix and Match" product type to WooCommerce. Allows creating a bundle product where customers can select from a list of simple products, set quantities, and add to cart if a minimum quantity is met. The price is the sum of the selected products.
|
||
* Author: Yoone
|
||
* Version: 0.1.0
|
||
* Requires at least: 6.0
|
||
* Requires PHP: 7.4
|
||
* Requires Plugins: woocommerce
|
||
* WC requires at least: 6.0
|
||
* WC tested up to: 8.x
|
||
*/
|
||
|
||
defined('ABSPATH') || exit;
|
||
|
||
// Basic defense: ensure WooCommerce is active
|
||
if (! function_exists('WC')) {
|
||
add_action('admin_notices', function () {
|
||
echo '<div class="notice notice-error"><p>' . esc_html__('Yoone Product Bundles requires WooCommerce to be activated.', 'yoone-product-bundles') . '</p></div>';
|
||
});
|
||
return;
|
||
}
|
||
|
||
// Plugin constants (paths)
|
||
define('YOONE_PB_PATH', plugin_dir_path(__FILE__));
|
||
define('YOONE_PB_URL', plugin_dir_url(__FILE__));
|
||
|
||
// 自动加载(简单版):按需引入类文件
|
||
require_once YOONE_PB_PATH . 'includes/class-yoone-product-bundles.php';
|
||
require_once YOONE_PB_PATH . 'includes/class-yoone-product-type-bundle.php';
|
||
require_once YOONE_PB_PATH . 'includes/admin/class-yoone-product-bundles-admin.php';
|
||
require_once YOONE_PB_PATH . 'includes/frontend/class-yoone-product-bundles-frontend.php';
|
||
require_once YOONE_PB_PATH . 'includes/blocks/register.php';
|
||
require_once YOONE_PB_PATH . 'includes/shortcodes/register.php';
|
||
// 注意:Elementor 小组件文件依赖 Elementor 的类,需在 Elementor 加载后再引入,避免致命错误
|
||
|
||
// 引导插件
|
||
add_action('plugins_loaded', function () {
|
||
// 注册产品类型
|
||
Yoone_Product_Bundles::instance();
|
||
Yoone_Product_Bundles_Admin::instance();
|
||
Yoone_Product_Bundles_Frontend::instance();
|
||
// 注册 Gutenberg 区块
|
||
if (function_exists('register_block_type')) {
|
||
Yoone_PB_Blocks::instance();
|
||
}
|
||
// 注册 Elementor 小组件(在 Elementor 通知 widgets 可注册时再加载并注册)
|
||
add_action('elementor/widgets/register', function($widgets_manager){
|
||
require_once YOONE_PB_PATH . 'includes/elementor/class-yoone-pb-elementor-widget.php';
|
||
if (class_exists('Yoone_PB_Elementor_Widget') && class_exists('\\Elementor\\Widget_Base')) {
|
||
$widgets_manager->register(new Yoone_PB_Elementor_Widget());
|
||
}
|
||
});
|
||
});
|
||
|
||
// 插件版本号
|
||
define('YOONE_PB_VERSION', '0.1.1');
|
||
|
||
// 资源加载(前端样式/脚本)
|
||
add_action('wp_enqueue_scripts', function () {
|
||
wp_enqueue_style('yoone-pb-frontend', YOONE_PB_URL . 'assets/css/frontend.css', array(), YOONE_PB_VERSION);
|
||
wp_enqueue_script('yoone-pb-frontend', YOONE_PB_URL . 'assets/js/frontend.js', array('jquery'), YOONE_PB_VERSION, true);
|
||
});
|
||
|
||
// 后台资源
|
||
add_action('admin_enqueue_scripts', function ($hook) {
|
||
// 仅在产品编辑页加载(post.php/post-new.php 且 post_type=product)
|
||
if (strpos($hook, 'post.php') !== false || strpos($hook, 'post-new.php') !== false) {
|
||
$screen = get_current_screen();
|
||
if ($screen && 'product' === $screen->post_type) {
|
||
wp_enqueue_style('woocommerce_admin_styles');
|
||
wp_enqueue_script('wc-product-search'); // Woo 的 select2 产品搜索
|
||
wp_enqueue_style('yoone-pb-admin', YOONE_PB_URL . 'assets/css/admin.css', array(), YOONE_PB_VERSION);
|
||
wp_enqueue_script('yoone-pb-admin', YOONE_PB_URL . 'assets/js/admin.js', array('jquery'), YOONE_PB_VERSION, true);
|
||
}
|
||
}
|
||
});
|
||
|
||
// 激活钩子:无需建表,使用 postmeta 保存配置;此处可预置默认值或做数据迁移
|
||
register_activation_hook(__FILE__, function () {
|
||
// 预留:未来若需要自定义 DB 表,可在此创建。
|
||
}); |