【问题标题】:Custom dynamic shipping rate cost calculation in WoocommerceWoocommerce中的自定义动态运费成本计算
【发布时间】:2018-08-28 06:30:27
【问题描述】:

我已经设置了自定义运输方式,每次用户进入购物车页面时我都需要计算运输成本。

似乎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();
}

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

【问题讨论】:

  • 您最好在您的问题中添加您的自定义代码... “寻求调试帮助的问题(“为什么此代码不起作用?”)必须包含所需的行为、特定问题或错误以及重现它在问题本身所必需的最短代码
  • @LoicTheAztec 我添加了一些 embryo 代码我试图了解更多关于正在发生的事情......
  • 能否在您的问题中添加您在woocommerce_package_rates 钩子中使用的自定义代码,请...您使用的似乎是v3 之前的WC 版本... 哪个?
  • @LoicTheAztec woocommerce v2.6.14
  • @LoicTheAztec 添加了woocommerce_package_rates 回调

标签: php wordpress woocommerce cart shipping


【解决方案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 文件中。测试和工作。它适用于 2.6 之后的所有 woocommerce 版本……

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

【讨论】:

  • 谢谢,我试过你的代码,但它没有解决问题,我想那是在别处。正如我在我的问题woocommerce_package_rates 中写的那样, 如果用户单击运输方式,则调用回调-我说的是购物车页面,而不是结帐页面。要用 mk 更新我的答案。 4 :)
  • @j.c 这个钩子在任何地方都有效,是定制运费的正确钩子……真的没有其他方法。因此,问题更多在于您在其中设置动态成本的方式。您也应该将 WC_Sessions 用于动态值……
  • WC_Sessions 和 WC_Shipping 是我正在查看的东西,但我很确定 woocommerce_package_rate 挂钩仅在用户更改运输方式时触发。 WC()-&gt;shipping-&gt;calculate_shipping(WC()-&gt;shipping-&gt;packages); 触发它,但我需要在正确的位置运行它。我正在尝试woocommerce_cart_totals_before_shipping钩子
猜你喜欢
  • 1970-01-01
  • 2019-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-07
  • 2019-01-04
  • 1970-01-01
  • 2019-04-03
相关资源
最近更新 更多