Hide coupon field for specific user role (wholesalers) in woocommerce - php

I am looking for a way to disable the coupon field for wholesalers in WooCommerce on the cart and checkout pages. I am using a custom theme and have the WooCommerce Role Based Methods plug-in in conjunction with WooCommerce Wholesale Pricing. I have tried the following in functions.php:
// hide coupon field on cart page for wholesale
function hide_coupon_field_on_cart( $enabled ) {
if( ! current_user_can( 'wholesale_customer' ) && is_cart() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_cart' );
// hide coupon field on checkout page for wholesale
function hide_coupon_field_on_checkout( $enabled ) {
if( ! current_user_can( 'wholesale_customer' ) && is_checkout() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_checkout' );
But that didn't work. What am I doing wrong?

Try this:
function woo_get_user_role() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
return $user_role;
}
// hide coupon field on cart page for wholesale
function hide_coupon_field_on_cart( $enabled ) {
if(woo_get_user_role() =='wholesale_customer' && is_cart() || is_checkout() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_cart' );
You can also merge both functions into one.

Try this:
$current_user = wp_get_current_user();
if ( !($current_user instanceof WP_User) )
return true;
$roles = $current_user->roles;
foreach($roles as $role){
// wholesaler is your role name, not display name
if( $role == "wholesaler" ){
$isWholesalers = 1;
}
}
if(!isset($isWholesalers)){
return true;
}
if(is_cart()/*is_checkout*/ )
return false;

Related

WooCommerce hide coupon field , but not for logged in users

I try to get my coupon field hidden but I would like to keep it on for logged-in users.
On this code, it does not work.
What can be my solution?
function hide_coupon_field_on_cart( $enabled ) {
if( !is_user_logged_in() )
return $disabled;
if ( is_cart() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_cart' );
function hide_coupon_field_on_cart( $enabled ) {
if( !is_user_logged_in() ){
return false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_cart' );

Optimize Woocommerce function that show specific products only in specific roles

I have the following function that runs on catalogue page and shows or hides specific products from users based on their role. If the user is customer then he can see the products that have checked the set_catalog_visibility (ACF).
The issue is that this function is slowing down my catalogue and i need to optimize it so it will run faster.
add_action('pre_get_posts', function( $query ){
$user = wp_get_current_user();
if ( $query->is_main_query() && is_woocommerce()) {
if (!check_user_role(array('customer','administrator')) || !is_user_logged_in() ) {
$product_ids = get_post_ids_by_meta_key_and_value('prd_clients', 1);
foreach($product_ids as $id){
// Get an instance of the product
$product = wc_get_product($id);
// Change the product visibility
$product->set_catalog_visibility('hidden');
// Save and sync the product visibility
$product->save();
}
}
else{
$product_ids = get_post_ids_by_meta_key_and_value('prd_clients', 1);
foreach($product_ids as $id){
// Get an instance of the product
$product = wc_get_product($id);
// Change the product visibility
$product->set_catalog_visibility('visible');
// Save and sync the product visibility
$product->save();
}
}
}
});
You can done it simply like this:
add_action(
'pre_get_posts',
function( $query ) {
if ( $query->is_main_query() && is_woocommerce() ) {
if ( ! is_user_logged_in() && ( is_user_logged_in() && ! check_user_role( array( 'customer', 'administrator' ) ) ) ) {
$product_ids = get_post_ids_by_meta_key_and_value( 'prd_clients', 1 );
if ( ! empty( $product_ids ) ) {
$query->set( 'post__not_in', $product_ids );
}
}
}
}
);

WooCommerce products: Allow backorders only for specific user roles

I want to allow backorders for specific customer roles.
Code i writed is:
// BackOrder Allow Part #1
add_filter( 'woocommerce_product_backorders_allowed', 'woocommerce_product_backorders_allowed', 10, 3 );
function woocommerce_product_backorders_allowed( $backorder_allowed, $product_id, $product ){
if ( current_user_can('administrator') ) {
$backorder_allowed = true;
} else {
$backorder_allowed = false;
}
return $backorder_allowed;
}
// BackOrder Allow Part #2
add_filter( 'woocommerce_product_get_stock_status', 'woocommerce_product_get_stock_status', 10, 2 );
function woocommerce_product_get_stock_status( $stock_status, $product ) {
if ( current_user_can('administrator') ) {
return 'onbackorder';
}
else {
return 'outofstock';
}
}
Seems this gived result only for simple product.
How i can make work for other product types.
I need to work for simple, variable and yith-bundle.
Any help will be appreciated!
thanks!
There are some mistakes and missing things in your code. Use the following instead:
// Custom conditional function targeting specific user roles
function is_allowed_user_role() {
$targeted_roles = array('administrator', 'shop_manager'); // Here define your targeted user roles
return (bool) array_intersect( wp_get_current_user()->roles, $targeted_roles );
}
add_filter( 'woocommerce_product_get_stock_status', 'filter_product_stock_status' );
add_filter( 'woocommerce_product_variation_get_stock_status', 'filter_product_stock_status' );
function filter_product_stock_status( $stock_status ) {
if ( ! is_allowed_user_role() && 'onbackorder' === $stock_status ) {
$stock_status = 'outofstock';
}
return $stock_status;
}
add_filter( 'woocommerce_product_get_backorders', 'filter_product_get_backorders' );
add_filter( 'woocommerce_product_variation_get_backorders', 'filter_product_get_backorders' );
function filter_product_get_backorders( $backorders ){
return is_allowed_user_role() ? $backorders : 'no';
}
add_filter( 'woocommerce_product_backorders_allowed', 'filter_product_backorders_allowed', 10, 3 );
function filter_product_backorders_allowed( $allowed, $product_id, $product ){
return is_allowed_user_role() ? $allowed : false;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
I did change on second function. If it's outofstock, like qty = 0 or stockstatus be outofstock, to allow backorder
// Custom conditional fuction to target specific user roles
function is_allowed_user_role() {
$targeted_roles = array('administrator', 'shop_manager'); // Here define your targeted user roles
return (bool) array_intersect( wp_get_current_user()->roles, $targeted_roles );
}
add_filter( 'woocommerce_product_get_stock_status', 'filter_product_stock_status' );
add_filter( 'woocommerce_product_variation_get_stock_status', 'filter_product_stock_status' );
function filter_product_stock_status( $stock_status ) {
if ( is_allowed_user_role() && 'outofstock' === $stock_status ) {
$stock_status = 'onbackorder';
}
return $stock_status;
}
add_filter( 'woocommerce_product_get_backorders', 'filter_product_get_backorders' );
add_filter( 'woocommerce_product_variation_get_backorders', 'filter_product_get_backorders' );
function filter_product_get_backorders( $backorders ){
return is_allowed_user_role() ? $backorders : 'no';
}
add_filter( 'woocommerce_product_backorders_allowed', 'filter_product_backorders_allowed', 10, 3 );
function filter_product_backorders_allowed( $allowed, $product_id, $product ){
return is_allowed_user_role() ? $allowed : false;
}

Redirection for non checkout guest allowed in WooCommerce

After Allow guest checkout for specific products only in WooCommerce answer to my previous question, the following code redirect users to login page:
add_action( 'template_redirect', 'checkout_redirect_non_logged_to_login_access');
function checkout_redirect_non_logged_to_login_access() {
if( is_checkout() && !is_user_logged_in()){
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
exit;
}
}
But I have some products which allows guest checkout (see the linked question/answer above). So how could I fix my code for the products which allows guest checkout to disable that code redirection?
You can replace my previous answer code with the following:
// Custom conditional function that checks if checkout registration is required
function is_checkout_registration_required() {
if ( ! WC()->cart->is_empty() ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $item ) {
// Check if there is any item in cart that has not the option "Guest checkout allowed"
if ( get_post_meta( $item['product_id'], '_allow_guest_checkout', true ) !== 'yes' ) {
return true; // Found: Force checkout user registration and exit
}
}
}
return false;
}
add_filter( 'woocommerce_checkout_registration_required', 'change_tax_class_user_role', 900 );
function change_tax_class_user_role( $registration_required ) {
return is_checkout_registration_required();
}
Then your current question code will be instead:
add_action( 'template_redirect', 'checkout_redirect_non_logged_to_login_access');
function checkout_redirect_non_logged_to_login_access() {
if( is_checkout() && !is_user_logged_in() && is_checkout_registration_required() ){
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
exit;
}
}
Code goes in functions.php file of your active child theme (or active theme). It should works.

Unable to Override WooCommerce Checkout Fields

I've created a custom WooCommerce checkout field with Woothemes Checkout Field Editor labeled "po_number". I would like the PO Number checkout field to only display for the user role "distributor".
So far I've been unsuccessful in overriding the checkout fields. I'm using Wordpress 4.5.1 / Woocommerce 2.5.5. Here's the code I've placed in my child theme's functions.php. I've also tested to make sure it is not a theme conflict.
Any help is greatly appreciated.
This is my code:
function custom_override_checkout_fields( $fields ) {
if ( ! current_user_can( 'distributor' ) && isset( $fields['billing']['po_number'] ) ) {
unset($fields['billing']['po_number']);
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
The current_user_can() function is related to capabilities of the user roles, but not to detect the user roles themselves. For that reason is not working in your code.
You need to set a conditional function for this purpose (user roles):
function is_user_role( $role, $user_id = null ) {
if ( is_numeric( $user_id ) ) {
$user = get_userdata( $user_id );
} else {
$user = wp_get_current_user();
}
if ( empty( $user ) ) {
return false;
}
if ( in_array( $role, (array) $user->roles ) == 1) {
return true;
} else {
return false;
}
}
Then in your code you can use that function:
function custom_override_checkout_fields( $fields ) {
if ( !is_user_role( 'distributor' ) && isset( $fields['billing']['po_number'] ) ) {
unset($fields['billing']['po_number']);
}
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
This should work in your code.

Categories