首页 文章

根据WooCommerce中的属性值计算更改统一费率运费

提问于
浏览
2

我试图在不使用插件的情况下指定2种不同的统一费率运费方式:

  • 如果购物车中只有一个供应商的产品,则统一运费需要为19英镑 .

  • 如果购物车中有多个产品来自多个供应商,则统一费率运费需要为39英镑 .

我尝试了各种插件,但他们专注于根据尺寸,重量,数量,位置,类别而非属性或条款的运费 .

我有一个名为 Vendor 的属性,有8个术语 . 每个期限是不同的供应商/供应商 .

这是我想要实现的PHP逻辑类型:

if product attribute term quantity = 1

then flat rate = £19

else

if product attribute term quantity > 1

then flat rate = £39

如果购物车中有超过1个属性供应商条款,我该如何更改此“统一费率”送货方式费用?

1 回答

  • 3

    这个过程需要两个步骤:一些代码和一些设置......

    1) CODE - 当购物车商品来自多个供应商时,您可以使用挂钩在 woocommerce_package_rates 过滤器挂钩中的自定义功能,定位"Flat rate"送货方式:

    add_filter( 'woocommerce_package_rates', 'custom_flat_rate_cost_calculation', 10, 2 );
    function custom_flat_rate_cost_calculation( $rates, $package )
    {
    
        // SET BELOW your attribute slug… always begins by "pa_"
        $attribute_slug = 'pa_vendor'; // (like for "Color" attribute the slug is "pa_color")
    
    
        // Iterating through each cart item to get the number of different vendors
        foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    
            // The attribute value for the current cart item
            $attr_value = $cart_item[ 'data' ]->get_attribute( $attribute_slug );
    
            // We store the values in an array: Each different value will be stored only one time
            $attribute_values[ $attr_value ] = $attr_value;
        }
        // We count the "different" attribute values stored
        $count = count($attribute_values);
    
        // 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 ) {
                // For more than 1 vendor (count)
                if( $count > 1 ){
                    // Get the original rate cost
                    $orig_cost = $rates[$rate_id]->cost;
                    // Calculate the new rate cost
                    $new_cost = $orig_cost + 20; // 19 + 20 = 39
                    // Set the new rate cost
                    $rates[$rate_id]->cost = $new_cost;
                    // Calculate the conversion rate (for below taxes)
                    $conversion_rate = $new_cost / $orig_cost;
                    // Taxes rate cost (if enabled)
                    foreach ($rates[$rate_id]->taxes as $key => $tax){
                        if( $rates[$rate_id]->taxes[$key] > 0 ){
                            $new_tax_cost = number_format( $rates[$rate_id]->taxes[$key]*$conversion_rate, 2 );
                            $rates[$rate_id]->taxes[$key] = $new_tax_cost; // set the cost
                        }
                    }
                }
            }
        }
        return $rates;
    }
    

    代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中 .

    此代码使用woocommerce版本3进行测试并且有效

    2) SETTINGS - 上述代码保存在您的活动主题的function.php文件中后,您需要将"Flat rate"运费方法的费用设置为"Flat rate"(£19)(并保存) .

    重要提示:要刷新送货方式缓存,您需要禁用“固定费率”然后保存,并启用“固定费率”然后保存 .

    现在,这应该按预期工作 .

相关问题