Created
October 21, 2023 16:30
-
-
Save alexdeborba/1d8b44ea9569e4b15e0879218804fd95 to your computer and use it in GitHub Desktop.
Wholesale Customers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Allow wholesale customers to view all products | |
function allow_wholesale_customer_view_all_products($query) { | |
// Check if the user has the "wholesale_customer" role and if it's the main query | |
if (current_user_can('wholesale_customer') && $query->is_main_query()) { | |
// Set the query to retrieve products only | |
$query->set('post_type', 'product'); | |
// Set the post status to include both published and private products | |
$query->set('post_status', ['publish', 'private']); | |
// Ignore sticky posts | |
$query->set('ignore_sticky_posts', true); | |
// Retrieve all products (no limit) | |
$query->set('posts_per_page', -1); | |
} | |
} | |
// Hook the function to the 'pre_get_posts' action | |
add_action('pre_get_posts', 'allow_wholesale_customer_view_all_products'); | |
// Allow wholesale customers to backorder products | |
function allow_wholesale_customer_backorder($product_data) { | |
// Check if the user has the "wholesale_customer" role | |
if (current_user_can('wholesale_customer')) { | |
// Disable stock management for the product | |
$product_data['manage_stock'] = false; | |
// Set the stock status to 'onbackorder' | |
$product_data['stock_status'] = 'onbackorder'; | |
} | |
// Return the modified product data | |
return $product_data; | |
} | |
// Hook the function to the 'woocommerce_product_get_stock_status' filter | |
add_filter('woocommerce_product_get_stock_status', 'allow_wholesale_customer_backorder'); | |
// Hook the function to the 'woocommerce_product_variation_get_stock_status' filter | |
add_filter('woocommerce_product_variation_get_stock_status', 'allow_wholesale_customer_backorder'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment