php - 在 Woocommerce 中为特定国家/地区自定义计算运输方式成本

标签 php wordpress woocommerce country shipping-method

我正在寻找一种解决方案,允许我更改每个运输区域的成本,因为我还需要以我的第二种货币(从欧元到日元,日本)的舍入值。我使用 WC_Geolocation::geolocate_ip() 根据 IP 地址动态更改货币,但我找不到更改运输区域成本的解决方案。举例说明:

    $location = WC_Geolocation::geolocate_ip();
    $country = $location['country'];
    if($country === "JP"){
    //do some code to change every shipping zone cost with a custom value
    //(different for every shipping zone)
    } 

我希望我的解释很清楚。

最佳答案

已更新

You may have to "Enable debug mode" in general shipping settings under "Shipping options" tab, to disable temporarily shipping caches.

以下代码将更改特定国家/地区自定义计算的运输方式成本(此处为日本):

add_filter( 'woocommerce_package_rates', 'custom_shipping_rates_based_on_country', 20, 2 );
function custom_shipping_rates_based_on_country( $rates, $package ) {

    // ONLY for Japan
    if( $package['destination']['country'] !== 'JP' )
        return $rates;

    // Loop through shipping methods rates
    foreach( $rates as $rate_key => $rate ){

        // Excluding free shipping method
        if( $rate->method_id !== 'free_shipping') {

            // Get initial shipping rate cost
            $initial_cost = $rate->cost;

            ## ---- START ------------------------------------------- ##

            // Add your calculations and settings

            // Rounding decimal setting
            $rounding_decimals = 2;

            // Conversion rate
            $conversion_rate = 1.25;

            // New calculated cost
            $new_cost = $initial_cost * $conversion_rate;

            ## ----  END  ------------------------------------------- ##

            // Set the rate cost (rounded with 2 decimals)
            $rates[$rate_key]->cost = round( $new_cost, $rounding_decimals );

            // Taxes rate cost (if enabled)
            foreach ($rate->taxes as $key => $tax){
                if( $rate->taxes[$key] > 0 ){

                    // Calculating the tax rate unit
                    $tax_rate = $rate->taxes[$key] / $initial_cost;

                    // Calculating the new tax cost
                    $new_tax_cost = $tax_rate * $new_cost;

                    // set the new tax cost
                    $taxes[$key] = round( $new_tax_cost, $rounding_decimals );

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

        }
    }

    return $rates;
}

代码位于事件子主题(或事件主题)的 function.php 文件中。经过测试并有效。

Don't forget to enable back shipping cache.

关于php - 在 Woocommerce 中为特定国家/地区自定义计算运输方式成本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52759832/

相关文章:

php - 从日期 ('g:i a' 减去 6 小时,strtotime($time_date_data));

PHP json_encode 和 javascript 函数

php - 使用 PDO、准备好的语句和用 bindParam 替换标记来更新 MySQL;收到 'variables don' t 匹配 token 错误

wordpress - 是否可以在一次调用中从数组更新后元数据?

wordpress - 如何从指向由 google app engine 托管的应用程序的域托管 wordpress 博客?

php - Woocommerce:从插件获取一些自定义购物车项目数据值

css - Woocommerce 递增和递减按钮未对齐

PHPUnit Symfony v3.4 继承已弃用

css - 在不删除产品标题的情况下无法删除购物车中产品图片的表格列

php - 如何在 WordPress 查询中获取分类类别的所有记录?