Php Woocommerce免运费最低金额(含货币换算)

Php Woocommerce免运费最低金额(含货币换算),php,wordpress,woocommerce,hook-woocommerce,shipping-method,Php,Wordpress,Woocommerce,Hook Woocommerce,Shipping Method,Woocommerce免费送货方式不适用于多种货币。 我定义了欧洲区免费送货的最低订单金额100 主要货币是欧元,它运行良好,但我转换国家(挪威)免费航运自动应用(因为挪威货币KR是非常低到欧元和免费航运只考虑最低挂载号码,而不是货币),它不是基于货币应用。转换器 add_filter('woocommerce_package_rates', function ($methods, $rates) { $currency = get_woocommerce_currency();

Woocommerce免费送货方式不适用于多种货币。 我定义了欧洲区免费送货的最低订单金额100

主要货币是欧元,它运行良好,但我转换国家(挪威)免费航运自动应用(因为挪威货币KR是非常低到欧元和免费航运只考虑最低挂载号码,而不是货币),它不是基于货币应用。转换器

add_filter('woocommerce_package_rates', function ($methods, $rates) {
    $currency = get_woocommerce_currency();
    foreach ((array)$methods as &$method) {
        if ($currency != 'USD' && $currency != 'GBP' && $currency != 'CHF') {
            // echo "Hello";
            // print_r($method->get_cost());
            $method->set_cost(round(get_exchanged_currency($currency, $method->get_cost(), true, 'EUR', '', true), 2));
        }
    }
    return $methods;
}, 10, 2);

以上代码可以很好地计算统一费率的运费

我想基于用户本地货币转换器实现免费送货(即,如果用户选择挪威国家,则免费送货最低订单价值=100欧元,仅当订单价值为1062.19 Kr时,才应用免费送货)


如果有人能帮我,我将不胜感激。

因为我不知道
兑换货币()函数如何工作,我不确定兑换金额的使用情况,但逻辑在下面的代码中

使用货币兑换以最低金额处理免费配送

  • 您首先需要为您的免费送货方式设置零的最小数量
  • 代码将处理启用该选项的应用优惠券的免费配送
  • 您将在代码中以默认货币设置最小金额
另外,要将多种货币作为IF/ELSE语句的条件处理,请改用
in_array()

守则:

add_filter('woocommerce_package_rates', 'filter_package_rates', 10, 2 );
function filter_package_rates( $rates, $package ) {
    $currency  = get_woocommerce_currency();
    $free = array();

    foreach ( $rates as $rate_key => $rate ) {
        // For "flat_rate" (or "local_pickup") and NOT for 'USD', 'GBP', 'CHF' currencies
        if ( ! in_array( $currency, array('USD', 'GBP', 'CHF') ) && 'free_shipping' !== $rate->method_id ) {
            $rates[$rate_key]->cost = round( get_exchanged_currency( $currency, $rate->cost, true, 'EUR', '', true ), 2 );
        }
        // For "free_shipping
        elseif ( 'free_shipping' === $rate->method_id ) {
            $cart_total = WC()->cart->subtotal; // (or WC()->cart->get_subtotal() without taxes)
            $min_amount = 100; // Free shipping min amount in EUR currency
            $min_amount = round( get_exchanged_currency( $currency, $min_amount, true, 'EUR', '', true ), 2 ); // Min amount convversion

            $free_shipping    = new \WC_Shipping_Free_Shipping( $rate->instance_id );
            $applied_coupons  = WC()->cart->get_applied_coupons();

            if ( 'either' === $free_shipping->requires && ! empty($applied_coupons) && $cart_total < $min_amount ) {
                foreach ( $applied_coupons as $coupon_code ) {
                    $coupon = new WC_Coupon( $coupon_code );
                    if( $coupon->get_free_shipping() ) {
                        $coupon_free_ship = true;
                        break;
                    }
                }
            }
            // Enable free shipping for a minimal cart amount or a coupon with free shipping option
            if( $cart_total < $min_amount && ! isset($coupon_free_ship) ) {
                unset($rates[$rate_key]);
            } else {
                $free[$rate_key] = $rate;
                break;
            }
        }
    }
    return ! empty( $free ) ? $free : $rates;
}
add_filter('woocommerce_package_rates','filter_package_rates',10,2);
函数过滤器\包\费率($rates,$package){
$currency=get_\u currency();
$free=array();
foreach($rate作为$rate\u key=>$rate的费率){
//适用于“统一汇率”(或“本地提货”),而非“美元”、“英镑”、“瑞士法郎”货币
如果(!in_数组($currency,array('USD','GBP','CHF'))和&“free_shipping”!==$rate->method_id){
$rates[$rate\u key]->cost=round(获取交换货币($currency,$rate->cost,true,'EUR','',true),2);
}
//免费送货
elseif('free\u shipping'=$rate->method\u id){
$cart\u total=WC()->cart->subtotal;//(或WC()->cart->get\u subtotal(),不含税)
$min_amount=100;//免费送货最低金额(欧元)
$min_amount=round(获取交换货币($currency,$min_amount,true,'EUR','',true),2);//最小金额转换
$free\u shipping=new\WC\u shipping\u free\u shipping($rate->instance\u id);
$applied_优惠券=WC()->购物车->获取应用的_优惠券();
如果('OR'==$free\u shipping->requires&&!empty($applicated\u优惠券)&&$cart\u total<$min\u amount){
foreach($优惠券作为$优惠券代码){
$coupon=新WC\U优惠券($coupon\U代码);
如果($优惠券->免费送货()){
$coupon\u free\u ship=true;
打破
}
}
}
//启用最低购物车金额的免费配送或具有免费配送选项的优惠券
if($cart_total<$min_amount&&!isset($coupon_free_ship)){
未设置($rates[$rate_key]);
}否则{
$free[$rate\u key]=$rate;
打破
}
}
}
返回!空($free)?$free:$rates;
}
代码放在活动子主题(或活动主题)的functions.php文件中。它应该可以工作


已启用免费配送选项的已应用优惠券: 当订单小计小于最低要求金额且应用了免费配送优惠券时,将启用免费配送方法以隐藏其他配送方法

刷新发货缓存:

  • 此代码已保存在functions.php文件中。
  • 在配送区域设置中,禁用/保存任何配送方法,然后启用返回/保存。

    您已经完成了,您可以进行测试。

  • 相关:

    请在您的问题中添加您用于多货币和货币转换的插件…我使用了WPML插件。下面的代码工作正常,但免费送货方法未自动应用。我想隐藏所有送货方法并自动应用免费送货。如果您有你有什么想法来解决这个问题?@LoicTheAztec非常感谢你的帮助。下面的代码运行良好,但免费送货方法不会自动应用。我想隐藏所有送货方法并自动应用免费送货。@LoicTheAztec非常感谢你。如果可能的话,给我一点提示,以便我可以解决这个问题并将其发送给客户。请您检查一下好吗?@Rigal抱歉,我在上次WooCommerce版本的店面主题下再次测试了我的代码,它也可以完美地用于免费送货的应用优惠券,因此在您的情况下还有其他问题。它可以是另一个自定义代码、您的主题或插件。