为了账号安全,请及时绑定邮箱和手机立即绑定

基于 WooCommerce 中的购物车总数和用户角色的渐进折扣

基于 WooCommerce 中的购物车总数和用户角色的渐进折扣

PHP
回首忆惘然 2023-07-01 15:33:04
我在 WooCommerce 应答代码中使用基于购物车总计的渐进折扣来进行一些 Woocommerce 订单总折扣(见下文)。但我想根据用户角色进行折扣,因为我的每个客户角色看到的价格都不同。我有一些自定义用户角色:wholesale_prices、wholesale_vat_exc和distributor_prices。我想让代码仅适用于wholesale_prices和wholesale_vat_exc用户角色,但不适用于distributor_prices (因为他们不能看到折扣)。这是我实际重新访问的代码版本:// Order total discountadd_action( 'woocommerce_cart_calculate_fees', 'progressive_discount_based_on_cart_total', 10, 1 );function progressive_discount_based_on_cart_total( $cart_object ) {    if ( is_admin() && ! defined( 'DOING_AJAX' ) )        return;    $cart_total = $cart_object->cart_contents_total; // Cart total    if ( $cart_total >= 3000.00 && $cart_total < 5000.00 )        $percent = 15; // 15%    elseif ( $cart_total >= 1500.00 && $cart_total < 3000.00 )        $percent = 10; // 10%    elseif ( $cart_total >= 750.00 && $cart_total < 1500.00 )        $percent =  5; // 5%    else        $percent = 0;    if ( $percent != 0 ) {        $discount =  $cart_total * $percent / 100;        $cart_object->add_fee( "Bulk Order Discount ($percent%)", -$discount, true );    }}如何使此代码仅对wholesale_prices用户wholesale_vat_exc角色可用?
查看完整描述

1 回答

?
德玛西亚99

TA贡献1770条经验 获得超3个赞

如下所示:

add_action( 'woocommerce_cart_calculate_fees', 'progressive_discount_based_on_cart_total', 10, 1 );

function progressive_discount_based_on_cart_total( $cart ) {

    // HERE we target other user roles than 'distributor_prices' (allowing guests)

    if ( current_user_can('distributor_prices') && is_user_logged_in() )

        return;


    if ( is_admin() && ! defined( 'DOING_AJAX' ) )

        return;


    $cart_total = $cart->get_cart_contents_total(); // Cart total


    if ( $cart_total >= 3000.00 && $cart_total < 5000.00 )

        $percent = 15; // 15%

    elseif ( $cart_total >= 1500.00 && $cart_total < 3000.00 )

        $percent = 10; // 10%

    elseif ( $cart_total >= 750.00 && $cart_total < 1500.00 )

        $percent =  5; // 5%

    else

        $percent = 0;


    if ( $percent > 0 ) {

        $discount   = $cart_total * $percent / 100;

        $label_text = sprintf( __("Bulk Order Discount %s"), '('.$percent.'%)' );

        $cart->add_fee( $label_text, -$discount, true );

    }

}

代码位于活动子主题(或活动主题)的functions.php 文件中。它应该有效。


对于多个用户角色,您将使用wp_get_current_user()来获取当前WP_User对象,然后您可以获得roles如下属性:


$user       = wp_get_current_user();

$user_roles = $user->roles;  // An array of the user roles

然后你将在代码中替换:


    // HERE we target other user roles than 'distributor_prices' (allowing guests)

    if ( current_user_can('distributor_prices') && is_user_logged_in() )

        return;

经过:


    // HERE we target "wholesale_prices" and "wholesale_vat_exc" user roles (allowing guests)

    if ( ! array_intersect( wp_get_current_user()->roles, array('wholesale_prices', 'wholesale_vat_exc') && is_user_logged_in() )

        return;

或者


    // HERE we target "wholesale_prices" and "wholesale_vat_exc" user roles (allowing guests)

    if ( ! ( current_user_can('wholesale_prices') || current_user_can('wholesale_vat_exc') ) && is_user_logged_in() )

        return;


查看完整回答
反对 回复 2023-07-01
  • 1 回答
  • 0 关注
  • 87 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信