首页 文章

Woocommerce中的自定义动态运费成本计算

提问于
浏览
2

我已经设置了自定义送货方式,每次用户进入购物车页面时我都需要计算运费 .

似乎 woocommerce_package_rates action(我计算自定义运费)只有在用户点击运送方式时才会运行 . 这样,大多数情况下购物车总数是错误的,最糟糕的是已经选择了自定义送货方式(用户没有更新) .

这是 woocommerce_package_rates 钩子的正常行为吗?

如何强制 woocommerce_package_rates 在显示购物车总数之前执行 always

EDIT

这是我试图破解的一些代码:

add_action( 'woocommerce_before_cart', 'force_shipping_calc', 1, 0 );
function force_shipping_calc() {
    foreach( WC()->session->get('shipping_for_package_0')['rates'] as $rate_id =>$rate) {
        // looking for the shipping method to recalc
        if($rate->method_id == 'flat_rate') {

            // mk1: set_shipping_total won't work, i'm using woocommerce < 3
            //WC()->cart->set_shipping_total( MY_calculate_shipping() );

            // mk2: doesn't work, "Indirect modification of overloaded property" 
            WC()->cart->totals['shipping_total'] = wc_format_decimal( MY_calculate_shipping(), wc_get_price_decimals() );

            // mk3: cart total nor shipping total affected (?!)
            WC()->cart->shipping_total = MY_calculate_shipping();

            // mk4: ... ?! work in progress...
    }
}

function MY_calculate_shipping() {
    return 99.99;
}


add_filter( 'woocommerce_package_rates', 'fty_shipping_flat_rate_cost_calculation', 10, 2 );
function fty_shipping_flat_rate_cost_calculation($rates, $package) {
    foreach($rates as $rate_key => $rate_values) {

        $method_id = $rate_values->method_id;
        $rate_id = $rate_values->id;

        if ( 'flat_rate' === $method_id ){

            $dist_cost  =   MY_calculate_shipping();

            $price_excl_tax = $rates[$rate_id]->cost + $dist_cost;
            $rates[$rate_id]->cost =  number_format($price_excl_tax, 2);

            $tax_keys   =   array_keys($rates[$rate_id]->taxes);

            $price_excl_tax = $rates[$rate_id]->cost + $dist_cost;
            $rates[$rate_id]->cost =  number_format($price_excl_tax, 2);

            $tax_calculation = $rates[$rate_id]->taxes[$tax_keys[0]] + $dist_cost*(TAX_AMOUNT_IVA-1);
            $rates[$rate_id]->taxes[$tax_keys[0]] = number_format($tax_calculation, 2);

            $rates[$rate_id]->cost +=   $dist_cost;
        }
    }

    return $rates;
}

EDIT ,再次

这(mk .~17786)似乎正朝着正确的方向发展 . 我更改了钩子并从WC_Shipping强制 calculate_shipping()

add_action( 'woocommerce_cart_totals_before_shipping', 'fty_force_calculate_shipping', 1, 2550 );
function fty_force_calculate_shipping() {
    WC()->shipping->calculate_shipping(WC()->shipping->packages);
    WC()->cart->calculate_totals();
}

但它还不完美,我认为这个钩子会在结帐页面中形成一个循环......

1 回答

  • 1

    这只需要在 woocommerce_package_rates 中完成 . 在您的代码中有许多错误或错误...请尝试以下操作:

    function custom_calculated_shipping() {
        return 99.99;
    }
    
    add_filter( 'woocommerce_package_rates', 'custom_shipping_rate_cost_calculation', 10, 2 );
    function custom_shipping_rate_cost_calculation( $rates, $package ) {
        foreach( $rates as $rate_key => $rate ) {
            if ( 'flat_rate' === $rate->method_id ){
    
                // Get rate cost and Custom cost
                $initial_cost = $rates[$rate_key]->cost;
                $additional_cost = custom_calculated_shipping();
    
                // Calculation
                $new_cost = $initial_cost + $additional_cost;
    
                // Set Custom rate cost
                $rates[$rate_key]->cost = round($new_cost, 2);
    
                // Taxes rate cost (if enabled)
                $new_taxes = array();
                $has_taxes = false;
                foreach ( $rate->taxes as $key => $tax ){
                    if( $tax > 0 ){
                        // Calculating the tax rate unit
                        $tax_rate = $tax / $initial_cost;
                        // Calculating the new tax cost
                        $new_tax_cost = $tax_rate * $new_cost;
                        // Save the calculated new tax rate cost in the array
                        $new_taxes[$key] = round( $new_tax_cost, 2 );
                        $has_taxes = true;
                    }
                }
                // Set new tax rates cost (if enabled)
                if( $has_taxes )
                    $rate->taxes = $new_taxes;
            }
        }
    
        return $rates;
    }
    

    代码位于活动子主题(或主题)的function.php文件中 . 经过测试和工作 . 它将适用于所有的woocommerce版本,从2.6 ...

    您应该刷新发货缓存:1)首先,此代码已保存在您的function.php文件中 . 2)空车 . 3)在装运设置中,输入装运区并禁用装运方法并“保存” . 然后重新启用“运输方式”并“保存” . 你完成了 .

相关问题