首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在Woocommerce中当购物车重量超过特定限制时的自定义消息

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

Stack Overflow用户
提问于 2018-06-07 05:00:58
回答 1查看 1.1K关注 0票数 1

在Woocommerce中,我需要创建一个自定义的错误消息,让客户知道他们的订单重量太多(超过70磅),他们需要拆分他们的订单,或者打电话给我们的商店,让我们的员工完成订单。到目前为止,我已经尝试将此代码添加到function.php文件中,但似乎不起作用。我在这方面是个新手,所以我确信我在这方面有一些不足之处。任何帮助都将不胜感激。

代码语言:javascript
复制
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.";
}
EN

回答 1

Stack Overflow用户

发布于 2018-06-07 05:55:20

当cart超过特定的重量限制时,下面的代码将:

  • 将在执行添加至购物车活动时显示错误通知。
  • 将删除所有发运方法,并将在购物车和结账页中显示自定义消息。
  • 将在提交订单时显示错误通知。

代码:

代码语言:javascript
复制
// 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文件中。经过测试,效果良好。

来测试您将需要刷新运输方法的代码。最好的方法是在一般的发布选项中启用调试模式(用于测试此代码)。然后,不要忘记禁用它回来。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50729527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档