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 关注
- 103 浏览
添加回答
举报