从 Woocommerce 中的自定义运费折扣中排除用户角色
Exclude user role from custom shipping discount in Woocommerce
根据答案代码,我修改了它,以便为每个卖家添加运费折扣。
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
。我该怎么做?
您的代码中存在一些错误和疏忽,请尝试以下操作:
add_filter( 'woocommerce_package_rates', 'custom_shipping_methods', 10, 2 );
function custom_shipping_methods( $rates, $package ) {
// Loop through cart items for the current package
foreach( $package['contents'] as $cart_item ){
$seller_id = get_post_field('post_author', $cart_item['product_id']);
$seller_data = get_userdata($seller_id);
// Excluding product 'seller' user role
if ( ! empty($seller_data) && is_array($seller_data->roles) && in_array('seller', $seller_data->roles) ) {
return $rates; // stop the loop and return normally the shipping rates
}
}
$percentage = 30; // <== Set your discount percentage
$discount_rate = $percentage / 100;
// Loop through shipping rates for the current package when seller user role is not found
foreach( $rates as $rate_key => $rate ){
// Not for free shipping
if( $rate->method_id != 'free_shipping' ){
// Change rate cost
$rates[$rate_key]->cost = $rate->cost * $discount_rate;
$taxes = array(); // Initializing
// change taxes rate cost (if enabled)
foreach ($rate->taxes as $key => $tax){
if( $tax > 0 ){
$taxes[$key] = $tax * $discount_rate;
$has_taxes = true;
}
}
// Change taxes cost
if( $has_taxes ) {
$rates[$rate_key]->taxes = $taxes;
}
}
}
return $rates;
}
代码进入您的活动子主题(活动主题)的 function.php 文件。它应该有效。
Clearing shipping caches:
- You will need to empty your cart, to clear cached shipping data
- Or In shipping settings, you can disable / save any shipping method, then enable back / save.
根据
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
。我该怎么做?
您的代码中存在一些错误和疏忽,请尝试以下操作:
add_filter( 'woocommerce_package_rates', 'custom_shipping_methods', 10, 2 );
function custom_shipping_methods( $rates, $package ) {
// Loop through cart items for the current package
foreach( $package['contents'] as $cart_item ){
$seller_id = get_post_field('post_author', $cart_item['product_id']);
$seller_data = get_userdata($seller_id);
// Excluding product 'seller' user role
if ( ! empty($seller_data) && is_array($seller_data->roles) && in_array('seller', $seller_data->roles) ) {
return $rates; // stop the loop and return normally the shipping rates
}
}
$percentage = 30; // <== Set your discount percentage
$discount_rate = $percentage / 100;
// Loop through shipping rates for the current package when seller user role is not found
foreach( $rates as $rate_key => $rate ){
// Not for free shipping
if( $rate->method_id != 'free_shipping' ){
// Change rate cost
$rates[$rate_key]->cost = $rate->cost * $discount_rate;
$taxes = array(); // Initializing
// change taxes rate cost (if enabled)
foreach ($rate->taxes as $key => $tax){
if( $tax > 0 ){
$taxes[$key] = $tax * $discount_rate;
$has_taxes = true;
}
}
// Change taxes cost
if( $has_taxes ) {
$rates[$rate_key]->taxes = $taxes;
}
}
}
return $rates;
}
代码进入您的活动子主题(活动主题)的 function.php 文件。它应该有效。
Clearing shipping caches:
- You will need to empty your cart, to clear cached shipping data
- Or In shipping settings, you can disable / save any shipping method, then enable back / save.