亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

允許的用戶角色限制 WooCommerce 優惠券使用

允許的用戶角色限制 WooCommerce 優惠券使用

PHP
幕布斯7119047 2023-04-21 16:53:51
如果當前用戶角色是 ( 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();    }}我們現在需要實現的是將優惠券的使用僅限于用戶角色(供應商),如果另一個用戶角色甚至管理員嘗試使用它,他們會收到一條消息無效的優惠券。
查看完整描述

1 回答

?
汪汪一只貓

TA貢獻1898條經驗 獲得超8個贊

以下代碼將一個新字段添加到使用限制選項卡,您可以在其中添加允許的用戶角色。


// 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 ) {

    // Isset

    if ( isset ( $_POST['customer_user_role'] ) ) {

        $coupon->update_meta_data( 'customer_user_role', sanitize_text_field( $_POST['customer_user_role'] ) );

        $coupon->save();

    }

}

add_action( 'woocommerce_coupon_options_save', 'action_woocommerce_coupon_options_save', 10, 2 );


// Valid

function filter_woocommerce_coupon_is_valid( $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 ) ) {           

            $valid = false;

            

            if ( ! $valid ) {

                throw new Exception( __( 'My custom error message', 'woocommerce' ), 109 );

            }

        }

    }


    return $valid;

}

add_filter( 'woocommerce_coupon_is_valid', 'filter_woocommerce_coupon_is_valid', 10, 3 );

http://img1.sycdn.imooc.com//64424f5800013c5806570387.jpg

編輯:


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



查看完整回答
反對 回復 2023-04-21
  • 1 回答
  • 0 關注
  • 164 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號