Установите минимально допустимый вес для конкретной страны в WooCommerce

Я пытаюсь специально применить обязательный минимальный вес 20 кг для страны Колумбия, избегая проверки, если общий вес тележки меньше этого минимального веса.

Вот мой фактический код, который позволяет мне установить минимальный вес:

add_action( 'woocommerce_check_cart_items', 'cldws_set_weight_requirements' );
function cldws_set_weight_requirements() {
// Only run in the Cart or Checkout pages
if( is_cart() || is_checkout() ) {
global $woocommerce;
// Set the minimum weight before checking out
$minimum_weight = 30;
// Get the Cart's content total weight
$cart_contents_weight = WC()->cart->cart_contents_weight;
// Compare values and add an error is Cart's total weight
if( $cart_contents_weight < $minimum_weight  ) {
// Display our error message
wc_add_notice( sprintf('<strong>A Minimum Weight of %s%s is required before checking out.</strong>'
. '<br />Current cart weight: %s%s',
$minimum_weight,
get_option( 'woocommerce_weight_unit' ),
$cart_contents_weight,
get_option( 'woocommerce_weight_unit' ),
get_permalink( wc_get_page_id( 'shop' ) )
),
'error' );
}
}
}

Как я могу заставить это работать только для страны Колумбии?

2

Решение

Обновление (для округов доставки Аргентина и Колумбия)

Я пересмотрел ваш код и заставил его работать только для Колумбии с минимальным весом 20 кг. Вам нужно будет проверить единицу веса в порядке «Кг«(в килограммах).

Код:

add_action( 'woocommerce_check_cart_items', 'mandatory_required_weight_country_based' );
function mandatory_required_weight_country_based() {
// Only on Cart or Checkout pages
if( ! ( is_cart() || is_checkout() ) ) return;

// Get the shipping country
$country = WC()->session->get('customer')['shipping_country'];
if( empty($country) ){
$country = WC()->session->get('customer')['billing_country'];
}

// For Colombia and Argentina shipping countries
if( in_array( $country, array('CO', 'AR') ) ){

// HERE Set the minimum weight
$minimum_weight = 20; // 20 kg

// Get the Cart's content total weight
$total_weight = WC()->cart->get_cart_contents_weight();

// If total weight is lower than minimum, we avoid checkout and display an error notice
if( $total_weight < $minimum_weight  ) {
// Display an dynamic error notice
wc_add_notice( sprintf(
'<strong>A Minimum Weight of %s is required before checking out.</strong>'
. '<br />Current cart weight: %s',
wc_format_weight($minimum_weight),
wc_format_weight($total_weight)
), 'error' );
}
}
}

Код помещается в файл function.php вашей активной дочерней темы (или активной темы). Проверено и работает.

Когда Колумбия является обнаруженной страной (или определенной страной), вы получите что-то вроде:

введите описание изображения здесь

3

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector