当购物车重量超过Woocommerce中的特定限制时的自定义消息

时间:2018-06-06 21:00:58

标签: php wordpress woocommerce attributes cart

在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 :(得分:1)

当购物车超过特定重量限制时,以下代码将:

  • 在添加到购物车操作时显示错误通知。
  • 删除所有送货方式,并在“无送货区域”(在购物车和结帐页面中)显示自定义信息
  • 在订单提交时显示错误通知。

代码:

// 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