we need to make an automatic discount coupon to be applied if the current user role is ( vendor ), we achieved that by creating a normal coupon then used the snippet below to automatic apply the coupon .. but we need to limit the usage of that coupon to user role (vendor) only .. if another user role even administrators use it they get a message invalid 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();
}
}
what we need to achieve now is to limit that coupon usage to user role ( vendors ) only and if another user role even administrator tried to use it they get a message invalid coupon.
The following code adds a new field to the usage restriction tab where you can add allowed user roles.
// 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 );
EDIT:
To automatically apply the coupon on the CART page and hide the delete coupon link (based on user role)
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 );
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
JQuery is arguably the most popular JavaScript library with so many features for modern development. JQuery is a fast and concise JavaScript library created by John Resig in 2006. It is a cross-platform JavaScript library designed to simplify client-side HTML scripting. Over 19 million websites are currently using jQuery! Companies like WordPress, Facebook, Google, IBM and many more rely on jQuery to provide a kind of web browsing experience.
https://jquery.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.