客戶下單,但總重量多達 11Kg ,超過系統運費設定上限 3Kg,結果在結帳頁無法結帳卡住,因為沒有對應的運費。
解決方案是:當購物車商品超重,就直接隱藏 [繼續結帳] 的按鈕,並在頁面提示限重及客戶目前累積的重量,引導至專人處理,接下來分享如何修改。
說明一下程式:
這個 function 負責在購物車小計區塊顯示過重,請洽專人服務的提示,描述在 print 內修改
add_filter( 'woocommerce_cart_no_shipping_available_html', 'change_noship_message' );
function change_noship_message() {
print "Your order is too heavy. Please <a href='https://yoururl' target='_blank'>contact us</a> to get the shipping fee.";
}
這個 function 負責移除購物車的結帳按鈕,並說明目前訂單重量及限重。
要調整限重,可以修改 $max_allowed 的值,
要調整提示描述,修改 wc_add_notice 的內容
function action_woocommerce_check_cart_items() {
if( is_cart()) {
// Max allowed weight
$max_allowed = 3;
// Get weight of items in the cart.
$cart_total_weight = WC()->cart->get_cart_contents_weight();
// If weight in cart greater than allowed weight, show error message
if ( $cart_total_weight > $max_allowed ) {
// Notice
wc_add_notice( sprintf( __( 'Allowed max weight is %s, You have %s in the shopping cart.<br/>Please contact us to get the shipping fee'),
'<strong>' . wc_format_weight($max_allowed) . '</strong>',
'<strong>' . wc_format_weight($cart_total_weight) . '</strong>'
), 'error' );
// Remove proceed to checkout button
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
}
}
add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10, 0 );
//Woocommerce 過重,無運費套用,請洽專人服務
//在購物車小計區塊顯示過重,請洽專人服務
add_filter( 'woocommerce_cart_no_shipping_available_html', 'change_noship_message' );
function change_noship_message() {
print "Your order is too heavy. Please <a href='https://seedemo.online/website/pure/contact/' target='_blank'>contact us</a> to get the shipping fee.";
}
//在購物車移除結帳按鈕,並說明目前訂單重量及限重
function action_woocommerce_check_cart_items() {
if( is_cart()) {
// Max allowed items
$max_allowed = 3;
// Get number of items in the cart.
$cart_total_weight = WC()->cart->get_cart_contents_weight();
// If items in cart greater than allowed amounts, show error message
if ( $cart_total_weight > $max_allowed ) {
// Notice
wc_add_notice( sprintf( __( 'Allowed max weight is %s, You have %s in the shopping cart.<br/>Please contact us to get the shipping fee'),
'<strong>' . wc_format_weight($max_allowed) . '</strong>',
'<strong>' . wc_format_weight($cart_total_weight) . '</strong>'
), 'error' );
// Remove proceed to checkout button
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
}
}
add_action( 'woocommerce_check_cart_items' , 'action_woocommerce_check_cart_items', 10, 0 );
cool~您是自己接案嗎?
謝謝,可以來信交流喔 ^^
想問一下如果我想設定購物車買到指定數量產品顯示特定字句,如購物車內有4盒會顯示買多1送1,這類可以做到嗎?
可以,在這個功能:
//在購物車移除結帳按鈕,並說明目前訂單重量及限重
function action_woocommerce_check_cart_items()
將原本對重量的判斷,改判斷特定商品是否大於 4,顯示文案調成買多1送1
請參考取得特定產品數量的教學:
https://stackoverflow.com/questions/44631312/how-to-get-particular-product-quantity-from-the-cart-page-in-the-woocommerce