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

如何在WooCommerce特定商品页移除Square的Google/Apple Pay按钮?

针对特定商品ID移除Square的Apple Pay/Google Pay按钮

当然可行,你可以通过判断当前商品ID,仅在指定商品页面移除数字钱包按钮,修改后的代码如下:

add_filter( 'wc_square_display_digital_wallet_on_pages', function( $pages ) {
    // 指定需要移除按钮的商品ID,多个用逗号分隔
    $target_product_ids = array(123, 456);

    // 检查当前是否是单个商品页面
    if (is_product()) {
        global $product;
        $current_product_id = $product->get_id();

        // 如果当前商品ID在目标列表中,移除商品页的显示权限
        if (in_array($current_product_id, $target_product_ids)) {
            $pages = array_diff($pages, array('product'));
        }
    }

    return $pages;
}, 10, 1 );

代码说明:

  • $target_product_ids:将需要移除按钮的商品ID填入这个数组,多个ID用逗号分隔即可
  • is_product():判断当前页面是否为单个商品详情页
  • $product->get_id():获取当前商品的ID
  • array_diff():从原有页面显示列表中移除product项,实现隐藏按钮的效果

如果遇到主题或插件导致global $product无法正常获取商品对象的情况,可以改用get_the_ID()来获取当前商品ID,替换代码中获取ID的部分即可:

$current_product_id = get_the_ID();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 21:48:31