繁体   English   中英

根据 WooCommerce 中客户的总订单更改特定国家/地区的运费

[英]Change shipping cost for specific countries based on total orders by customer in WooCommerce

我遵循这个完美的线程:

在 WooCommerce 中根据订单数量设置折扣

但就我而言,我需要将折扣应用于运费而不是订单总额,我只想针对特定国家/地区(特别是英国)执行此操作。

例如:

  • 仅首单:6 欧元(统一运费)
  • 从第二个订单开始,始终为 25 欧元(统一运费)

我已经将英国的统一费率设置为 6 欧元,但我需要了解如何仅从第二个订单开始收取 25 欧元的附加费。

这是我的代码尝试:

add_filter( 'woocommerce_package_rates', 'free_first_order_shipping', 20, 2 );
function free_first_order_shipping( $rates, $package ) {
if(is_user_logged_in()) {
    $user_id = get_current_user_id();
    //We want to know how many completed orders customer have or remove status if you dont care.
    $args = array(
        'customer_id' => 1,
        'status' => array('wc-completed'),
    );
    $orders = wc_get_orders($args);
    //If there are no orders returned apply free shipping
    if($orders == 0) {
      unset( $rates['flat_rate:15'] ); // Shipping 25 euro
     }
    else{
    unset( $rates['betrs_shipping:10-2'] ); // Shipping 6 euro
     }
        }           

return $rates;
}

但是,这并没有调整成本,而是删除了运输方式。 有什么建议吗?

关于您的代码尝试/问题的一些说明:

  • 您提到的答案是关于添加(负)费用, woocommerce_package_rates挂钩确实比woocommerce_cart_calculate_fees挂钩更适合您的问题
  • 您可以使用$package['destination']['country']来确定国家
  • unset( $rates['flat_rate:15'] )不会调整成本但会删除该方法
  • 要获得客户的总订单,您可以使用wc_get_customer_order_count() function

所以你得到:

function filter_woocommerce_package_rates( $rates, $package ) {
    // ONLY for specific countries
    $specific_countries = array( 'UK', 'BE' );

    // Checks if a value (country) exists in an array, if not return
    if ( ! in_array( $package['destination']['country'], $specific_countries ) ) return $rates;

    // Only for logged in users
    if ( is_user_logged_in() ) {
        // Get user ID
        $user_id = get_current_user_id();

        // Get the total orders by a customer.
        $count = wc_get_customer_order_count( $user_id );

        // Loop through
        foreach ( $rates as $rate_key => $rate ) {
            // Initialize
            $has_taxes = false;

            // Targeting "Flat Rate" shipping method
            if ( $rate->method_id == 'flat_rate' ) {
                // Get the initial cost
                $initial_cost = $new_cost = $rates[$rate_key]->cost;

                // Based on order count
                if ( $count == 0 ) {
                    // Set the new rate cost
                    $new_cost = 6;
                } else {
                    // Set the new rate cost
                    $new_cost = 25;
                }

                // Set the new cost
                $rates[$rate_key]->cost = $new_cost;

                // Taxes rate cost (if enabled)
                $taxes = [];

                // Loop through the shipping taxes array (as they can be many)
                foreach ($rates[$rate_key]->taxes as $key => $tax ) {
                    if ( $rates[$rate_key]->taxes[$key] > 0 ) {
                        // Get the initial tax cost
                        $initial_tax_cost = $new_tax_cost = $rates[$rate_key]->taxes[$key];

                        // Get the tax rate conversion
                        $tax_rate = $initial_tax_cost / $initial_cost;

                        // Set the new tax cost
                        $taxes[$key] = $new_cost * $tax_rate;

                        // Enabling tax
                        $has_taxes = true;
                    }
                }

                // When true
                if ( $has_taxes ) {
                    $rates[$rate_key]->taxes = $taxes;
                }
            }
        }
    }

    return $rates;
}
add_filter( 'woocommerce_package_rates','filter_woocommerce_package_rates', 10, 2 );

不要忘记清空购物车以刷新运输缓存数据!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM