今天的目標是檢查具有特定運輸類的產品是否在購物車中,因此如果是真的,則禁用免費送貨。這是非常有用的,當有多個專案在購物車,您不想給免費送貨某些訂單。

WooCommerce Snippet(版本 2.6+):禁用免費送貨(對於特定運送區域)如果購物車具有特定運送類別的產品
/** * @snippet Disable Free Shipping if Cart has Shipping Class (WooCommerce 2.6+) * @sourcecode https://businessbloomer.com/?p=19960 * @author Rodolfo Melogli * @testedwith WooCommerce 2.6.1 */ add_filter( 'woocommerce_package_rates', 'businessbloomer_hide_free_shipping_for_shipping_class', 10, 2 ); function businessbloomer_hide_free_shipping_for_shipping_class( $rates, $package ) { $shipping_class_target = 15; // shipping class ID (to find it, see screenshot below) $in_cart = false; foreach( WC()->cart->cart_contents as $key => $values ) { if( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) { $in_cart = true; break; } } if( $in_cart ) { unset( $rates['free_shipping:8'] ); // shipping method with ID (to find it, see screenshot below) } return $rates; }
WooCommerce Snippet (version 2.5 and below): Disable Free Shipping if Cart has Product with Specific Shipping Class
/** * @snippet Disable Free Shipping if Cart has Shipping Class * @sourcecode https://businessbloomer.com/?p=19960 * @author Rodolfo Melogli * @testedwith WooCommerce 2.5.2 */ add_filter( 'woocommerce_package_rates', 'businessbloomer_hide_free_shipping_for_shipping_class', 10, 2 ); function businessbloomer_hide_free_shipping_for_shipping_class( $rates, $package ) { $shipping_class_target = 15; // shipping class ID (to find it, see screenshot below) $in_cart = false; foreach( WC()->cart->cart_contents as $key => $values ) { if( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) { $in_cart = true; break; } } if( $in_cart ) unset( $rates['free_shipping'] ); return $rates; }
WooCommerce: How to Find Shipping Class ID (Woo 2.6+, after the introduction of the new Shipping Zones)

WooCommerce: How to Find Shipping Method ID (Woo 2.6+, after the introduction of the new Shipping Zones)

WooCommerce: How to Find Shipping Class ID (Woo 2.5 and below)
可以在哪裡新增此程式碼?
您可以將 PHP 程式碼片段放置在主題或子主題的 functions.php 檔案的底部(如果是 CSS 程式碼,請新增到主題的 style.css 檔案底部),修改之前建議先備份原始檔案,若出現錯誤請先刪除此程式碼。
這段程式碼是否正常可用?
或是您有更好的解決方案想要分享?請到薇曉朵 WooCommerce 中文論壇 留言告知,我們希望可以幫到更多國內的 WooCommerce 使用者也希望您的參與。
需要關於 WooCommerce 的幫助 ?
請觀看我們提供的免費影片教程或到薇曉朵 WooCommerce 中文論壇提問,會有專業技術團隊提供相關幫助。