如果您想要為特定國家/地區僅顯示一種運送方式,則需要一些額外的編碼。在此示例中,我們將禁用所有運送方式,但 “免費送貨” 。這裡是新增到您的 functions.php 的 PHP 程式碼

程式碼段#1:在 1 個運送區域中取消設定 1 個免費版本

要找到新的 “運送方式名稱”,例如 “ free_shipping:8
  /**   * @snippet       Hide one shipping option in one zone when Free Shipping is available     * @sourcecode    https://businessbloomer.com/?p=260   * @author        Rodolfo Melogli   * @compatible    WooCommerce 3.1.1   */    add_filter( 'woocommerce_package_rates', 'bbloomer_unset_shipping_when_free_is_available_in_zone', 10, 2 );     function bbloomer_unset_shipping_when_free_is_available_in_zone( $rates, $package ) {   	   	// Only unset rates if free_shipping is available    	if ( isset( $rates['free_shipping:8'] ) ) {    	unset( $rates['flat_rate:1'] );  }  	  	  return $rates;    }    

程式碼段#1:在可用空閒時,在所有區域中取消設定所有費率

  /**   * @snippet       Hide ALL shipping rates in ALL zones when Free Shipping is available     * @sourcecode    https://businessbloomer.com/?p=260   * @author        Rodolfo Melogli   * @compatible    WooCommerce 3.1.1   */    add_filter( 'woocommerce_package_rates', 'bbloomer_unset_shipping_when_free_is_available_all_zones', 10, 2 );     function bbloomer_unset_shipping_when_free_is_available_all_zones( $rates, $package ) {   	   	$all_free_rates = array();  	          foreach ( $rates as $rate_id => $rate ) {  		if ( 'free_shipping' === $rate->method_id ) {  			$all_free_rates[ $rate_id ] = $rate;  			break;  		}  	}  	  	if ( empty( $all_free_rates )) {          return $rates;          } else {          return $all_free_rates;          }   }    

對於舊版本的 WooCommerce [2.1-2.5](當 Free 可用時,取消設定一個速率)

  /**   * @snippet       Hide one shipping option when Free Shipping is available     * @sourcecode    https://businessbloomer.com/?p=260   * @author        Rodolfo Melogli   * @compatible    WooCommerce 2.4.10   */    add_filter( 'woocommerce_package_rates', 'bbloomer_unset_shipping_when_free_is_available', 10, 2 );     function bbloomer_unset_shipping_when_free_is_available( $rates, $package ) {   	   	// Only unset rates if free_shipping is available    	if ( isset( $rates['free_shipping'] ) ) {    		unset( $rates['flat_rate'] );    		return $rates;  }    }    

對於舊版本的 WooCommerce [2.1-2.5](免費提供時,設定所有費率)

  /**   * @snippet       Hide ALL shipping options when Free Shipping is available     * @sourcecode    https://businessbloomer.com/?p=260   * @author        Rodolfo Melogli   * @compatible    WooCommerce 2.4.10   */    add_filter( 'woocommerce_package_rates', 'bbloomer_unset_shipping_when_free_is_available', 10, 2 );     function bbloomer_unset_shipping_when_free_is_available( $rates, $package ) {   	   	// Only unset rates if free_shipping is available    	if ( isset( $rates['free_shipping'] ) ) {    	    	$free_shipping = $rates['free_shipping'];  	$rates = array();  	$rates['free_shipping'] = $free_shipping;  	}  	  	return $rates;  }    
請注意:如果您無法使其工作,您必須清除 WooCommerce 快取(WooCommerce> 系統狀態)。見下圖。

達到 WooCommerce 2.1

  // Hide standard shipping option when free shipping is available  add_filter( 'woocommerce_available_shipping_methods', 'hide_standard_shipping_when_free_is_available' , 10, 1 );     /**   *  Hide Standard Shipping option when free shipping is available   *    * @param array $available_methods   */  function hide_standard_shipping_when_free_is_available( $available_methods ) {         if( isset( $available_methods['free_shipping'] ) AND isset( $available_methods['flat_rate'] ) ) {             // remove standard shipping option          unset( $available_methods['flat_rate'] );      }         return $available_methods;  }    


可以在哪裡新增此程式碼?

您可以將 PHP 程式碼片段放置在主題或子主題的 functions.php 檔案的底部(如果是 CSS 程式碼,請新增到主題的 style.css 檔案底部),修改之前建議先備份原始檔案,若出現錯誤請先刪除此程式碼。


這段程式碼是否正常可用?

或者是您有更好的解決方案想要分享?請到薇曉朵 WooCommerce 中文論壇留言告知,我們希望可以幫到更多國內的 WooCommerce 使用者也希望您的參與。


需要關於 WooCommerce 的幫助?

請觀看我們提供的免費影片教程或到薇曉朵 WooCommerce 中文論壇提問,會有專業技術團隊提供相關幫助。