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

WooCommerce外部商品"查看更多"按钮仅分类页生效,Elementor页面失效修复

解决WooCommerce外部产品循环按钮在Elementor产品块中消失的问题

你的代码存在两个核心问题:

  1. 仅限制在产品分类页和商店页生效,Elementor产品块这类页面不在判断范围内
  2. 非目标页面场景下未返回任何值,导致按钮被清空消失

另外,原代码没有针对外部产品做类型判断,会误改所有产品的按钮,不符合你的需求。

修正后的代码如下:

// 替换外部产品的循环加入购物车按钮为查看更多
add_filter( 'woocommerce_loop_add_to_cart_link', 'ts_replace_external_product_add_to_cart_button', 10, 2 );
function ts_replace_external_product_add_to_cart_button( $button, $product ) {
    // 仅针对外部产品进行按钮替换
    if ( $product->is_type('external') ) {
        $button_text = __("Ver más", "woocommerce");
        $button_link = $product->get_permalink();
        $button = '<a class="button" href="' . esc_url( $button_link ) . '">' . esc_html( $button_text ) . '</a>';
    }
    
    // 无论是否修改,必须返回按钮,避免无返回值导致按钮消失
    return $button;
}

关键改动说明:

  • 新增$product->is_type('external')判断,精准定位外部产品,不会影响普通产品的按钮
  • 移除页面场景限制,让所有展示产品循环的页面(包括Elementor产品块、主页产品列表等)都能生效
  • 函数末尾强制返回$button,非外部产品场景会返回原始按钮,彻底解决按钮消失问题
  • 增加esc_url()和esc_html()转义,提升代码安全性,符合WooCommerce开发规范

替换这段代码后,所有页面的产品循环中,外部产品的「加入购物车」按钮都会被替换为「Ver más」按钮,其他产品的按钮保持正常显示。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 19:12:04