2022-08-24

如何在 Woocommerce 購物車提示超重,引導專人協助

狀況是Woocommerce 購物車超重不能結單

客戶下單,但總重量多達 11Kg ,超過系統運費設定上限 3Kg,結果在結帳頁無法結帳卡住,因為沒有對應的運費。

解決方案是:當購物車商品超重,就直接隱藏 [繼續結帳] 的按鈕,並在頁面提示限重及客戶目前累積的重量,引導至專人處理,接下來分享如何修改。

需要修改主題的 functions.php

說明一下程式:

這個 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 );
請我喝杯咖啡吧 ^^

4 comments on “如何在 Woocommerce 購物車提示超重,引導專人協助”

  1. 想問一下如果我想設定購物車買到指定數量產品顯示特定字句,如購物車內有4盒會顯示買多1送1,這類可以做到嗎?

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

PeiYing Tsai
author
愛藝術,喜歡以科技解決問題的數位產品設計師。 堅持信念,任何困難事,必有搞定的一天,Just try it! 隨手紀錄日常學習到的知識心得,希望對妳/你有幫助。
coffee-cup linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram