Hi Folks!
I understood that using of hook 'woocommerce_shipping_free_shipping_is_available' is not available to get the index of shipping zone from WooCommerce 2.6.0 -> from fact this not possible to use this hook.
After some investigation I understood that this task can be resolved in another way using hook 'woocommerce_package_rates', below is the code which do the job:
add_filter('woocommerce_package_rates', function($rates) {
if (version_compare(WOOCOMMERCE_VERSION, '2.6.0', '>='))
{
global $woocommerce;
$free_shipping_index = -1;
foreach ($rates as $rk => $rate)
{
if ($rate->method_id == 'free_shipping')
{
$free_shipping_id = explode(':', $rate->id);
$free_shipping_id = (int) $free_shipping_id[1];
$free_shipping_settings = get_option('woocommerce_free_shipping_' . $free_shipping_id . '_settings');
$allows_array = array('min_amount', 'either', 'both');
if (in_array($free_shipping_settings['requires'], $allows_array))
{
$min_amount = $free_shipping_settings['min_amount'];
$amount = floatval(preg_replace('#[^d.]#', '', $woocommerce->cart->get_cart_total()));
if ($min_amount > $amount)
{
$free_shipping_index = $rk;
}
//***
$free_shipping_coupon = false;
if (!empty($woocommerce->cart->applied_coupons))
{
$coupon = new WC_Coupon($woocommerce->cart->applied_coupons[0]);
$free_shipping_coupon_val = get_post_meta($coupon->id, 'free_shipping', true);
if ($free_shipping_coupon_val == 'yes')
{
//in in coupom enabled 'Allow free shipping' checkbox
$free_shipping_coupon = true;
}
}
if ($free_shipping_settings['requires'] == 'both')
{
if ($free_shipping_coupon AND $amount >= $min_amount)
{
$free_shipping_index = -1;
} else
{
$free_shipping_index = $rk;
}
}
//***
if ($free_shipping_settings['requires'] == 'either')
{
if ($free_shipping_coupon)
{
$free_shipping_index = -1;
}
}
}
break;
}
}
if ($free_shipping_index > -1)
{
unset($rates[$free_shipping_index]);
}
}
return $rates;
}, 9999);
برچسب:
نویسنده: استخدام کار