Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.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/1/wordpress/11.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_Cart_Shipping - Fatal编程技术网

Php 如何为附加项目添加不同的装运价格

Php 如何为附加项目添加不同的装运价格,php,wordpress,woocommerce,cart,shipping,Php,Wordpress,Woocommerce,Cart,Shipping,在WooCommerce中,我试图根据购物车项目数量确定不同的运输价格 例如: 1项$5 2件物品$5+2美元 3件物品$5+(2 x 2$)…等等 因此,第一个项目将设置为5美元的成本,所有附加项目将为每个项目增加2美元 这可能吗?更新了-两个步骤: 1) 添加此代码,使您的运费计算成本基于购物车项目计数(项目总数): 代码位于活动子主题(或主题)的function.php文件或任何插件文件中 2) 现在,在常规配送设置中,您需要为每个配送区域设置一个“统一费率”配送方法,成本将设置为1(

在WooCommerce中,我试图根据购物车项目数量确定不同的运输价格

例如:

  • 1项$5
  • 2件物品$5+2美元
  • 3件物品$5+(2 x 2$)…等等
因此,第一个项目将设置为5美元的成本,所有附加项目将为每个项目增加2美元


这可能吗?

更新了-两个步骤:

1) 添加此代码,使您的运费计算成本基于购物车项目计数(项目总数):

代码位于活动子主题(或主题)的function.php文件或任何插件文件中

2) 现在,在常规配送设置中,您需要为每个配送区域设置一个“统一费率”配送方法,成本将设置为
1
(在您的WooCommerce配送设置中)

有时您需要刷新配送区域缓存(在每个区域的woocommerce配送设置中):
禁用每个统一费率,然后保存,然后启用此统一费率并保存。
最好从新的购物车开始(所以在测试之前清空它)


这段代码在WooCommerce 3+上进行了测试,可以正常工作

只需设置固定费率即可。。。不需要代码

只需激活固定费率航运

写下这条规则---

5+2*([数量]-1)

add_filter( 'woocommerce_package_rates', 'progressive_flat_rate_cost_calculation', 10, 2 );
function progressive_flat_rate_cost_calculation( $rates, $package )
{
    // The cart count (total items in cart)
    $cart_count = WC()->cart->get_cart_contents_count();
    $taxes = array();

    $cost = 5; // Cost for the 1st item
    $other = 2; // Cost for additional items

    // Calculation
    if( $cart_count > 1 )
        $cost += ($cart_count - 1) * $other;

    // Iterating through each shipping rate
    foreach($rates as $rate_key => $rate_values){
        $method_id = $rate_values->method_id;
        $rate_id = $rate_values->id;
        // Targeting "Flat Rate" shipping method
        if ( 'flat_rate' === $method_id ) {
            // Set the new calculated rate cost
            $rates[$rate_id]->cost = number_format($rates[$rate_id]->cost * $cost, 2);
            // Taxes rate cost (if enabled)
            foreach ($rates[$rate_id]->taxes as $key => $tax){
                if( $rates[$rate_id]->taxes[$key] > 0 ){ // set the new tax cost
                    $taxes[$key] = number_format( $rates[$rate_id]->taxes[$key] * $cost, 2 );
                    $has_taxes = true;
                } else {
                    $has_taxes = false;
                }
            }
            if( $has_taxes )
                $rates[$rate_id]->taxes = $taxes;
        }
    }
    return $rates;
}