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

67 lines
2.8 KiB
PHP
Raw Permalink 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: 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
* 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';
// 引导插件
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 表,可在此创建。
});