add_action( 'wp_head', 'casamexico24_visual_artwork_schema' ); function casamexico24_visual_artwork_schema() { if ( ! is_product() ) return; global $post; $product = wc_get_product( $post->ID ); if ( ! $product ) return; // --- Datos básicos --- $title = $product->get_name(); $url = get_permalink( $post->ID ); $price = $product->get_price(); $currency = get_woocommerce_currency(); $description = wp_strip_all_tags( $product->get_short_description() ); $image_id = $product->get_image_id(); $image_url = $image_id ? wp_get_attachment_url( $image_id ) : ''; // --- Artista desde categorías --- $artist_name = ''; $artists_known = [ 'alber' => 'Alber', 'beamar' => 'Beamar', 'rogelio-perez' => 'Rogelio Pérez', 'wilfrido-ramirez' => 'Wilfrido Ramírez', 'manuel-gilavalos' => 'Manuel Gilávalos', 'miguel-angel-rivera-lopez' => 'Miguel Ángel Rivera López', 'oscar-omar' => 'Oscar Omar', 'luis-bauti' => 'Luis Bauti', 'christian-vache' => 'Christian Vache', ]; $terms = get_the_terms( $post->ID, 'product_cat' ); if ( $terms && ! is_wp_error( $terms ) ) { foreach ( $terms as $term ) { $slug = $term->slug; if ( isset( $artists_known[ $slug ] ) ) { $artist_name = $artists_known[ $slug ]; break; } } // Si no matchea el slug, usa el nombre de la primera categoría que no sea genérica if ( ! $artist_name ) { $generic = [ 'uncategorized', 'sin-categoria', 'obras', 'pintura' ]; foreach ( $terms as $term ) { if ( ! in_array( $term->slug, $generic ) ) { $artist_name = $term->name; break; } } } } // --- Técnica, medidas y año desde descripción corta --- $tecnica = ''; $width = ''; $height = ''; $year = ''; // Técnica if ( preg_match( '/[Tt][eé]cnica[:\s]+([^\n\r<•\-]+)/u', $description, $m ) ) { $tecnica = trim( $m[1] ); } // Año if ( preg_match( '/[Aa][ñn]o[:\s]+(\d{4})/u', $description, $m ) ) { $year = trim( $m[1] ); } // Medidas — acepta formatos: "30 cm x 90 cm", "30x90 cm", "30 × 90 cm" if ( preg_match( '/(\d+[\.,]?\d*)\s*cm?\s*[x×X]\s*(\d+[\.,]?\d*)\s*cm?/iu', $description, $m ) ) { $width = str_replace( ',', '.', $m[1] ) . ' cm'; $height = str_replace( ',', '.', $m[2] ) . ' cm'; } // --- Disponibilidad --- $availability = $product->is_in_stock() ? 'https://schema.org/InStock' : 'https://schema.org/SoldOut'; // --- Construir schema --- $schema = [ '@context' => 'https://schema.org', '@type' => [ 'VisualArtwork', 'Product' ], '@id' => $url . '#artwork', 'name' => $title, 'url' => $url, 'description' => $description ?: $title . ' — obra original de arte contemporáneo mexicano.', ]; if ( $image_url ) { $schema['image'] = $image_url; } if ( $artist_name ) { $schema['artist'] = [ '@type' => 'Person', 'name' => $artist_name, 'url' => home_url( '/' . sanitize_title( $artist_name ) . '/' ), ]; $schema['creator'] = $schema['artist']; } if ( $tecnica ) { $schema['artMedium'] = $tecnica; $schema['artworkSurface'] = strpos( strtolower( $tecnica ), 'lienzo' ) !== false || strpos( strtolower( $tecnica ), 'canvas' ) !== false ? 'Canvas' : 'Paper'; } if ( $width ) $schema['width'] = [ '@type' => 'QuantitativeValue', 'value' => floatval( $width ), 'unitCode' => 'CMT' ]; if ( $height ) $schema['height'] = [ '@type' => 'QuantitativeValue', 'value' => floatval( $height ), 'unitCode' => 'CMT' ]; if ( $year ) $schema['dateCreated'] = $year; $schema['artform'] = 'Pintura'; $schema['genre'] = 'Arte contemporáneo mexicano'; $schema['locationCreated'] = [ '@type' => 'Place', 'name' => 'Bucerías, Nayarit, México', 'address' => [ '@type' => 'PostalAddress', 'addressLocality' => 'Bucerías', 'addressRegion' => 'Nayarit', 'addressCountry' => 'MX', ], ]; // Parte Product (precio, disponibilidad) if ( $price ) { $schema['offers'] = [ '@type' => 'Offer', 'price' => $price, 'priceCurrency' => $currency, 'availability' => $availability, 'url' => $url, 'seller' => [ '@type' => 'Organization', 'name' => 'Casa México24', 'url' => home_url(), ], ]; } echo '' . "\n"; }