Woocommerce-通过挂钩更改运输成本(functions.php) [英] Woocommerce - change shipping cost via hooks (functions.php)
问题描述
我正在使用带有会员资格的Woocommerce订阅,我想根据用户拥有的会员计划对运输成本进行一些折扣.例如,银卡会员可享受所有运输方式-30%的折扣.要查看当前的会员计划,请使用:
I'm using Woocommerce Subscriptions with Memberships and I would like to apply some discounts on the shipping cost according to the membership plan my user have. For example, silver members have a discount of -30% of all shipping method. To check the current membership plan i use :
$user_id = get_current_user_id();// Get current user ID
if ( wc_memberships_is_user_active_member( $user_id, 'silver' ) ) {
//echo 'This is a special message only for Silver members!';
} else {
//echo 'I\'m sorry, you are not a silver member. There\'s nothing here for you!';
}
但是我找不到任何可以通过功能更改运输成本的方法.
But I don't find any way to change the shipping cost with functions..
有可能吗?谢谢
推荐答案
I have written some samples here: http://reigelgallarde.me/programming/woocommerce-shipping-fee-changes-condition-met/
add_filter( 'woocommerce_package_rates', 'woocommerce_package_rates' );
function woocommerce_package_rates( $rates ) {
$user_id = get_current_user_id();
if ( ! wc_memberships_is_user_active_member( $user_id, 'silver' ) ) { return $rates; }
$discount_amount = 30; // 30%
foreach($rates as $key => $rate ) {
$rates[$key]->cost = $rates[$key]->cost - ( $rates[$key]->cost * ( $discount_amount/100 ) );
}
return $rates;
}
以上代码将为您提供所有运输方式所需的功能.
Above code will do what you want for all shipping methods.
这篇关于Woocommerce-通过挂钩更改运输成本(functions.php)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!