Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 根据购物车项目重量添加额外成本,以降低运费_Php_Wordpress_Woocommerce_Hook Woocommerce_Shipping Method - Fatal编程技术网

Php 根据购物车项目重量添加额外成本,以降低运费

Php 根据购物车项目重量添加额外成本,以降低运费,php,wordpress,woocommerce,hook-woocommerce,shipping-method,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_

我将下面的代码放在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;
}

请帮忙,谢谢

如果您不想向购物车添加费用,可以避免使用
woocommerce\u cart\u calculate\u fees
hook,并使用
woocommerce\u package\u rates
hook创建一个函数(结合两个函数):


该代码已经过测试,可以正常工作。将其添加到活动主题的functions.php中。

您可以使用
WC\u Cart
方法
get\u Cart\u contents\u 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;
}
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文件。测试和工作

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


处理多个装运包 由于购物车项目可以通过一些插件或自定义代码拆分为多个装运包,因此,在这种情况下,您将使用以下代码处理装运税计算

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;
}
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文件。测试和工作

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

相关的: