我試圖根據其他運輸方式的可用性(通過它們的 ID)隱藏運輸方式,以實作有點復雜的運輸設定。
根據我發現的其他代碼片段(對于其他用例,不包括州或僅在可用時顯示免費送貨),我想出了這個:
function hide_duplicate_shipping_methods ( $rates ) {
foreach ( $rates as $rate_id => $rate ) {
if ( 'flat_rate:10' === $rate->method_id ) {
unset( $rates['flat_rate:28'] );
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_duplicate_shipping_methods', 100 );
但是,這并沒有隱藏任何東西,我真的找不到或想不到其他任何東西。
有什么建議嗎?
uj5u.com熱心網友回復:
$rate->method_id將等于local_pickup,free_shipping,flat_rate,等。while
$rate_id將等于local_pickup:1,free_shipping:2, 等等。
所以要么你像這樣使用它:
function filter_woocommerce_package_rates( $rates, $package ) {
// Loop trough
foreach ( $rates as $rate_id => $rate ) {
// Checks if a value exists in an array, multiple can be added, separated by a comma
if ( in_array( $rate->method_id, array( 'local_pickup', 'free_shipping' ) ) ) {
unset( $rates['flat_rate:28'] );
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );
或者像這樣:
function filter_woocommerce_package_rates( $rates, $package ) {
// Loop trough
foreach ( $rates as $rate_id => $rate ) {
// Checks if a value exists in an array, multiple can be added, separated by a comma
if ( in_array( $rate_id, array( 'local_pickup:1', 'free_shipping:2' ) ) ) {
unset( $rates['flat_rate:28'] );
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'filter_woocommerce_package_rates', 10, 2 );
代碼位于functions.php活動子主題(或活動主題)的檔案中。在 Wordpress 5.8.1 和 WooCommerce 5.8.0 中測驗和作業
不要忘記清空您的購物車以重繪 運輸快取資料。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/331144.html
標籤:WordPress的 求购 大车 邮寄方式
