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

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


    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();

    }


}

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


幕布斯7119047
浏览 127回答 1
1回答

汪汪一只猫

以下代码将一个新字段添加到使用限制选项卡,您可以在其中添加允许的用户角色。// Add new field - usage restriction tabfunction action_woocommerce_coupon_options_usage_restriction( $coupon_get_id, $coupon ) {&nbsp; &nbsp; woocommerce_wp_text_input( array(&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'id' => 'customer_user_role',&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'label' => __( 'User role restrictions', 'woocommerce' ),&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'placeholder' => __( 'No restrictions', 'woocommerce' ),&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'description' => __( 'List of allowed user roles. Separate user roles with commas.', 'woocommerce' ),&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'desc_tip' => true,&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'type' => 'text',&nbsp;&nbsp;&nbsp; &nbsp; ));&nbsp;}add_action( 'woocommerce_coupon_options_usage_restriction', 'action_woocommerce_coupon_options_usage_restriction', 10, 2 );// Savefunction action_woocommerce_coupon_options_save( $post_id, $coupon ) {&nbsp; &nbsp; // Isset&nbsp; &nbsp; if ( isset ( $_POST['customer_user_role'] ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $coupon->update_meta_data( 'customer_user_role', sanitize_text_field( $_POST['customer_user_role'] ) );&nbsp; &nbsp; &nbsp; &nbsp; $coupon->save();&nbsp; &nbsp; }}add_action( 'woocommerce_coupon_options_save', 'action_woocommerce_coupon_options_save', 10, 2 );// Validfunction filter_woocommerce_coupon_is_valid( $valid, $coupon, $discount ) {&nbsp; &nbsp; // Get meta&nbsp; &nbsp; $customer_user_role = $coupon->get_meta('customer_user_role');&nbsp; &nbsp; // NOT empty&nbsp; &nbsp; if( ! empty( $customer_user_role ) ) {&nbsp; &nbsp; &nbsp; &nbsp; // Convert string to array&nbsp; &nbsp; &nbsp; &nbsp; $customer_user_role = explode( ', ', $customer_user_role );&nbsp; &nbsp; &nbsp; &nbsp; // Get current user role&nbsp; &nbsp; &nbsp; &nbsp; $user = wp_get_current_user();&nbsp; &nbsp; &nbsp; &nbsp; $roles = ( array ) $user->roles;&nbsp; &nbsp; &nbsp; &nbsp; // Compare&nbsp; &nbsp; &nbsp; &nbsp; $compare = array_diff( $roles, $customer_user_role );&nbsp; &nbsp; &nbsp; &nbsp; // NOT empty&nbsp; &nbsp; &nbsp; &nbsp; if ( ! empty ( $compare ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $valid = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( ! $valid ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Exception( __( 'My custom error message', 'woocommerce' ), 109 );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $valid;}add_filter( 'woocommerce_coupon_is_valid', 'filter_woocommerce_coupon_is_valid', 10, 3 );编辑:在CART页面自动应用优惠券并隐藏删除优惠券链接(基于用户角色)function action_woocommerce_before_calculate_totals( $cart ) {&nbsp; &nbsp; if ( is_admin() && ! defined( 'DOING_AJAX' ) )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // Only cart&nbsp; &nbsp; if( ! is_cart() )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /* SETTINGS */&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // Coupon code&nbsp; &nbsp; $coupon_code = 'test';&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // Allowed user role&nbsp; &nbsp; $allowed_user_role = 'administrator';&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; /* END SETTINGS */&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // check current user role&nbsp; &nbsp; $user = wp_get_current_user();&nbsp; &nbsp; $user_roles = ( array ) $user->roles;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // ADD js&nbsp; &nbsp; $add_js = false;&nbsp; &nbsp; // In array user roles&nbsp; &nbsp; if ( in_array( $allowed_user_role, $user_roles ) ) {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Format&nbsp; &nbsp; &nbsp; &nbsp; $coupon_code = wc_format_coupon_code( $coupon_code );&nbsp; &nbsp; &nbsp; &nbsp; // Applied coupons&nbsp; &nbsp; &nbsp; &nbsp; $applied_coupons = $cart->get_applied_coupons();&nbsp; &nbsp; &nbsp; &nbsp; // Is applied&nbsp; &nbsp; &nbsp; &nbsp; $is_applied = in_array( $coupon_code, $applied_coupons );&nbsp; &nbsp; &nbsp; &nbsp; // NOT applied&nbsp; &nbsp; &nbsp; &nbsp; if ( ! $is_applied ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Apply&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $cart->apply_coupon( $coupon_code );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $add_js = true;&nbsp; &nbsp; &nbsp; &nbsp; } elseif ( $is_applied ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // True&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $add_js = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // True&nbsp; &nbsp; &nbsp; &nbsp; if ( $add_js ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jQuery( function($) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Hide remove link&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $( '.woocommerce-remove-coupon' ).hide();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </script>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
打开App,查看更多内容
随时随地看视频慕课网APP