【问题标题】:Progressive discount based on cart total and user roles in WooCommerce基于购物车总数和 WooCommerce 中用户角色的累进折扣
【发布时间】:2020-10-18 02:59:13
【问题描述】:

我正在使用 Progressive discount based on cart total in WooCommerce 回答代码来获得一些 Woocommerce 订单总折扣(见下文)。但我想根据用户角色打折,因为我的每个客户角色看到不同的价格。

我有一些自定义用户角色:wholesale_priceswholesale_vat_excdistributor_prices

我想让代码仅适用于 wholesale_priceswholesale_vat_exc 用户角色,但不适用于 distributor_prices (因为他们一定看不到折扣)

这是我实际重新访问的代码版本:

// Order total discount

add_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_priceswholesale_vat_exc 用户角色?

【问题讨论】:

    标签: php wordpress woocommerce cart discount


    【解决方案1】:

    正如您在 Apply a discount for a specific user role in Woocommerce 相关答案中看到的,我使用 WordPress 条件函数 current_user_can() 来定位特定的用户角色……

    所以你需要在Progressive discount based on cart total in WooCommerce回答代码中使用它,就像:

    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;
    

    【讨论】:

    • 谢谢你!如何为此添加多个角色:if ( ! current_user_can('wholesale_prices') ) ?
    • 好的,我想我找到了:if ( ! current_user_can('wholesale_customer') &amp;&amp; !current_user_can('wholesale_vat_exc') )
    • 一段时间后我意识到上面的代码只适用于注册用户,如何包括访客用户?这是当前代码:if ( ! array_intersect( wp_get_current_user()-&gt;roles, array('wholesale_customer', 'wholesale_vat_exc') ) ) return;
    • @HuseinYuseinov 我已经更新了我的答案代码……现在它也应该允许客人了。
    • 非常感谢您的回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 2019-02-21
    • 1970-01-01
    相关资源
    最近更新 更多