【发布时间】:2021-01-06 03:59:28
【问题描述】:
根据Set custom shipping rates programmatically in Woocommerce 3的回答代码,我对其进行了修改,以便为每个卖家添加运费折扣。
add_filter( 'woocommerce_package_rates', 'custom_shipping_methods', 10, 2 );
function custom_shipping_methods( $rates, $package ) {
$reduction_cost_percentage = 30; // Discount percentage
foreach( WC()->cart->get_cart() as $cart_item ){
$in_cart_product_id = $cart_item['product_id'];
$cart_seller_id = get_post_field('post_author', $in_cart_product_id);
$cart_seller_meta = get_userdata($cart_seller_id);
$cart_seller_roles = $cart_seller_meta->roles;
if($cart_seller_roles[0] == 'seller'){
foreach( $rates as $rate_key => $rate ){
if( $rate->method_id != 'free_shipping'){
$rates[$rate_key]->cost = $rates[$rate_key]->cost * ((100-$reduction_cost_percentage) / 100);
return $rates;
}
}
}
}
}
现在我想从某个角色中排除这个折扣,例如seller_2。我该怎么做?
【问题讨论】:
标签: php wordpress woocommerce hook-woocommerce shipping-method