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

WooCommerce:特定类目满500英镑免运费钩子实现及优化咨询

Hey there! Let's tackle optimizing your WooCommerce free shipping logic for Category A. First, let's go over the issues in your current code, then I'll share a refined solution that meets your exact requirements.

Issues in Your Current Code

  • Variable typos: You used $pakage instead of $package (the correct parameter from the hook) and $item instead of $cart_item in your loop—this would throw undefined variable errors and break the logic entirely.
  • Redundant calculations: Using number_format when summing totals is unnecessary and can lead to precision issues (it converts numbers to strings temporarily).
  • Core logic gap: Your code sets all shipping costs to zero once Category A hits the £500 threshold, but your requirement specifies that only Category A items should be free when mixed with other categories. This means we need to split the shipping cost proportionally instead of wiping it entirely.

Optimized Solution

Here's a revised version of your code that fixes these issues and implements your exact requirements:

add_filter( 'woocommerce_package_rates', 'category_based_free_shipping_logic', 10, 2 );
function category_based_free_shipping_logic( $rates, $package ) {
    // Easy-to-edit configuration
    $target_category_id = 642; // Your Category A ID
    $free_shipping_threshold = 500; // GBP threshold for free shipping on Category A
    
    $category_a_total = 0;
    $cart_total = 0;
    
    // Calculate totals for Category A and entire cart (including tax)
    foreach( $package['contents'] as $cart_item ) {
        $item_total = $cart_item['line_total'] + $cart_item['line_subtotal_tax'];
        $cart_total += $item_total;
        
        // Check if item belongs to our target category
        if( in_array( $target_category_id, $cart_item['data']->get_category_ids() ) ) {
            $category_a_total += $item_total;
        }
    }
    
    // Only apply free shipping logic if Category A meets the threshold
    if( $category_a_total >= $free_shipping_threshold && $cart_total > 0 ) {
        foreach( $rates as $rate_key => $rate ) {
            // Calculate what percentage of the cart is Category A
            $category_a_ratio = $category_a_total / $cart_total;
            
            // Adjust shipping cost: subtract the proportion that's covered by free shipping
            $rates[$rate_key]->cost = round( $rate->cost * ( 1 - $category_a_ratio ), 2 );
            
            // Adjust taxes proportionally to match the new cost
            $adjusted_taxes = [];
            foreach( $rate->taxes as $tax_key => $tax_amount ) {
                $adjusted_taxes[$tax_key] = round( $tax_amount * ( 1 - $category_a_ratio ), 2 );
            }
            $rates[$rate_key]->taxes = $adjusted_taxes;
        }
    }
    
    return $rates;
}

Key Improvements Explained

  1. Fixed variable errors: We properly use the $package parameter passed to the hook and reference $cart_item correctly in the loop.
  2. Precise totals calculation: We sum item totals (including tax) directly without unnecessary string conversions.
  3. Proportional shipping adjustment: Instead of zeroing out all shipping, we calculate the ratio of Category A items to the total cart value. For example:
    • If your cart has £600 of Category A and £400 of Category B, Category A makes up 60% of the cart. We reduce the shipping cost by 60%, leaving the 40% (for Category B) to be charged normally.
  4. Maintainable configuration: We moved hardcoded values (category ID, threshold) to the top of the function so you can easily update them later.
  5. Tax alignment: We adjust shipping taxes proportionally to match the reduced cost, ensuring your tax calculations stay accurate.

Important Note

After updating your code, clear WooCommerce transients (go to WooCommerce > Status > Tools > Clear transients). WooCommerce caches shipping rates, so your changes won't take effect until you flush this cache.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 04:59:07