根据购物车物品重量向 Woocommerce 运费添加额外费用

时间:2021-03-06 22:06:36

标签: php wordpress woocommerce hook-woocommerce shipping-method

我将以下代码放在functions.php中

function shipping_weight_fee( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;
    
    $extraFee  = 0;
    $total_weight = 0;

    // Loop though each cart item
    foreach ( $cart->get_cart() as $cart_item ) {
        // Get product id
        $product_id = $cart_item['product_id'];

        // Get weight
        $product_weight = $cart_item['data']->get_weight();

        // NOT empty 
        if ( ! empty( $product_weight )  ) {
            // Quantity
            $product_quantity = $cart_item['quantity'];

            // Add to total
            $total_weight += $product_weight * $product_quantity;
        }
    }

    if ( $total_weight > 10 ) {          
        $extraFee = 20;
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'shipping_weight_fee', 10, 1 );

如何从第一个代码片段中获取 $extraFee 以在以下代码中工作: 我尝试使用 global 但它不起作用。我相信第二个代码首先运行,但我不确定。

add_filter( 'woocommerce_package_rates', 'custom_shipping_costs', 20, 2 );
function custom_shipping_costs( $rates, $package ) {
    
    foreach( $rates as $rate_key => $rate ){
        // Excluding free shipping methods
        if( $rate->method_id != 'free_shipping'){

            // Set rate cost
            $rates[$rate_key]->cost +=  $extraFee + 3;

        }
    }
    return $rates;
}

请帮忙,谢谢!

2 个答案:

答案 0 :(得分:1)

如果您不想向购物车添加费用,您可以避免使用 woocommerce_cart_calculate_fees 钩子并使用 woocommerce_package_rates 钩子创建单个函数 (结合这两个函数)< /em>:

// set custom shipping cost by weight
add_filter( 'woocommerce_package_rates', 'custom_shipping_costs', 20, 2 );
function custom_shipping_costs( $rates, $package ) {

    $extraFee  = 0;
    $total_weight = 0;

    // Loop though each cart item
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // Get weight
        $product_weight = $cart_item['data']->get_weight();
        // NOT empty 
        if ( ! empty( $product_weight )  ) {
            // Quantity
            $product_quantity = $cart_item['quantity'];
            // Add to total
            $total_weight += $product_weight * $product_quantity;
        }
    }

    if ( $total_weight > 10 ) {          
        $extraFee = 20;
    }
    
    foreach( $rates as $rate_key => $rate ){
        // Excluding free shipping methods
        if( $rate->method_id != 'free_shipping'){
            // Set rate cost
            $rates[$rate_key]->cost += $extraFee + 3;
        }
    }
    return $rates;
}

代码已经过测试并且可以工作。将它添加到您的活动主题的functions.php。

答案 1 :(得分:1)

您可以使用 WC_Cart 方法 get_cart_contents_weight() 获取购物车总重量,而不是简化代码。

如果购物车没有分成多个运输包裹,这将起作用(请参阅下面的其他代码)。

以下代码还处理运费计算

add_filter( 'woocommerce_package_rates', 'additional_shipping_weight_costs', 10, 2 );
function additional_shipping_weight_costs( $rates, $package ) {
    $extra_cost   = (20 + 3); // Here define your extra shipping fee
    $total_weight = WC()->cart->get_cart_contents_weight(); // Get total weight

    if ( $total_weight > 10 ) {
        // Loop through available shipping rates
        foreach( $rates as $rate_key => $rate ){
            // Excluding free shipping methods
            if( 'free_shipping' !== $rate->method_id ){
                $has_taxes    = false; // Initializing
                $taxes        = array(); // Initializing

                $initial_cost = $rate->cost;
                $new_cost     = $rate->cost + $extra_cost;
            
                $rates[$rate_key]->cost = $new_cost;

                // Loop through taxes array (change taxes rate cost if enabled)
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $tax > 0 ){
                        // Get the tax rate conversion
                        $tax_rate    = $tax / $initial_cost;

                        // Set the new tax cost in the array
                        $taxes[$key] = $new_cost * $tax_rate;
                        $has_taxes   = true; // Enabling tax changes
                    }
                }
                // set array of shipping tax cost
                if( $has_taxes ) {
                    $rates[$rate_key]->taxes = $taxes;
                }
            }
        }
    }
    return $rates;
}

代码位于活动子主题(或活动主题)的functions.php 文件中。经测试有效。

不要忘记清空您的购物车以刷新运费缓存。


处理多个运输包裹

由于某些插件或自定义代码可以将购物车项目拆分为多个运输包裹,在这种情况下,您将使用以下代码同时处理运费税计算

add_filter( 'woocommerce_package_rates', 'additional_shipping_weight_costs', 10, 2 );
function additional_shipping_weight_costs( $rates, $package ) {
    $extra_cost   = (20 + 3); // Here define your extra shipping fee
    $total_weight = 0; // Initializing

    // Get cart items for the current shipping package (to calculate package weight)
    foreach( $package['contents'] as $item ) {
        $total_weight += floatval( $item['data']->get_weight() ) * $item['quantity'];
    }

    if ( $total_weight > 10 ) {
        // Loop through available shipping rates
        foreach( $rates as $rate_key => $rate ){
            // Excluding free shipping methods
            if( 'free_shipping' !== $rate->method_id ){
                $has_taxes = false; // Initializing
                $taxes     = array(); // Initializing

                $initial_cost = $rate->cost;
                $new_cost     = $initial_cost + $extra_cost;

                $rates[$rate_key]->cost = $new_cost; // Set new rate cost

                // Loop through taxes array (change taxes rate cost if enabled)
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $tax > 0 ){
                        // Get the tax rate conversion
                        $tax_rate    = $tax / $initial_cost;

                        // Set the new tax cost in the array
                        $taxes[$key] = $new_cost * $tax_rate;
                        $has_taxes   = true; // Enabling tax changes
                    }
                }
                // set array of shipping tax cost
                if( $has_taxes ) {
                    $rates[$rate_key]->taxes = $taxes;
                }
            }
        }
    }
    return $rates;
}

代码位于活动子主题(或活动主题)的functions.php 文件中。经测试有效。

不要忘记清空您的购物车以刷新运费缓存。

相关:Filter "woocommerce_cart_contents_weight" not applied for shipping weight