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

求助:WooCommerce购物车超24件时显示特定插件配送选项

Show/Hide Custom Free Shipping by Label Based on Cart Item Count

Got it, let's adjust that code to target your custom free shipping method using its label instead of the shipping type. Here's how to do it:

First, you'll need to know the exact label of your custom free shipping option (the text that shows up on the checkout page for that method). Let's say it's "API-Powered Free Delivery"—you'll replace that with your actual label in the code below.

Modified Code

add_filter('woocommerce_package_rates', 'show_hide_custom_free_shipping_by_label', 10, 2);
function show_hide_custom_free_shipping_by_label($available_shipping_methods, $package){
    // 1. Set your minimum item count (24 in your case)
    $minimum_item_count = 24;
    
    // 2. Calculate total items in cart (simpler built-in method)
    $total_items = WC()->cart->get_cart_contents_count();
    
    // 3. Define your custom free shipping label (replace with your actual label)
    $custom_free_shipping_label = "API-Powered Free Delivery";
    
    // 4. Check if we meet the item count requirement
    $eligible_for_free_shipping = ($total_items >= $minimum_item_count);
    
    // 5. Loop through shipping methods to show/hide the custom one
    foreach ($available_shipping_methods as $method_key => $method) {
        // Check if this is our custom free shipping method by label
        if ($method->label === $custom_free_shipping_label) {
            // Hide it if not eligible, keep it if eligible
            if (!$eligible_for_free_shipping) {
                unset($available_shipping_methods[$method_key]);
            }
        }
        // Optional: Uncomment below to hide ALL other methods when eligible for free shipping
        /*
        else {
            if ($eligible_for_free_shipping) {
                unset($available_shipping_methods[$method_key]);
            }
        }
        */
    }
    
    // Refresh shipping rates cache to ensure changes take effect immediately
    WC()->session->set('shipping_methods', null);
    
    return $available_shipping_methods;
}

Key Changes Explained:

  • Simplified item count: Used WC()->cart->get_cart_contents_count() instead of looping through cart items—it’s a cleaner, built-in way to get the total number of items.
  • Target by label: We check $method->label against your custom free shipping’s exact display text. Make sure to replace "API-Powered Free Delivery" with the exact label that appears on your checkout page.
  • Eligibility toggle: The code only shows your custom free shipping when the cart has 24+ items, and hides it otherwise.
  • Cache fix: Added WC()->session->set('shipping_methods', null) to prevent WooCommerce from using cached shipping rates, so your changes show up right away.

How to Use:

  1. Replace the placeholder label with your actual custom free shipping method’s label.
  2. Copy this code into your theme’s functions.php file, or use a custom code plugin like Code Snippets (recommended—this way you won’t lose the code when updating your theme).
  3. Test it: Add items to your cart until you hit 24+ items, then check the checkout page to confirm the custom free shipping appears. Remove items to verify it hides again.

Optional: Hide All Other Methods When Eligible

If you want only the custom free shipping to show up when customers meet the item count requirement, uncomment the else block in the code. This will hide all other shipping methods once eligibility is met.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 16:22:50