php – no cart validation with specific user role woocommerce

Question

I have the following code which separates products in my gas-tanks category from every other product in the store so the items cannot be purchased together.
It has worked fine for a long time, however, I have recently installed the wholesale for woocommerce plugin and I have just noticed that the code has stopped working.
The wholesale for woocommerce plugin offers different pricing based on user roles. The plugin seems to work fine for guest users, admins and all other standard users however it does not seem to run for users that have the wholesale_customer role?

There does not seem to be a logical reason as to why this would happen based on the codes functionality (at least none that i’m aware of)

//*** Prevent mixture of gas and other prods in same cart ***//
function dont_add_gas_to_cart_containing_other($validation, $product_id) {

    // Set flag false until we find a product in gas
    $cart_has_gas = false;

    // Set $cat_check true if a cart item is in gas cat
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {

        $product = $cart_item['data'];

        if (has_term('gas-tanks', 'product_cat', $product->id)) {
            $cart_has_gas = true;
            // break because we only need one "true" to matter here
            break;
        }
    }

    $product_is_gas = false;
    if (has_term('gas-tanks', 'product_cat', $product_id)) {
        $product_is_gas = true;
    }

    // Return true if cart empty
    if (!WC()->cart->get_cart_contents_count() == 0) {
        // If cart contains gas and product to be added is not gas, display error message and return false.
        if ($cart_has_gas && !$product_is_gas) {
            wc_add_notice( 'Sorry, you can only purchase Helium Gas products on their own. To purchase this product, please checkout your current cart or empty your cart and try again', 'error');
            $validation = false;
        }
        // If cart contains a product that is not gas and product to be added is gas, display error message and return false.
        elseif (!$cart_has_gas && $product_is_gas) {
            wc_add_notice('Sorry, you can only purchase Helium Gas products on their own. To purchase this product, please checkout your current cart or empty your cart and try again', 'error');
            $validation = false;
        }
    }
    // Otherwise, return true.
    return $validation;
}

add_filter('woocommerce_add_to_cart_validation', 'dont_add_gas_to_cart_containing_other', 10, 2);

code doesn’t seem to format?

0
Buttered_Toast 2 weeks 2021-09-09T05:48:43-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse