允许的用户角色限制WooCommerce优惠券的使用

时间:2020-05-30 01:56:42

标签: php wordpress woocommerce hook-woocommerce coupon

如果当前用户角色是(供应商),我们需要制作一个自动折扣优惠券,我们通过创建一个普通优惠券然后使用下面的代码片段自动应用优惠券来实现。.但是我们需要限制该优惠券仅用于用户角色(供应商)的情况..如果另一个用户角色甚至管理员使用它,他们也会收到消息无效的优惠券

    add_action( 'woocommerce_before_cart', 'apply_matched_coupons' );

function apply_matched_coupons() {
    global $woocommerce;

    $coupon_code = 'freeee'; // coupon code

    if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;

if ( current_user_can('yith_vendor') ) {
        $woocommerce->cart->add_discount( $coupon_code );
        wc_print_notices();
    }

}

我们现在需要实现的是将优惠券的使用仅限于用户角色(供应商),如果另一个用户角色甚至管理员尝试使用它,他们也会收到消息无效的优惠券。

1 个答案:

答案 0 :(得分:1)

以下代码将新字段添加到使用限制标签,您可以在其中添加允许的用户角色。

// Add new field - usage restriction tab
function action_woocommerce_coupon_options_usage_restriction( $coupon_get_id, $coupon ) {
    woocommerce_wp_text_input( array( 
        'id' => 'customer_user_role',  
        'label' => __( 'User role restrictions', 'woocommerce' ),  
        'placeholder' => __( 'No restrictions', 'woocommerce' ),  
        'description' => __( 'List of allowed user roles. Separate user roles with commas.', 'woocommerce' ),  
        'desc_tip' => true,  
        'type' => 'text',  
    )); 
}
add_action( 'woocommerce_coupon_options_usage_restriction', 'action_woocommerce_coupon_options_usage_restriction', 10, 2 );

// Save
function action_woocommerce_coupon_options_save( $post_id, $coupon ) {
    update_post_meta( $post_id, 'customer_user_role', $_POST['customer_user_role'] );
}
add_action( 'woocommerce_coupon_options_save', 'action_woocommerce_coupon_options_save', 10, 2 );

// Valid
function filter_woocommerce_coupon_is_valid( $is_valid, $coupon, $discount ) {
    // Get meta
    $customer_user_role = $coupon->get_meta('customer_user_role');

    // NOT empty
    if( ! empty( $customer_user_role ) ) {
        // Convert string to array
        $customer_user_role = explode(', ', $customer_user_role);

        // Get current user role
        $user = wp_get_current_user();
        $roles = ( array ) $user->roles;

        // Compare
        $compare = array_diff( $roles, $customer_user_role );

        // NOT empty
        if ( ! empty ( $compare ) ) {           
            $is_valid = false;
        }
    }

    return $is_valid;
}
add_filter( 'woocommerce_coupon_is_valid', 'filter_woocommerce_coupon_is_valid', 10, 3 );

enter image description here


编辑:

要在 CART 页面上自动应用优惠券并隐藏删除优惠券链接(根据用户角色)

function action_woocommerce_before_calculate_totals( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

   if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Only cart
    if( ! is_cart() )
        return;

    /* SETTINGS */

    // Coupon code
    $coupon_code = 'test';

    // Allowed user role
    $allowed_user_role = 'administrator';

    /* END SETTINGS */

    // check current user role
    $user = wp_get_current_user();
    $user_roles = ( array ) $user->roles;

    // ADD js
    $add_js = false;

    // In array user roles
    if ( in_array( $allowed_user_role, $user_roles ) ) {        
        // Format
        $coupon_code = wc_format_coupon_code( $coupon_code );

        // Applied coupons
        $applied_coupons = $cart->get_applied_coupons();

        // Is applied
        $is_applied = in_array( $coupon_code, $applied_coupons );

        // NOT applied
        if ( ! $is_applied ) {
            // Apply
            $cart->apply_coupon( $coupon_code );

            // True
            $add_js = true;
        } elseif ( $is_applied ) {
            // True
            $add_js = true;
        }

        // True
        if ( $add_js ) {
            ?>
            <script type="text/javascript">
            jQuery( function($) {               
                // Hide remove link
                $( '.woocommerce-remove-coupon' ).hide();
            });
            </script>
            <?php
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );