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

如何修复WC()->cart->get_remove_url弃用替换后出现的致命错误

解决WooCommerce移除商品链接函数替换后的致命错误问题

错误原因

你将WC()->cart->get_remove_url( $cart_item_key )改为WC()->cart->wc_get_cart_remove_url( $cart_item_key )后出现致命错误,核心原因是:wc_get_cart_remove_url是全局函数,不属于WC_Cart类的成员方法,不能通过WC()->cart->这种类实例调用的方式执行。

正确修改方案

直接调用全局函数wc_get_cart_remove_url并传入$cart_item_key参数即可,修改后的完整代码如下:

function lp_woocommerce_checkout_remove_item( $product_name, $cart_item, $cart_item_key ) {
    if ( is_checkout() || is_cart() ) {
        $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
        $product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );

        $remove_link = apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
            '<a href="%s" class="remove" aria-label="%s" data-product_id="%s" data-product_sku="%s">×</a>',
            esc_url( wc_get_cart_remove_url( $cart_item_key ) ), // 此处为核心修改点
            __( 'Remove this item', 'woocommerce' ),
            esc_attr( $product_id ),
            esc_attr( $_product->get_sku() )
        ), $cart_item_key );

        return '<span>' . $remove_link . '</span> <span>' . $product_name . '</span>';
    }

    return $product_name;
}
add_filter( 'woocommerce_cart_item_name', 'lp_woocommerce_checkout_remove_item', 10, 3 );

补充说明

  • WooCommerce 3.3及以上版本弃用了WC_Cart::get_remove_url类方法,官方指定使用全局函数wc_get_cart_remove_url来替代,这是符合版本规范的调用方式。
  • 修改完成后建议清除网站缓存,确保新代码正常生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 09:25:19