<?php
/*
Theme Name: Astra Child
Template: astra
Version: 1.0
*/

/* Mostrar solo categorías Stamina y Ropa Roly aunque estén vacías */
add_filter('woocommerce_product_subcategories_args', function($args) {
    $args['slug__in'] = ['stamina', 'ropa-roly']; // Solo estas categorías
    $args['hide_empty'] = false; // Mostrar aunque estén vacías
    return $args;
});

/* Mostrar estas categorías destacadas en la tienda principal */
add_action('astra_woocommerce_shop_template', function() {

    if (!is_shop()) return; // Solo en página principal de tienda

    $categories = get_terms([
        'taxonomy'   => 'product_cat',
        'slug'       => ['stamina', 'ropa-roly'],
        'hide_empty' => false,
    ]);

    if (empty($categories) || is_wp_error($categories)) return;

    echo '<section class="custom-forced-categories woocommerce" style="margin-bottom:40px;">';
    echo '<h2 class="woocommerce-products-header__title" style="text-align:center; margin-bottom:25px;">Catálogos destacados</h2>';
    echo '<ul class="products columns-4">';

    foreach ($categories as $cat) {
        $thumbnail_id = get_term_meta($cat->term_id, 'thumbnail_id', true);
        $image_url = $thumbnail_id ? wp_get_attachment_url($thumbnail_id) : wc_placeholder_img_src();
        $link = get_term_link($cat);

        echo '<li class="product-category product ' . esc_attr($cat->slug) . '" style="text-align:center;">
                <a href="' . esc_url($link) . '">
                    <img src="' . esc_url($image_url) . '" alt="' . esc_attr($cat->name) . '" style="border-radius:8px; margin-bottom:10px; width:100%; max-width:300px; height:auto;">
                    <h2 class="woocommerce-loop-category__title">' . esc_html($cat->name) . '</h2>
                </a>
              </li>';
    }

    echo '</ul>';
    echo '</section>';
}, 5); // Prioridad menor a 10 para que aparezca antes del loop de productos

/* Banner para Stamina */
add_action('woocommerce_before_main_content', function() {
    if (is_product_category('stamina')) {
        echo '<div style="position:relative; left:50%; transform:translateX(-50%); width:100vw; text-align:center; padding:40px 0; background:transparent;">
            <img src="https://moda.cositascostura.com/wp-content/uploads/2025/10/banner.jpg" alt="Catálogo Stamina"
            style="width:100%; max-width:1200px; height:auto; border-radius:8px; margin-bottom:20px;">
            <br>
            <a href="http://www.stamina-shop.es/yourstamina/Cositascostura" target="_blank" rel="noopener noreferrer"
            style="padding:14px 28px; background-color:#ff7a00; color:#000; font-weight:bold; text-decoration:none; border-radius:30px; font-size:18px;">
            ACCEDER PARA COMPRAR LOS PRODUCTOS - Lee el banner -
            </a>
        </div>';
    }
});

/* Banner para Roly */
add_action('woocommerce_before_main_content', function() {
    if (is_product_category('ropa-roly')) {
        echo '<div style="position:relative; left:50%; transform:translateX(-50%); width:100vw; text-align:center; padding:40px 0; background:transparent;">
            <img src="https://moda.cositascostura.com/wp-content/uploads/2025/10/banner.jpg" alt="Catálogo Roly"
            style="width:100%; max-width:1200px; height:auto; border-radius:8px; margin-bottom:20px;">
            <br>
            <a href="https://www.roly.es/youroly/Cositascostura" target="_blank" rel="noopener noreferrer"
            style="padding:14px 28px; background-color:#ff7a00; color:#000; font-weight:bold; text-decoration:none; border-radius:30px; font-size:18px;">
            ACCEDER PARA COMPRA LOS PRODUCTOS DE ROLY - Lee el banner -
            </a>
        </div>';
    }
});

/* Evitar mensaje "No se han encontrado productos" en estas categorías */
add_action('woocommerce_no_products_found', function() {
    if (is_product_category('ropa-roly') || is_product_category('stamina')) {
        remove_action('woocommerce_no_products_found', 'wc_no_products_found');
    }
}, 1);
