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

WooCommerce追加数量输入框 解决wishlist页加购按钮消失问题

现有代码错误点
  • 核心错误是return $html的位置完全放错了:返回逻辑被包裹在if (!is_page('wishlist'))的判断块内部,访问愿望清单页面时该判断不成立,函数没有任何返回值,WordPress过滤器会默认返回空内容,直接把原本的加入购物车按钮替换成空,导致按钮消失。
  • 存在隐藏兼容bug:非愿望清单页面下,如果商品不满足「简单类型、可购买、有库存、非单独销售」的判断条件,内层逻辑不会执行,同样不会触发return,这类商品的加购按钮也会被清空。
  • 存在变量未定义风险:$quantity仅在购物车循环匹配到当前商品时才会赋值,一旦后续逻辑调整误在匹配失败分支调用该变量,会直接抛出PHP错误。
修正后的钩子实现代码

只需要调整判断逻辑顺序、把返回语句放到函数最外层、初始化默认数量即可修复问题:

add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
    // 愿望清单页面直接返回原始按钮内容,不做修改
    if (is_page('wishlist')) {
        return $html;
    }
    if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
        $html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
        $in_cart = false;
        $quantity = 1; // 初始化默认数量,避免未定义变量报错
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            if($cart_item['product_id'] == $product->get_ID()){
                $in_cart = true;
                $quantity = $cart_item['quantity'];
                break;
            }
        }
        if($in_cart) {
            $html .= woocommerce_quantity_input( array('input_value' => $quantity), $product, false );
        } else {
            $html .= woocommerce_quantity_input( array(), $product, false );
        }
        $html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
        $html .= '</form>';
    }
    // 所有场景最终都返回对应内容,不会出现空返回
    return $html;
}
不使用woocommerce_loop_add_to_cart_link钩子的替代方案

可以实现,直接用WooCommerce商品循环自带的woocommerce_after_shop_loop_item动作钩子即可,默认的加入购物车按钮就挂载在这个钩子上。我们可以先移除默认的按钮输出,再挂载自定义的带数量选择的加购模块,逻辑更直观,不会出现过滤器返回值异常导致的内容丢失问题,示例代码如下:

// 移除商品循环默认的加购按钮
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
// 挂载自定义的带数量选择的加购模块
add_action( 'woocommerce_after_shop_loop_item', 'custom_loop_add_to_cart_with_quantity', 10 );
function custom_loop_add_to_cart_with_quantity() {
    global $product;
    // 愿望清单页面直接输出原始加购按钮
    if (is_page('wishlist')) {
        woocommerce_template_loop_add_to_cart();
        return;
    }
    if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
        echo '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
        $in_cart = false;
        $quantity = 1;
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            if($cart_item['product_id'] == $product->get_ID()){
                $in_cart = true;
                $quantity = $cart_item['quantity'];
                break;
            }
        }
        if($in_cart) {
            woocommerce_quantity_input( array('input_value' => $quantity), $product, false );
        } else {
            woocommerce_quantity_input( array(), $product, false );
        }
        echo '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
        echo '</form>';
    } else {
        // 不符合自定义条件的商品,输出默认加购按钮
        woocommerce_template_loop_add_to_cart();
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 12:39:15