Woocommerce免费送货最低金额与货币换算

时间:2020-08-06 05:32:11

标签: php wordpress woocommerce hook-woocommerce shipping-method

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);

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

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

如果有人可以帮助我,我会很感激。

1 个答案:

答案 0 :(得分:2)

由于我真的不知道get_exchanged_currency()函数的工作原理,所以我不确定转换量的用法,但是逻辑在下面的代码中。

使用货币换算以最少的费用处理免费送货

  • 您首先需要为免费送货方式设置最低数量
  • 该代码将处理启用了该选项的优惠券的免费送货
  • 您将在代码中设置默认货币的最小金额。

如果要在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;
}

代码进入活动子主题(或活动主题)的functions.php文件中。应该可以。


已启用免费送货的适用优惠券:

当订单小计小于最小所需金额并且应用了可免费送货的优惠券时,将启用“免费送货”方法来隐藏其他送货方式。

enter image description here

刷新发货缓存:

  1. 此代码已保存在您的functions.php文件中。
  2. 在运输区域设置中,禁用/保存任何运输方式,然后启用回退/保存。

    您已完成,您可以对其进行测试。

相关:WooCommerce - Hide other shipping methods when FREE SHIPPING is available