You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

插件激活钩子函数失效:无法批量创建WooCommerce产品

问题分析与修复方案

问题根源

  1. 激活判断逻辑完全颠倒:原代码中if ( get_option('my_plugin_activated') )意味着仅当激活标记已存在时才执行创建逻辑,但首次激活时该选项不存在,导致核心代码块从未运行。
  2. 函数嵌套定义不合法:在activate_core_functions内部定义insert_SB1等函数,会引发PHP作用域错误,甚至导致函数无法调用。
  3. 错误依赖admin_notices钩子:激活钩子执行时后台页面尚未加载,admin_notices不会触发,挂载的创建函数根本不会执行。
  4. add_costs_meta语法错误:函数定义缺少function关键字,直接导致语法报错。

修复后的代码实现

1. 主插件文件(保持不变)

// Activate important functions
include( plugin_dir_path(__FILE__) . '/includes/activation.php');
register_activation_hook( __FILE__, 'activate_core_functions' );

2. activation.php修正版

// 全局作用域定义所有产品创建函数与辅助函数
function insert_SB1() {
    // 检查产品是否已存在,避免重复创建
    $existing_post = get_page_by_title('SB: 0 - 60', OBJECT, 'product');
    if ($existing_post) return;

    $my_post = array(
        'post_title'    => 'SB: 0 - 60', 
        'post_status'   => 'publish', 
        'post_type'     => 'product'
    );
    
    // 捕获创建错误,便于排查
    $post_id = wp_insert_post( $my_post, true );
    if (is_wp_error($post_id)) {
        error_log('SB1产品创建失败: ' . $post_id->get_error_message());
        return;
    }
    
    wp_set_object_terms( $post_id, 'Skates', 'product_cat' );
    wp_set_object_terms( $post_id, 'simple', 'product_type');

    update_post_meta( $post_id, '_stock_status', 'instock');
    
    // Meta values
    update_post_meta( $post_id, 'sections', 3);

    // Materials related
    update_post_meta( $post_id, 'bpl', 672);
    update_post_meta( $post_id, '1pp', 2);    
    update_post_meta( $post_id, 'plf', 132);    
    update_post_meta( $post_id, 'bnp', 152);    
    update_post_meta( $post_id, 'sb', 152);    
    update_post_meta( $post_id, 'cs', 218);    
    update_post_meta( $post_id, 'spl1', 578);    
    update_post_meta( $post_id, 'spl2', 0);    
    update_post_meta( $post_id, 'spl3', 0);    
    update_post_meta( $post_id, 'bwt', 60);    
    update_post_meta( $post_id, '6hh', 18);    
    update_post_meta( $post_id, '611hh', 30);    
    update_post_meta( $post_id, 'cp', 0);    
    update_post_meta( $post_id, 'abpl', 175);    
    update_post_meta( $post_id, 'ab', 1);

    add_costs_meta($post_id);
}

// 同理定义insert_SB2至insert_SB8,替换对应产品标题与元数据
function insert_SB2() {
    $existing_post = get_page_by_title('SB: [你的产品标题2]', OBJECT, 'product');
    if ($existing_post) return;

    $my_post = array(
        'post_title'    => 'SB: [你的产品标题2]', 
        'post_status'   => 'publish', 
        'post_type'     => 'product'
    );
    
    $post_id = wp_insert_post( $my_post, true );
    if (is_wp_error($post_id)) {
        error_log('SB2产品创建失败: ' . $post_id->get_error_message());
        return;
    }
    
    wp_set_object_terms( $post_id, 'Skates', 'product_cat' );
    wp_set_object_terms( $post_id, 'simple', 'product_type');
    update_post_meta( $post_id, '_stock_status', 'instock');
    
    // 替换为该产品对应的元数据
    update_post_meta( $post_id, 'sections', [对应值]);
    // ...其他材料相关元数据
    add_costs_meta($post_id);
}

// insert_SB3至insert_SB8以此类推...

// 修复辅助函数语法错误
function add_costs_meta($post_id){  
    // Costs Related meta vals
    update_post_meta( $post_id, 'psc', 1.75);
    update_post_meta( $post_id, 'pmhc', 45);
    update_post_meta( $post_id, 'osc', 1.75);
    update_post_meta( $post_id, 'oabc', 295);
    update_post_meta( $post_id, 'mbc', 1.75);
    update_post_meta( $post_id, 'mbcp', 0.20);
    update_post_meta( $post_id, 'wc', 45);
    update_post_meta( $post_id, 'ihsc', 75);
}

// 激活主函数
function activate_core_functions() {
    // 仅首次激活(无标记时)执行产品创建
    if ( !get_option('my_plugin_activated') ) {        
        // 直接调用所有产品创建函数
        insert_SB1();
        insert_SB2();
        insert_SB3();
        insert_SB4();
        insert_SB5();
        insert_SB6();
        insert_SB7();
        insert_SB8();
    }

    // 更新激活标记,记录激活时间
    update_option('my_plugin_activated', time());
}

额外优化建议

  • 封装通用创建函数:避免重复代码,可封装一个通用函数处理类似产品的创建:
function create_skate_product($title, $meta_data) {
    $existing_post = get_page_by_title($title, OBJECT, 'product');
    if ($existing_post) return;

    $my_post = array(
        'post_title'    => $title, 
        'post_status'   => 'publish', 
        'post_type'     => 'product'
    );
    
    $post_id = wp_insert_post( $my_post, true );
    if (is_wp_error($post_id)) {
        error_log("产品{$title}创建失败: " . $post_id->get_error_message());
        return;
    }
    
    wp_set_object_terms( $post_id, 'Skates', 'product_cat' );
    wp_set_object_terms( $post_id, 'simple', 'product_type');
    update_post_meta( $post_id, '_stock_status', 'instock');
    
    // 批量更新元数据
    foreach ($meta_data as $key => $value) {
        update_post_meta( $post_id, $key, $value );
    }
    
    add_costs_meta($post_id);
}

// 调用示例
create_skate_product('SB: 0 - 60', array(
    'sections' => 3,
    'bpl' => 672,
    '1pp' => 2,
    // 其他元数据...
));

内容的提问来源于stack exchange,提问作者Mr.Coder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 10:05:22