<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:
if ( !function_exists( 'chld_thm_cfg_locale_css' ) ):
    function chld_thm_cfg_locale_css( $uri ){
        if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . '/rtl.css' ) )
            $uri = get_template_directory_uri() . '/rtl.css';
        return $uri;
    }
endif;
add_filter( 'locale_stylesheet_uri', 'chld_thm_cfg_locale_css' );
         
if ( !function_exists( 'child_theme_configurator_css' ) ):
    function child_theme_configurator_css() {
        wp_enqueue_style( 'chld_thm_cfg_child', trailingslashit( get_stylesheet_directory_uri() ) . 'style.css', array( 'astra-theme-css','woocommerce-layout','woocommerce-smallscreen','woocommerce-general' ) );
    }
endif;
add_action( 'wp_enqueue_scripts', 'child_theme_configurator_css', 10 );
// END ENQUEUE PARENT ACTION

// Desativa o lightbox do Elementor na galeria do WooCommerce
add_action('wp_footer', function() {
    ?>
    <script>
    jQuery(document).ready(function($) {
        $(window).on('elementor/frontend/init', function() {
            elementorFrontend.hooks.addFilter(
                'elementor/lightbox/slideshow/action',
                function() { return false; }
            );
        });
        $(document).on('touchend', '.flex-control-thumbs li img', function(e) {
            e.preventDefault();
            e.stopPropagation();
            var index = $(this).closest('li').index();
            var $flexslider = $(this).closest('.woocommerce-product-gallery').find('.flexslider');
            $flexslider.flexslider(index);
            return false;
        });
    });
    </script>
    <?php
});

// Produtos relacionados por categoria principal
add_filter('woocommerce_related_products_args', function($args) {
    global $post;
    
    $categorias_principais = array('feminino', 'masculino', 'solar', 'clipon', 'clipon-feminino', 'clipon-masculino');
    
    $terms = get_the_terms($post->ID, 'product_cat');
    if ($terms) {
        $slugs = wp_list_pluck($terms, 'slug');
        $slugs_principais = array_intersect($slugs, $categorias_principais);
        
        if (!empty($slugs_principais)) {
            $args['category'] = array_values($slugs_principais);
        }
    }
    return $args;
});