is_woocommerce_active() ) { return; } add_filter( 'schema_ai_schema_data', array( $this, 'enhance_product_schema' ), 10, 3 ); add_filter( 'schema_ai_field_data', array( $this, 'add_woo_fields' ), 10, 3 ); } /** * Enhance Product schema with WooCommerce data. * * If the schema type is Product and the post is a valid WC product, * enrich the schema array with offers, SKU, brand, GTIN/MPN, * aggregate rating, gallery images, and short description. * * @param array $schema The current schema data array. * @param int $post_id The WordPress post ID. * @param string $schema_type The schema.org type (e.g. 'Product'). * @return array Modified schema data. */ public function enhance_product_schema( $schema, $post_id, $schema_type ) { if ( 'Product' !== $schema_type ) { return $schema; } if ( ! function_exists( 'wc_get_product' ) ) { return $schema; } $product = wc_get_product( $post_id ); if ( ! $product ) { return $schema; } // Offers. $offers = $this->get_product_offers( $product ); if ( ! empty( $offers ) ) { $schema['offers'] = $offers; } // SKU. $sku = $product->get_sku(); if ( ! empty( $sku ) ) { $schema['sku'] = $sku; } // Brand. $brand = $this->get_product_brand( $product ); if ( ! empty( $brand ) ) { $schema['brand'] = array( '@type' => 'Brand', 'name' => $brand, ); } // GTIN. $gtin = $product->get_meta( '_gtin' ); if ( ! empty( $gtin ) ) { $schema['gtin'] = $gtin; } // MPN. $mpn = $product->get_meta( '_mpn' ); if ( ! empty( $mpn ) ) { $schema['mpn'] = $mpn; } // Aggregate rating. $rating_count = $product->get_review_count(); $avg_rating = $product->get_average_rating(); if ( $rating_count > 0 && $avg_rating > 0 ) { $schema['aggregateRating'] = array( '@type' => 'AggregateRating', 'ratingValue' => $avg_rating, 'reviewCount' => $rating_count, 'bestRating' => 5, 'worstRating' => 1, ); } // Product gallery images. $gallery_ids = $product->get_gallery_image_ids(); if ( ! empty( $gallery_ids ) ) { $images = array(); // Include the main product image first. $main_image_id = $product->get_image_id(); if ( $main_image_id ) { $main_src = wp_get_attachment_image_url( $main_image_id, 'full' ); if ( $main_src ) { $images[] = $main_src; } } foreach ( $gallery_ids as $gallery_id ) { $src = wp_get_attachment_image_url( $gallery_id, 'full' ); if ( $src ) { $images[] = $src; } } if ( ! empty( $images ) ) { $schema['image'] = $images; } } // Short description. $short_description = $product->get_short_description(); if ( ! empty( $short_description ) && empty( $schema['description'] ) ) { $schema['description'] = wp_strip_all_tags( $short_description ); } return $schema; } /** * Add WooCommerce-specific fields to the field data array. * * Provides price, currency, stock status, SKU, weight, and dimensions * so that other parts of Schema AI can consume them. * * @param array $fields The current field data array. * @param int $post_id The WordPress post ID. * @param string $schema_type The schema.org type. * @return array Modified field data. */ public function add_woo_fields( $fields, $post_id, $schema_type ) { if ( 'Product' !== $schema_type ) { return $fields; } if ( ! function_exists( 'wc_get_product' ) ) { return $fields; } $product = wc_get_product( $post_id ); if ( ! $product ) { return $fields; } // Price. $price = $product->get_price(); if ( '' !== $price ) { $fields['price'] = $price; } // Currency. if ( function_exists( 'get_woocommerce_currency' ) ) { $fields['currency'] = get_woocommerce_currency(); } // Stock status. $fields['stock_status'] = $product->is_in_stock() ? 'InStock' : 'OutOfStock'; // SKU. $sku = $product->get_sku(); if ( ! empty( $sku ) ) { $fields['sku'] = $sku; } // Weight. $weight = $product->get_weight(); if ( ! empty( $weight ) ) { $fields['weight'] = $weight; } // Dimensions. if ( $product->has_dimensions() ) { $fields['dimensions'] = array( 'length' => $product->get_length(), 'width' => $product->get_width(), 'height' => $product->get_height(), ); } return $fields; } /** * Build an Offer or AggregateOffer schema from a WC_Product. * * Handles simple, variable, and grouped product types. Variable * products produce an AggregateOffer with lowPrice/highPrice. * * @param \WC_Product $product The WooCommerce product object. * @return array Schema.org Offer or AggregateOffer array. */ public function get_product_offers( $product ) { $currency = function_exists( 'get_woocommerce_currency' ) ? get_woocommerce_currency() : 'USD'; $url = get_permalink( $product->get_id() ); // Availability mapping. $availability = $product->is_in_stock() ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock'; if ( $product->is_on_backorder() ) { $availability = 'https://schema.org/BackOrder'; } // Variable products use AggregateOffer. if ( $product->is_type( 'variable' ) ) { $prices = $product->get_variation_prices( true ); if ( empty( $prices['price'] ) ) { return array(); } $min_price = current( $prices['price'] ); $max_price = end( $prices['price'] ); return array( '@type' => 'AggregateOffer', 'lowPrice' => wc_format_decimal( $min_price, wc_get_price_decimals() ), 'highPrice' => wc_format_decimal( $max_price, wc_get_price_decimals() ), 'priceCurrency' => $currency, 'availability' => $availability, 'offerCount' => count( $prices['price'] ), 'url' => $url, ); } // Grouped products use AggregateOffer over child prices. if ( $product->is_type( 'grouped' ) ) { $children = $product->get_children(); if ( empty( $children ) ) { return array(); } $child_prices = array(); foreach ( $children as $child_id ) { $child = wc_get_product( $child_id ); if ( $child ) { $child_price = $child->get_price(); if ( '' !== $child_price ) { $child_prices[] = (float) $child_price; } } } if ( empty( $child_prices ) ) { return array(); } sort( $child_prices ); return array( '@type' => 'AggregateOffer', 'lowPrice' => wc_format_decimal( $child_prices[0], wc_get_price_decimals() ), 'highPrice' => wc_format_decimal( end( $child_prices ), wc_get_price_decimals() ), 'priceCurrency' => $currency, 'availability' => $availability, 'offerCount' => count( $child_prices ), 'url' => $url, ); } // Simple / external / other product types use a single Offer. $price = $product->get_price(); if ( '' === $price ) { return array(); } $offer = array( '@type' => 'Offer', 'price' => wc_format_decimal( $price, wc_get_price_decimals() ), 'priceCurrency' => $currency, 'availability' => $availability, 'url' => $url, 'priceValidUntil' => gmdate( 'Y-12-31' ), ); // Use sale price when available. $sale_price = $product->get_sale_price(); if ( ! empty( $sale_price ) && $sale_price !== $price ) { $offer['price'] = wc_format_decimal( $sale_price, wc_get_price_decimals() ); } // Item condition - default to new. $offer['itemCondition'] = 'https://schema.org/NewCondition'; return $offer; } /** * Get the brand name for a product. * * Checks product attributes ('brand', 'pa_brand') first, then falls * back to common brand taxonomies used by popular plugins. * * @param \WC_Product $product The WooCommerce product object. * @return string Brand name or empty string. */ public function get_product_brand( $product ) { // Check product attributes first. $brand = $product->get_attribute( 'brand' ); if ( empty( $brand ) ) { $brand = $product->get_attribute( 'pa_brand' ); } // Check common brand taxonomies (used by WooCommerce Brands, YITH, etc.). if ( empty( $brand ) ) { $brand_taxonomies = array( 'product_brand', 'pwb-brand', 'yith_product_brand', ); foreach ( $brand_taxonomies as $taxonomy ) { if ( taxonomy_exists( $taxonomy ) ) { $terms = get_the_terms( $product->get_id(), $taxonomy ); if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { $brand = $terms[0]->name; break; } } } } // Check product meta as a last resort. if ( empty( $brand ) ) { $brand = $product->get_meta( '_brand' ); } return $brand ? $brand : ''; } /** * Check if WooCommerce is active. * * @return bool True if WooCommerce class is available. */ public function is_woocommerce_active() { return class_exists( 'WooCommerce' ); } }