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

时间:2018-03-19 16:01:13

标签: php wordpress woocommerce cart shipping

我已经设置了自定义送货方式,我需要在每次用户到达购物车页面时计算运费。

似乎woocommerce_package_rates操作(我计算自定义运费)只有在用户点击送货方式时才会运行。这样,大多数情况下购物车总数都是错误的,最糟糕的是已经选择了自定义送货方式(用户不需要点击它,因此其成本不会更新)。

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

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

修改

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

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;
}

编辑,再次

这(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 个答案:

答案 0 :(得分:2)

这只需要在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)在送货设置中,输入送货区并禁用送货方式和“保存”。然后重新启用送货方式和“保存”。 你已经完成了。