gpt4 book ai didi

php - 基于用户角色的 Woocommerce 最低订单总额

转载 作者:行者123 更新时间:2023-12-04 09:35:31 25 4
gpt4 key购买 nike

我正在使用来自 Woocommerce Minimum Order Amount 的片段代码设置最低订单总额。但我想为每个用户角色设置不同的最小值。
我有一些自定义用户角色:wholesale_prices , wholesale_vat_exc , 和 distributor_prices .我想让代码根据使用角色来工作,每个角色的最低金额不同。
这是我的代码:

// Minimum order total

add_action( 'woocommerce_check_cart_items', 'wc_minimum_order_amount' );

function wc_minimum_order_amount() {
// Set this variable to specify a minimum order value
$minimum = 300;

if ( WC()->cart->subtotal < $minimum ) {

if( is_cart() ) {

wc_print_notice(
sprintf( 'You must have an order with a minimum of %s to place your order, your current order total is %s.' ,
wc_price( $minimum ),
wc_price( WC()->cart->subtotal )
), 'error'
);

} else {

wc_add_notice(
sprintf( 'You must have an order with a minimum of %s to place your order, your current order total is %s.' ,
wc_price( $minimum ),
wc_price( WC()->cart->subtotal )
), 'error'
);

}
}

最佳答案

使用 Wordpress 条件函数 current_user_can()喜欢:

add_action( 'woocommerce_check_cart_items', 'wc_minimum_order_amount' );
function wc_minimum_order_amount() {
// minimum order value by user role
if ( current_user_can('distributor_prices') )
$minimum = 3000;
elseif ( current_user_can('wholesale_prices') )
$minimum = 1000;
elseif ( current_user_can('wholesale_vat_exc') )
$minimum = 600;
else
$minimum = 300; // default

if ( WC()->cart->subtotal < $minimum ) {

if( is_cart() ) {
wc_print_notice( sprintf(
'You must have an order with a minimum of %s to place your order, your current order total is %s.' ,
wc_price( $minimum ),
wc_price( WC()->cart->subtotal )
), 'error' );
} else {
wc_add_notice( sprintf(
'You must have an order with a minimum of %s to place your order, your current order total is %s.' ,
wc_price( $minimum ),
wc_price( WC()->cart->subtotal )
), 'error' );
}
}
}
代码位于事件子主题(或事件主题)的 functions.php 文件中。它应该有效。

关于php - 基于用户角色的 Woocommerce 最低订单总额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62615268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com