在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.";
}
发布于 2018-06-07 05:55:20
当cart超过特定的重量限制时,下面的代码将:
代码:
// 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文件中。经过测试,效果良好。
来测试您将需要刷新运输方法的代码。最好的方法是在一般的发布选项中启用调试模式(用于测试此代码)。然后,不要忘记禁用它回来。
https://stackoverflow.com/questions/50729527
复制相似问题