如果想在購物車上方顯示距離還要花費多少錢才享受特定優惠

只需要插入已下程式碼即可

add_action( 'woocommerce_before_cart', 'bbloomer_free_shipping_cart_notice' );
function bbloomer_free_shipping_cart_notice() {
  
   $min_amount = 1000; //change this to your free shipping threshold
   $current = WC()->cart->subtotal;
   
   if ( $current < $min_amount ) {
      $added_text = '您還差' . wc_price( $min_amount - $current ) . ' 可免運!';
      $return_to = wc_get_page_permalink( 'shop' );
      $notice = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), 'Continue Shopping', $added_text );
      wc_print_notice( $notice, 'notice' );
   } 
}

上方程式碼插入後即可得到以下效果

而$min_amount = 1000;

代表著設定顯示的免運門檻為1000元

以上方法 無法應用在有打折的情況(綠界物流)

若需要應用於綠界物流+折扣的情況

WC()->cart->subtotal要修改為WC()->cart->total

WC()->cart->subtotal > 小計

WC()->cart->total > 總計

所以程式碼為已下

add_action( 'woocommerce_before_cart', 'bbloomer_free_shipping_cart_notice' );
function bbloomer_free_shipping_cart_notice() {
  
   $min_amount = 1000; //change this to your free shipping threshold
   
   $current = WC()->cart->total;
   
   if ( $current < $min_amount ) {
      $added_text = '您還差' . wc_price( $min_amount - $current ) . ' 可免運!';
      $return_to = wc_get_page_permalink( 'shop' );
      $notice = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), 'Continue Shopping', $added_text );
      wc_print_notice( $notice, 'notice' );
   }
  
}
最後修改日期: 2021 年 4 月 15 日

作者