Пользовательское сообщение, когда вес корзины превышает определенный лимит в Woocommerce - PullRequest
0 голосов
/ 07 июня 2018

В Woocommerce мне нужно создать собственное сообщение об ошибке, чтобы клиенты знали, что их вес заказа слишком велик (более 70 фунтов) и что им нужно разделить свой заказ или позвонить в наш магазин, чтобы наши сотрудники выполнилипорядок.До сих пор я пытался добавить этот код в файл function.php, но, похоже, он не работает.Я довольно новичок в этом, поэтому я уверен, что у меня есть что-то здесь.Любая помощь будет высоко ценится.

add_filter( 'woocommerce_no_shipping_available_html', 
'my_custom_no_shipping_message' );
add_filter( 'woocommerce_cart_no_shipping_available_html', 
'my_custom_no_shipping_message' );
function my_custom_no_shipping_message( $message ) {
   $cart_weight = WC()->cart->get_cart_contents_weight();
   if ($cart_weight >= 70 ){
  return "Your order exceeds the shipping limit weight.  Split into 
 multiple orders or call us to place order.";
}

1 Ответ

0 голосов
/ 07 июня 2018

Следующий код, когда корзина превышает определенный предел веса, будет:

  • отображать сообщение об ошибке при добавлении в корзину.
  • удалить все способы доставки и отобразитПользовательское сообщение в поле «нет доставки» (на страницах корзины и оформления заказа)
  • отображает сообщение об ошибке при отправке заказа.

Код:

// Add to cart validation - Add an error message when total weight exeeds a limit
add_filter( 'woocommerce_add_to_cart_validation', 'custom_price_field_add_to_cart_validation', 20, 3 );
function custom_price_field_add_to_cart_validation( $passed, $product_id, $quantity ) {
    $cart_weight = WC()->cart->get_cart_contents_weight();
    $product     = wc_get_product($product_id);
    $total_weight = ( $product->get_weight() * $quantity ) + $cart_weight;

    if ( $total_weight >= 70 ) {
        $passed = false ;
        $message = __( "Your order exceeds the shipping limit weight. Please contact us.", "woocommerce" );
        wc_add_notice( $message, 'error' );
    }
    return $passed;
}

// Disable all shipping methods when cart weight exeeds a limit
add_filter( 'woocommerce_package_rates', 'cart_weight_disable_shipping_methods', 20, 2 );
function cart_weight_disable_shipping_methods( $rates, $package ) {
    if( WC()->cart->get_cart_contents_weight() >= 70 ) {
        $rates = array();
    }
    return $rates;
}

// Display a custom shipping message when cart weight exeeds a limit
add_filter( 'woocommerce_no_shipping_available_html', 'weight_limit_no_shipping_message', 20, 1 );
add_filter( 'woocommerce_cart_no_shipping_available_html', 'weight_limit_no_shipping_message', 20, 1 );
function weight_limit_no_shipping_message( $message ) {
    if (WC()->cart->get_cart_contents_weight() >= 70 )
        $message = wpautop( __( "Your order exceeds the shipping limit weight. Split into multiple orders or call us to place order.", "woocommerce") );

    return $message;
}

// Display an error notice on order submit when cart weight exeeds a limit
add_action( 'woocommerce_checkout_process', 'cart_weight_limit_submit_alert', 20, 1 );
function cart_weight_limit_submit_alert() {
    if ( WC()->cart->get_cart_contents_weight() >= 70 ){
        $message = __( "Your order exceeds the shipping limit weight. Split into multiple orders or call us to place order.", "woocommerce");
        wc_clear_notices();
        wc_add_notice( $message, 'error' );
    }
}

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

Чтобы протестировать этот код, вам необходимо обновить методы доставки.Лучший способ - включить режим отладки в общих параметрах доставки (для тестирования этого кода).Тогда не забудьте отключить его обратно.

enter image description here


enter image description here


enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...