I have variable products in woocommerce that have 3 skus. Each sku has2 options (size and color) and also has retail and wholesale pricing set for each variation. I’m using a plugin to set the wholesale pricing.
link to wholesale plugin

Based on size, in ounces, some of the skus have the price set to 0 for the retail customers.
A sample product is set up with skus as follows:

500ml - wholesale = $9.95 retail =$19.95 
250ml - wholesale $4.75 retail =$9.95 
1000ml wholesale = $39.95 retail = $0.00

Some skus are set to zero (0) for retail pricing because the 1000ml size is not available for retail shoppers. When a retail customer chooses the option of 1000ml I get the message “Sorry, this product is unavailable. Please choose a different combination.” Which is perfect. If a wholesale user is logged in and chooses 1000ml I want it to show the price and allow them to place it in their cart. But instead it shows the item with the price and the cart button is disabled. And, the message shows “Sorry, this product is unavailable. Please choose a different combination.”
see the link to screenshot example

I found the hook to show if a product is purchasable or not, but I don’t know how to alter it to allow purchases. Since I have several products set up in this way, it doesn’t seem to make sense to me to enter the product 2 times and hide the wholesale product from the general population. Is it even possible to do what I want to do, or is creating 2 products the way to go?

/** * Returns false if the product cannot be bought. * * @return bool */
        public function is_purchasable() {
                return apply_filters( 'woocommerce_is_purchasable', $this->exists() && ( 'publish' === $this->get_status() || current_user_can( 'edit_post', $this->get_id() ) ) && '' !== $this->get_price(), $this );

I found a snippet that gets the user role

function get_user_role() {
    global $current_user;

    $user_roles = $current_user->roles;
    $user_role = array_shift($user_roles);

    return $user_role;

So if I return the user role as wholesale_customer would this work?

function user_filter_addtocart_for_shop_page(){
$user_role = get_user_role();
$role_id = get_role('wholesale_customer');
if($user_role == $role_id){
    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );

Read more here: add to cart button work with price of zero


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: