yoone-wc-product-bundles/yoone-product-bundles.php

67 lines
2.8 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
/**
* Plugin Name: Yoone Product Bundles (混装产品)
* Description: 为 WooCommerce 提供“混装产品Bundle”类型在后台为某个产品配置可混装的简单商品与最小混装数量前端允许顾客按分类选择混装商品与数量当达到最小数量时可加入购物车结算价格为所选商品的价格总和。
* Author: Yoone
* Version: 0.1.0
* Requires at least: 6.0
* Requires PHP: 7.4
* WC requires at least: 6.0
* WC tested up to: 8.x
*/
defined('ABSPATH') || exit;
// 简单防御:确保 WooCommerce 已激活
if (! function_exists('WC')) {
add_action('admin_notices', function () {
echo '<div class="notice notice-error"><p>Yoone Product Bundles 需要启用 WooCommerce。</p></div>';
});
return;
}
// 插件常量(路径)
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';
// 引导插件
add_action('plugins_loaded', function () {
// 注册产品类型
Yoone_Product_Bundles::instance();
Yoone_Product_Bundles_Admin::instance();
Yoone_Product_Bundles_Frontend::instance();
});
// 插件版本号
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 表,可在此创建。
});