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

如何在WooCommerce中针对特定产品ID或分类使用add_action/remove_action

修改WooCommerce代码以仅应用于指定产品或分类

直接把你的代码包裹在条件判断里,只在目标产品或分类页面执行动作调整:

// 定义目标产品ID和分类slug
$target_product_ids = array(707, 195, 193);
$target_category_slug = 'pick-mix'; // 注意:这里要替换成你「Pick & Mix」分类实际的slug,可在WP后台分类编辑页查看

// 仅在单个产品页面执行
if (is_product()) {
    $current_product_id = get_the_ID();
    // 检查当前产品是否是目标ID之一,或者属于目标分类
    if (in_array($current_product_id, $target_product_ids) || has_term($target_category_slug, 'product_cat', $current_product_id)) {
        remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
        add_action( 'woocommerce_after_single_product', 'woocommerce_template_single_add_to_cart', 1 );

        remove_action( 'woocommerce_single_product_summary', 'wc_usp_table', 33 );
        add_action( 'woocommerce_after_single_product', 'wc_usp_table', 33 );
    }
}

关键说明:

  • $target_category_slug:需要替换成你「Pick & Mix」分类的实际slug,登录WordPress后台进入产品分类编辑页,页面里的“别名”就是slug,也能从地址栏里找到。
  • 条件逻辑:先确认当前是单个产品页面,再检查产品ID是否在目标列表,或者产品属于指定分类,满足任一条件就执行动作调整。
  • 动作优先级(代码里的数字)保持你原来的设置,确保移除和添加的顺序正确。

内容的提问来源于stack exchange,提问作者Tom

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 15:45:57