Allow add to cart products of the same parent category for specific user role in WooCommerce

I need guidance on a code. It is currently working correctly for me. What the code does is restrict the combination of products from different categories, that is, only you can add to the cart products from one category at a time.

But now I need this code to be executed only when a specific role is connected, in this case, "Franchise" role. I modified the code, and put the condition to only apply to the role "Franchise", but it does not work properly, in effect, restricts me the combination of category in "Franchises", but in the other roles does not allow me to add product to the cart and is not what I want, should be allowed to add normally in the other roles existing, and combine products from different categories, assuming that you are conditioning for these restrictions only happen in "Franchise".

I would appreciate your help please

Here is my code attempt:

$user = wp_get_current_user();

if ( in_array( 'franquicia', (array) $user->roles ) ) {

    function avoid_add_to_cart_from_different_main_categories( $passed, $product_id, $quantity ) {

        
        $cart      = WC()->cart;
        $taxonomy  = 'product_cat';
        $ancestors = [];

        if( $cart->is_empty() )
             return $passed;

        $terms = (array) wp_get_post_terms( $product_id, $taxonomy, array('fields' => 'ids') );

        if( count($terms) > 0 ) {
            // Loop through product category terms  set for the current product
            foreach( $terms as $term) {
                foreach( get_ancestors( $term, $taxonomy ) as $term_id ) {
                    $ancestors[(int) $term_id] = (int) $term_id;
                }
            }

            // Loop through cart items
            foreach ( $cart->get_cart() as $item ) {
                $terms = (array) wp_get_post_terms( $item['product_id'], $taxonomy, array('fields' => 'ids') );
                if( count($terms) > 0 ) {
                    // Loop through product category terms set for the current cart item
                    foreach( $terms as $term) {
                        foreach( get_ancestors( $term, $taxonomy ) as $term_id ) {
                            $ancestors[(int) $term_id] = (int) $term_id;
                        }
                    }
                }
            }

            // When there is more than 1 parent product category
            if( count($ancestors) > 1 ) {
                wc_add_notice( __('Disculpa, NO puedes agregar a la misma orden/pedido productos que pertenezcan a distintas preventas o proveedores. Por favor, escoge solo productos que pertenezcan una misma preventa o proveedor.'), 'error' );
                $passed = false; 
            }
        }
        return $passed;
    }
}
add_filter( 'woocommerce_add_to_cart_validation', 'avoid_add_to_cart_from_different_main_categories', 10, 3 );

I would appreciate your help.

Upvotes: 1

Views: 108

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253784

You need to use the user role IF statement condition inside your function as follows:

function avoid_add_to_cart_from_different_main_categories( $passed, $product_id, $quantity ) {
    if( WC()->cart->is_empty() )
        return $passed;
        
    $user = wp_get_current_user();

    if ( in_array( 'franquicia', $user->roles ) ) {
        $taxonomy  = 'product_cat';
        $ancestors = [];

        $terms = (array) wp_get_post_terms( $product_id, $taxonomy, array('fields' => 'ids') );

        if( count($terms) > 0 ) {
            // Loop through product category terms  set for the current product
            foreach( $terms as $term) {
                foreach( get_ancestors( $term, $taxonomy ) as $term_id ) {
                    $ancestors[(int) $term_id] = (int) $term_id;
                }
            }

            // Loop through cart items
            foreach ( WC()->cart->get_cart() as $item ) {
                $terms = (array) wp_get_post_terms( $item['product_id'], $taxonomy, array('fields' => 'ids') );
                if( count($terms) > 0 ) {
                    // Loop through product category terms set for the current cart item
                    foreach( $terms as $term) {
                        foreach( get_ancestors( $term, $taxonomy ) as $term_id ) {
                            $ancestors[(int) $term_id] = (int) $term_id;
                        }
                    }
                }
            }

            // When there is more than 1 parent product category
            if( count($ancestors) > 1 ) {
                wc_add_notice( __('Disculpa, NO puedes agregar a la misma orden/pedido productos que pertenezcan a distintas preventas o proveedores. Por favor, escoge solo productos que pertenezcan una misma preventa o proveedor.'), 'error' );
                $passed = false; 
            }
        }
    }
    return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'avoid_add_to_cart_from_different_main_categories', 10, 3 );

Code goes in functions.php file of the active child theme (or active theme). It should better works.

Upvotes: 1

Related Questions