1 回答

TA貢獻1831條經驗 獲得超10個贊
如果特定運輸類別的總物品數為 5 或更多,以下內容將隱藏特定定義的運輸方式:
add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package ) {
$targeted_class_ids = array(182); // Shipping Class To Find
$allowed_max_qty = 4; // Max allowed quantity for the shipping class
$shipping_rates_ids = array( // Shipping Method rates Ids To Hide
'wf_shipping_ups:07',
'wf_shipping_ups:08',
'wf_shipping_ups:11',
'wf_shipping_ups:54',
'wf_shipping_ups:65',
'wf_shipping_ups:70',
'wf_shipping_ups:74',
'free_shipping:2',
'request_shipping_quote'
);
$related_total_qty = 0;
// Checking cart items for current package
foreach( $package['contents'] as $key => $cart_item ) {
if( in_array( $cart_item['data']->get_shipping_class_id(), $targeted_class_ids ) ){
$related_total_qty += $cart_item['quantity'];
}
}
// When total allowed quantity is more than allowed (for items from defined shipping classes)
if ( $related_total_qty > $allowed_max_qty ) {
// Hide related defined shipping methods
foreach( $shipping_rates_ids as $shipping_rate_id ) {
if( isset($rates[$shipping_rate_id]) ) {
unset($rates[$shipping_rate_id]); // Remove Targeted Methods
}
}
}
代碼位于活動子主題(或活動主題)的functions.php 文件中。未經測試它應該有效。
刷新運輸緩存:
此代碼已保存在您的functions.php 文件中。
在運輸區域設置中,禁用/保存任何運輸方式,然后啟用返回/保存。
你已經完成了,你可以測試它。
處理物品數量而不是物品累計數量:
代替:
$related_total_qty += $cart_item['quantity'];
經過
$related_total_qty++;
- 1 回答
- 0 關注
- 133 瀏覽
添加回答
舉報