WooCommerce 店主經常希望向其客戶提供默認的送貨方式。這裡我們提供兩種方式來實現默認配送方式的自動選中。
1 、設置特定的配送方式為默認,下方請修改 sto_kd 值為你自己設置的配送方式;
function reset_default_shipping_method( $method, $available_methods ) {
$default_method = 'wf_fedex_woocommerce_shipping:sto_kd'; //在此處提供將選擇默認值的服務名稱
selected default
if( array_key_exists($method, $available_methods ) )
return $default_method;
else
return $method;
}
add_filter('woocommerce_shipping_chosen_method', 'reset_default_shipping_method', 10, 2);
2 、將最便宜的送貨方式設置為默認方式,使用下方的代碼用戶結賬時,將會自動將最便宜的配送方式選中;
function wf_default_shipping_method( $method ) {
$the_cheapest_cost = 1000000;
$packages = WC()->shipping()->get_packages()[0]['rates'];
foreach ( array_keys( $packages ) as $key ) {
if ( ( $packages[$key]->cost > 0 ) && ( $packages[$key]->cost < $the_cheapest_cost ) ) {
$the_cheapest_cost = $packages[$key]->cost;
$method_id = $packages[$key]->id;
}
}
return $method_id;
}
add_filter( 'woocommerce_shipping_chosen_method', 'wf_default_shipping_method', 10 );
如何添加此代碼?
1 、您可以將 PHP 代碼片段放置在主題或子主題的 functions.php 文件的底部(如果是 CSS 代碼,請添加到子主題的 style.css 文件底部)修改之前建議先備份原始文件,若出現錯誤請先刪除此代碼。
2 、 WordPress 4.9 後改進了主題編輯器,對於 CSS 代碼也可打開網站前台編輯器的【自定義】,複製代碼添加到自定義 css 中。
此代碼是否可用?
如需幫助或是您有更好的方案想分享?請到薇曉朵 WooCommerce 中文論壇留言告知,我們希望可以幫到更多國內的 WooCommerce 用戶也希望您的參與。