/**
 * Theme Name: Rey Child
 * Theme URI: http://reytheme.com/
 * Description: This is a child theme of Rey.
 * Author: Marius H.
 * Author URI:  https://twitter.com/mariushoria
 * Template: rey
 * Version: 1.0.0
 * License: General Public License
 * License URI: http://www.gnu.org/licenses/gpl.html
 * Text Domain: rey-child
 */


add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

    // On s'assure de modifier uniquement la requête principale de l'archive produit
    if ( ! $q->is_main_query() ) {
        return;
    }
    if ( ! $q->is_post_type_archive() ) {
        return;
    }

    // Appliquer uniquement en front-end sur la page boutique
    if ( ! is_admin() && is_shop() ) {
        // Récupérer d'éventuels tax_query existants
        $tax_query = $q->get( 'tax_query' );
        if ( ! is_array( $tax_query ) ) {
            $tax_query = array();
        }
        // Ajouter la condition d'exclusion pour la catégorie PRODUITS PRIVES (ID 182)
        $tax_query[] = array(
            'taxonomy'         => 'product_cat',
            'field'            => 'id',
            'terms'            => array( 182 ),
            'operator'         => 'NOT IN',
            'include_children' => true,
        );
        $q->set( 'tax_query', $tax_query );
    }

    // Supprimer l'action pour éviter une ré-application lors de requêtes ultérieures
    remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
