Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1466010

    Dear Support Team,

    I hope this message finds you well. I am currently working on customizing the product catalog module on my website and need some assistance. I would like to adapt the module so that it displays category classes for products, similar to how the product grid module handles category classes or the related product section in single product pages. Could you please provide guidance or a solution on how to integrate category classes into the product catalog module so that appears at the “class=”av-catalogue-item product_type_simple product-nr-2” so that I can add CSS class for the design that only applied with the product category needed, a bit similar to the approach used in the product grid module?

    Thank you in advance for your assistance. I look forward to your guidance on this matter. Best regards

    #1466011

    Hey manyfloor,
    Thank you for your question, but the product catalog already includes category classes such as:
    product_cat-music
    product_cat-accessories
    product_cat-hoodies

    these are basic examples from the woo demo import.
    Please provide more detail with example links to your site so we can understand better and reproduce, perhaps what you call a “product catalog module” we call something else?
    A link to your pages and a admin login would help a lot.
    As I understand, you only want DOM classes for CSS and not the classes to show on the frontend.

    Best regards,
    Mike

    #1466013

    Hi Mike,

    I hope you’re doing well.

    I’m currently working on a page (url down below) that features a list of products displayed using the catalogue module. Each product belongs to specific categories that I’ve created, such as:

    product_cat-domaine-du-chene-vert
    product_cat-vallee-du-rhone-nord

    For instance, these categories apply to the product “Crozes-Hermitage millésime 2022 / Rouge.”

    However, I’ve noticed that the product category classes are not included in the HTML structure of the catalogue module. Specifically, they do not appear in the “li” or “a” tags, which makes it challenging for me to apply CSS modifications based on product categories.

    Currently, only the data-product_id attribute is present, but this isn’t practical for my needs. Since the product inventory will continue to grow, I require a solution that automatically includes product category classes in the “li” or “a” tags for CSS purposes. I don’t need these categories to be visible in the front-end module, just added to the HTML for styling.

    Could you please advise on how I can achieve this?

    Thank you for your help.

    Best regards,

    • This reply was modified 2 months, 2 weeks ago by manyfloor.
    #1466024

    Hi,

    Thank you for the info.

    Please add this filter in the functions.php file:

    function avf_add_category_class_to_cart_link($link, $product) {
        $categories = get_the_terms($product->get_id(), 'product_cat');
    
        $category_class = '';
        if ($categories && !is_wp_error($categories)) {
            $category_class = ' product_cat-' . esc_attr($categories[0]->slug);
        }
    
        $link = str_replace('class="av-catalogue-item', 'class="av-catalogue-item' . $category_class, $link);
    
        return $link;
    }
    add_filter('woocommerce_loop_add_to_cart_link', 'avf_add_category_class_to_cart_link', 10, 2);
    

    Best regards,
    Ismael

    #1466025

    Hi Ismael,

    Thanks, it works like a charm after clearing the cache.

    I made two modifications for better results, and everything seems to be working fine now. First, I adjusted the code to retrieve all product categories instead of just the first one. Additionally, I applied the filter specifically to the “a” link to avoid affecting the “div” below, which contains the class .av-catalogue-item-inner that includes the same string part .av-catalogue-item:

    function avf_add_category_class_to_cart_link($link, $product) {
        $categories = get_the_terms($product->get_id(), 'product_cat');
    
        $category_classes = '';
        if ($categories && !is_wp_error($categories)) {
            foreach ($categories as $category) {
                $category_classes .= ' product_cat-' . esc_attr($category->slug);
            }
        }
    
        $link = preg_replace(
            '/(<a[^>]*class="av-catalogue-item)([^"]*)"/',
            '$1' . $category_classes . '$2"',
            $link
        );
    
        return $link;
    }
    add_filter('woocommerce_loop_add_to_cart_link', 'avf_add_category_class_to_cart_link', 10, 2);

    Thanks for your help and your quick response!

    #1466037

    Hi,
    Glad Ismael could help, if you have any further questions please create a new thread and we will gladly try to help you. Thank you for using Enfold.

    Best regards,
    Mike

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘How to get product categories class with the catalogue product module in ALB’ is closed to new replies.