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

WordPress+WooCommerce本地取件无法豁免最低订单金额问题求助

问题:WooCommerce本地取件未豁免最低订单金额限制
  • 当前代码可正常实现配送订单的最低金额限制(25欧元),但选择本地取件时,该限制仍然生效,未被豁免。
  • 怀疑shipping_method_id设置错误,同时想确认是否有其他可用Hook。
  • 网站:papabross.gr
  • 当前使用代码:
add_action( 'woocommerce_check_cart_items', 'wc_minimum_required_order_amount' ); function wc_minimum_required_order_amount() {

// HERE Your settings
$minimum_amount     = 25; // The minimum cart total amount
$shipping_method_id = 'local_pickup:10'; // The targeted shipping method Id (exception)

// Get some variables
$cart_total     = (float) WC()->cart->total; // Total cart amount
$chosen_methods = (array) WC()->session->get( 'chosen_shipping_methods' ); // Chosen shipping method rate Ids (array)

// Only when a shipping method has been chosen
if ( ! empty($chosen_methods) ) {
    $chosen_method  = explode(':', reset($chosen_methods)); // Get the chosen shipping method Id (array)
    $chosen_method_id = reset($chosen_method); // Get the chosen shipping method Id
}

// If \"Local pickup\" shipping method is chosen, exit (no minimun is required)
if ( isset($chosen_method_id) && $chosen_method_id === $shipping_method_id ) {
    return; // exit
}

// Add an error notice is cart total is less than the minimum required
if ( $cart_total < $minimum_amount ) {
    wc_add_notice( sprintf(
        __(\"Η ελάχιστη παραγγελία για αποστολή είναι %s (Η παραγγελία σας μέχρι στιγμής είναι %s).\", \"woocommerce\"), // Text message
        wc_price( $minimum_amount ),
        wc_price( $cart_total )
    ), 'error' );
}
}

核心问题:Shipping Method ID匹配逻辑错误

你的代码里存在两个关键问题:

  1. 代码通过explode(':', reset($chosen_methods))拆分后,$chosen_method_id仅拿到了local_pickup(冒号前的类型标识),但你用它和完整的local_pickup:10做对比,必然匹配失败。
  2. 未确认网站中本地取件的真实ID是否为local_pickup:10。

第一步:获取正确的Shipping Method ID

添加以下临时调试代码到主题functions.php,选择本地取件后,在结算页面即可看到真实的配送方式ID:

add_action('woocommerce_after_checkout_form', 'debug_shipping_method_id');
function debug_shipping_method_id() {
    $chosen_methods = WC()->session->get('chosen_shipping_methods');
    if (!empty($chosen_methods)) {
        echo '<pre>当前选中配送方式ID:' . print_r($chosen_methods, true) . '</pre>';
    }
}

调试完成后删除这段代码即可。

第二步:修正后的代码

以下代码支持两种豁免逻辑:要么豁免指定ID的本地取件,要么豁免所有类型的本地取件,按需选择即可:

add_action( 'woocommerce_check_cart_items', 'wc_minimum_required_order_amount' );
function wc_minimum_required_order_amount() {
    // 配置项
    $minimum_amount     = 25; // 最低订单金额
    $target_shipping_id = 'local_pickup:10'; // 替换为你获取到的真实本地取件ID
    $allow_all_local_pickup = true; // 设为true则豁免所有本地取件,false则仅豁免指定ID

    $cart_total = (float) WC()->cart->total;
    $chosen_methods = (array) WC()->session->get('chosen_shipping_methods');

    if (!empty($chosen_methods)) {
        $chosen_method = reset($chosen_methods);
        
        // 判断是否触发豁免
        $is_local_pickup = $allow_all_local_pickup 
            ? strpos($chosen_method, 'local_pickup') !== false 
            : $chosen_method === $target_shipping_id;
        
        if ($is_local_pickup) {
            return; // 本地取件,跳过最低金额检查
        }
    }

    // 非本地取件时执行金额检查
    if ($cart_total < $minimum_amount) {
        wc_add_notice( sprintf(
            __( "Η ελάχιστη παραγγελία για αποστολή είναι %s (Η παραγγελία σας μέχρι στιγμής είναι %s).", "woocommerce" ),
            wc_price($minimum_amount),
            wc_price($cart_total)
        ), 'error' );
    }
}

Hook选择说明

woocommerce_check_cart_items是合适的Hook,会在购物车页面和结算页面触发,完全适配订单金额检查场景。如果需要更精准的触发时机,也可以替换为:

  • woocommerce_before_checkout_process:仅在结算页面提交前检查
  • woocommerce_cart_checkout_validation:和woocommerce_check_cart_items作用一致,同样适用

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 11:55:26