Woocommerce hooks not working on child theme

19 Views Asked by At

I'm trying to build a filter for woocommerce product but it seems that woocommerce hooks placed in the functions.php file of my child them are not working...

I have this function:

<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}


add_action( 'wp_ajax_filter_products', 'filter_products' );
add_action( 'wp_ajax_nopriv_filter_products', 'filter_products' );

function filter_products() {
    // Vérifier si les variables sont définies et ne sont pas vides
    $styles = isset( $_POST['styles'] ) ? $_POST['styles'] : array();
    $countries = isset( $_POST['country'] ) ? $_POST['country'] : array();

    $args = array(
        'post_type' => 'product',
        'tax_query' => array(
            'relation' => 'AND', // Utiliser la relation "ET" pour correspondre à tous les filtres
        ),
    );

    if ( ! empty( $styles ) ) {
        $args['tax_query'][] = array(
            'taxonomy' => 'styles',
            'field' => 'slug',
            'terms' => $styles,
            'operator' => 'AND'
        );
    }

    if ( ! empty( $countries ) ) {
        $args['tax_query'][] = array(
            'taxonomy' => 'country',
            'field' => 'slug',
            'terms' => $countries,
            'operator' => 'AND'   
        );
    }

    $products = new WP_Query( $args );


    if ( $products->have_posts() ) {
        // Output header and description
        ?>
        <header class="woocommerce-products-header">
            <?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>
                <h1 class="woocommerce-products-header__title page-title"><?php woocommerce_page_title(); ?></h1>
            <?php endif; ?>

            <?php do_action( 'woocommerce_archive_description' ); ?>
        </header>
        <?php

        // Output notices, result count, and ordering
        do_action( 'woocommerce_before_shop_loop' );

        // Start the product loop
        woocommerce_product_loop_start();

        // Loop through products
        while ( $products->have_posts() ) {
            $products->the_post();

            // Hook: woocommerce_shop_loop.
            do_action( 'woocommerce_shop_loop' );

            wc_get_template_part( 'content', 'product' );
        }

        // End the product loop
        woocommerce_product_loop_end();

        // Output pagination
        woocommerce_pagination();

    } else {
        // If no products found, show no products found message
        do_action( 'woocommerce_no_products_found' );
    }

    // Reset post data
    wp_reset_postdata();

    // Terminate script
    die();
}

So it just display a loop of 10 product, but no pagination, no archive title etc...

Any suggestion?

I've tried to copy and paste the wc-template-hooks.php file in my child theme but it doesn't work

0

There are 0 best solutions below