下面是我用来定义和设置基于购物车总数的运费的函数:


function adjust_shipping_rate( $rates, $package ) {
    if (is_admin() && !defined('DOING_AJAX')) {
        return;
    }

    $cart_total = WC()->cart->get_subtotal();
    // Define shipping rates based on cart total
    $shipping_rates = [
        ['min' => 0, 'max' => 50, 'rate' => 20],
        ['min' => 50.1, 'max' => 100, 'rate' => 15],
        ['min' => 100.1, 'max' => 200, 'rate' => 10],
        ['min' => 200.1, 'max' => PHP_INT_MAX, 'rate' => 5]
    ];

    // Find the appropriate shipping rate
    foreach ($shipping_rates as $range) {
        if ($cart_total >= $range['min'] && $cart_total <= $range['max']) {
            foreach ($rates as $rate_key => $rate) {
                if (strpos($rate_key, 'flat_rate') === 0) {
                    $rates[$rate_key]->cost = $range['rate'];
                }
            }
            break;
        }
    }
    
    return $rates;
}
add_filter( 'woocommerce_package_rates', 'adjust_shipping_rate', 10, 2 );

我确认,如果没有这个功能,运费货币正在正确转换,但有了这个功能,成本保持不变,但格式正在转换.例如,而不是5美元航运转换为4,61 €,它转换为5,00 €.

我们正在使用WooPayments及其多货币费率.

如何获取当前货币兑换率,以便在函数中自己进行兑换?我知道get_woocommerce_currency()抓取货币代码,所以我希望有一些东西可以抓取WooPayments中设置的货币汇率,但我找不到任何类似的情况.

我是不是搞错了?我不想安装一个插件的运费,如果我没有.

推荐答案

似乎插件WooPayments插件已经在使用woocommerce_package_rates钩子,所以也许降低钩子优先级将采取之前的手,允许WooPayments插件转换,使其工作在您的定制运费.

此外,您的代码可以被简化/优化.最后,您当前代码中缺少税率处理.

try 以下方法:

add_filter( 'woocommerce_package_rates', 'custom_flat_rate_shipping_cost', 1, 2 );
function custom_flat_rate_shipping_cost( $rates, $package ) {
    // Define shipping rates costs based on cart subtotal thresholds
    $threshold_shipping_rate_cost = [
        ['threshold' => 200,  'rate' => 5],
        ['threshold' => 100,  'rate' => 10],
        ['threshold' => 50,   'rate' => 15],
        ['threshold' => 0,    'rate' => 20],
    ];

    // Get Cart Subtotal excl. taxes for the current shipping package
    $subtotal = $package['contents_cost']; 

    // Find the appropriate shipping rate
    foreach ($rates as $rate_key => $rate) {
        if ( 'flat_rate' !== $rate->method_id ) continue;

        foreach ($threshold_shipping_rate_cost as $data) {
            if ( $subtotal >= $data['threshold']) {
                $base_cost = $rate->cost; // Original rate cost
                $has_taxes = false; // Initializing
                $taxes     = array(); // Initializing

                $rates[$rate_key]->cost = $data['rate']; // Set the new cost

                // Loop through taxes array (change taxes rate cost if enabled)
                foreach ($rate->taxes as $key => $tax){
                    if( $tax > 0 ){
                        $conv_rate   = $tax / $base_cost; // Get the conversion tax rate
                        $taxes[$key] = $data['rate'] * $conv_rate; // Set the new tax cost in the array
                        $has_taxes   = true; // Enabling tax changes
                    }
                } 
                // Set the array of shipping tax cost (if any)
                if( $has_taxes ) {
                    $rates[$rate_key]->taxes = $taxes;
                }
                break;
            }
        }
    }
    return $rates;
}

代码将在子主题的functions.php文件中(或插件中).它可以工作.

Important:您必须清空购物车以刷新送货方式缓存.

注意,$package['contents_cost'] (or 101)是购物车小计excluding taxes.购物车小计including taxes,使用$package['cart_subtotal'] (or 103).

Php相关问答推荐

WordPress:将自定义单选输入字段添加到用户配置文件

如何限制WordPress自定义分类术语页面中的术语数量

EBay Inventory API createOrReplaceInventoryItem出错

将SVG包含在另一个php生成的SVG中

返回WooCommerce中的所有已启用变体

限制某些产品只能在WooCommerce的特定邮政编码/邮政编码范围内发货

模式 bootstrap 未显示

PHP Match如何准确判断条件?

如何在 PHP 中以 hh:mm:ss 格式获取两个 strtotime datetime/s 之间的时间差

PHP API - 本地主机中的 SSL 证书问题

批量更新 WooCommerce 中所有产品的特定自定义字段

使用 foreach php 将记录正确分配给正确的父级

WordPress 分页无法正常工作

在 groupBy laravel 5.7 中 Select 多列

如何在WooCommerce管理产品列表中显示运输类别列

Laravel的Storage::exists返回false,而file_exists返回true.

如何在构建 PHP 类别数组树的递归函数中将父面包屑传递给子项?

如何修复正则表达式以在换行符后查找非数值

输出每个可能的产品选项

PHP 自动函数参数和变量