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

WooCommerce结账页面定制:隐藏指定配送及支付方式

Hey there! Let's get those checkout tweaks sorted for your WooCommerce store using the Code Snippets plugin—this is totally doable with a couple of custom code snippets. I'll break down each requirement step by step, with code you can copy-paste directly.


1. Hide a Specific Shipping Method When Order Total > €100

We'll use the woocommerce_package_rates filter hook to modify available shipping methods based on the order total. Here's the code:

add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_order_total', 10, 2 );
function hide_shipping_method_based_on_order_total( $rates, $package ) {
    // Only run on checkout page
    if ( ! is_checkout() || is_wc_endpoint_url() ) {
        return $rates;
    }

    // Get the total order amount (includes taxes & shipping; use subtotal if you want pre-shipping total)
    $order_total = WC()->cart->total;
    $target_total = 100; // €100, no currency symbol needed
    $target_shipping_method_slug = 'flat_rate:1'; // Replace with YOUR shipping method slug

    // If order total exceeds the target, remove the specified shipping method
    if ( $order_total > $target_total && isset( $rates[$target_shipping_method_slug] ) ) {
        unset( $rates[$target_shipping_method_slug] );
    }

    return $rates;
}

Quick Setup Tips:

  • Replace 'flat_rate:1' with your actual shipping method slug:
    1. Go to WooCommerce > Settings > Shipping
    2. Edit the shipping method you want to target
    3. Check the URL in your browser—you'll see a parameter like method_id=flat_rate&instance_id=1
    4. Combine them into method_id:instance_id (e.g., flat_rate:1)
  • To use the cart subtotal (before shipping/taxes) instead of the full total, swap WC()->cart->total with WC()->cart->subtotal

2. Hide Cash on Delivery (COD) When Local Pickup is Selected

For this, we'll use the woocommerce_available_payment_gateways filter to adjust payment methods based on the selected shipping option. Here's the code:

add_filter( 'woocommerce_available_payment_gateways', 'hide_cod_for_local_pickup' );
function hide_cod_for_local_pickup( $available_gateways ) {
    // Only run on checkout page
    if ( ! is_checkout() || is_wc_endpoint_url() ) {
        return $available_gateways;
    }

    $local_pickup_slug = 'local_pickup'; // Default local pickup slug; adjust if needed
    $cod_gateway_slug = 'cod'; // Default COD slug; change if using a custom COD plugin

    // Check if local pickup is the selected shipping method
    foreach ( WC()->session->get( 'chosen_shipping_methods' ) as $shipping_method ) {
        if ( strpos( $shipping_method, $local_pickup_slug ) !== false ) {
            // Unset COD if it's available
            if ( isset( $available_gateways[$cod_gateway_slug] ) ) {
                unset( $available_gateways[$cod_gateway_slug] );
            }
            break;
        }
    }

    return $available_gateways;
}

Quick Setup Tips:

  • If your local pickup has a non-default slug, find it using the same method as the shipping step above
  • For custom COD plugins, check the slug in WooCommerce > Settings > Payments: edit the COD method and look for gateway_id in the URL

How to Add These Snippets with Code Snippets Plugin

  1. Go to Snippets > Add New in your WordPress dashboard
  2. Give each snippet a clear name (e.g., "Hide Shipping Method Over €100", "Hide COD for Local Pickup")
  3. Paste the corresponding code into the "Code" box
  4. Under "Settings", select "Run snippet everywhere" (or "Only run on site front-end" if preferred)
  5. Click "Save Changes and Activate"

Important Checks:

  • Always backup your site first, or test changes in a staging environment
  • After activation, test both scenarios:
    • Add items to reach an order total over €100 and verify the target shipping method is hidden
    • Select local pickup and confirm COD disappears from payment options
  • If the code doesn't work, double-check the slugs—this is the most common fix!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:15:41