首页 > 解决方案 > 每 X 步增加统一运费,最少。基本费用-Woocommerce

问题描述

在 WooCommerce 商店中,我有各种产品可以根据数量断点确定运费。

IE:

到目前为止,我有这个,但它并没有为每个项目添加。它将返回它找到的第一个类的结果,然后停止。

因此,如果有一个Class1_1项目和一个Class2_1项目,它只会为第一个项目做方程式。

我需要做什么来制作这个循环和所有产品线项目?

 add_filter( 'woocommerce_package_rates', 'different_rates_based_on_quantity_class1_3', 10, 2 );

    function different_rates_based_on_quantity_class1_3 ( $rates, $package ){

        $item_count = 0;

        // Checking in cart items
        foreach( WC()->cart->get_cart() as $cart_item ){


        $slug_class_shipping =  $cart_item['data']->get_shipping_class();


        switch ($slug_class_shipping) {
            case "class1_3":
                $step_change = 3;
                break;
            case "class1_2":
                $step_change = 2;
                break;
            case "class3_1":
                $step_change = 1;
                break;
            default:
                 $step_change = 1;
        }
        // If we find the shipping class
        if( $cart_item['data']->get_shipping_class() == $slug_class_shipping ){

        $item_count += $cart_item['quantity']; // Cart item count
        $rate_operand = ceil( $item_count / $step_change ); // Operand increase each # items here

        foreach ( $rates as $rate_key => $rate ){
            // Targetting "Flat rate"
            if( 'flat_rate' === $rate->method_id ) {
                $has_taxes = false; 

                // Set the new cost
                $rates[$rate_key]->cost = $rate->cost * $rate_operand;

                // Taxes rate cost (if enabled)
                foreach ($rates[$rate_key]->taxes as $key => $tax){
                    if( $tax > 0 ){
                        // New tax calculated cost
                        $taxes[$key] = $tax * $rate_operand;
                        $has_taxes = true;
                    }
                }
                // Set new taxes cost
                if( $has_taxes )
                    $rates[$rate_key]->taxes = $taxes;
            }
        }    return $rates;
        } break; } } 

编辑:第一次粘贴错误的代码

标签: phpwordpresswoocommerceshippingrate

解决方案


推荐阅读