<?php
/**
 * OceanWP Child - functions.php
 * (No cierres este archivo con "?>")
 */

/**
 * Campo "Número de color" solo en productos 7063 y 7059 (obligatorio)
 * Muestra con hooks (si existen) y, si no, lo inyecta con JavaScript dentro del <form class="cart">.
 */

function mysite_color_field_product_ids() {
    return [7063, 7059];
}

/* ========= RENDER POR PHP (si tu plantilla llama a los hooks) ========= */

function mysite_should_render_color_field() {
    if (is_admin() && !wp_doing_ajax()) return false;
    if (!is_product()) return false;

    global $product;
    $product_id = 0;
    if ($product && is_a($product, 'WC_Product')) {
        $product_id = (int) $product->get_id();
        // Si el producto no es comprable (sin precio, etc.) no mostramos
        if (method_exists($product, 'is_purchasable') && !$product->is_purchasable()) return false;
    } else {
        $product_id = (int) get_the_ID();
    }

    return in_array($product_id, mysite_color_field_product_ids(), true);
}

function mysite_render_color_field() {
    static $done = false;
    if ($done) return;
    if (!mysite_should_render_color_field()) return;
    $done = true;

    echo '<div class="color-number-field" style="margin:12px 0;">';
    echo '<label for="color_number" style="display:block;margin-bottom:6px;">Número de color <span style="color:#d00">*</span></label>';
    echo '<input type="text" id="color_number" name="color_number" placeholder="Ej: 1234" required pattern="[A-Za-z0-9\- ]{1,20}" />';
    echo '<small>Introduce el código exacto de la carta de color.</small>';
    echo '</div>';
}
add_action('woocommerce_before_add_to_cart_button', 'mysite_render_color_field', 9);
add_action('woocommerce_after_add_to_cart_quantity', 'mysite_render_color_field', 99);
add_action('woocommerce_after_add_to_cart_button', 'mysite_render_color_field', 1);

/* ========= FALLBACK POR JS (si los*
