build( AumNexCart_Product::to_array( $post_id ) ); if ( empty( $schema ) ) { return; } echo "\n\n"; echo '` would close this element and everything after it would * be parsed as markup. JSON_HEX_TAG escapes `<` and `>` as \u003C / \u003E, which JSON-LD * consumers unescape back to the original characters. */ echo wp_json_encode( $schema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_HEX_TAG ); echo "\n\n"; } /** * Builds the Product schema graph from normalized product data. * * @param array $d Normalized product (see AumNexCart_Product::to_array). * @return array */ private function build( array $d ) { $schema = array( '@context' => 'https://schema.org', '@type' => 'Product', 'name' => (string) ( $d['title'] ?? '' ), 'url' => (string) ( $d['permalink'] ?? '' ), ); if ( ! empty( $d['description'] ) ) { $schema['description'] = (string) $d['description']; } if ( ! empty( $d['images'] ) && is_array( $d['images'] ) ) { $schema['image'] = array_values( $d['images'] ); } if ( ! empty( $d['sku'] ) ) { $schema['sku'] = (string) $d['sku']; } if ( ! empty( $d['categories'][0]['name'] ) ) { $schema['category'] = (string) $d['categories'][0]['name']; } if ( ! empty( $d['attributes'] ) && is_array( $d['attributes'] ) ) { $props = array(); foreach ( $d['attributes'] as $attr ) { $key = isset( $attr['key'] ) ? trim( (string) $attr['key'] ) : ''; if ( '' === $key ) { continue; } $props[] = array( '@type' => 'PropertyValue', 'name' => $key, 'value' => isset( $attr['value'] ) ? (string) $attr['value'] : '', ); } if ( $props ) { $schema['additionalProperty'] = $props; } } $offers = $this->offers( (array) ( $d['price'] ?? array() ), $schema['url'] ); if ( $offers ) { $schema['offers'] = $offers; } return $schema; } /** * Maps a normalized price block to an Offer / AggregateOffer. Mirrors * AumViso's offers_from_source_price. * * @param array $price Normalized price. * @param string $url Product URL. * @return array */ private function offers( array $price, $url ) { $type = $price['type'] ?? 'hidden'; if ( 'range' !== $type && 'fixed' !== $type ) { return array(); } $currency = isset( $price['currency'] ) ? strtoupper( trim( (string) $price['currency'] ) ) : ''; if ( 3 !== strlen( $currency ) || ! ctype_alpha( $currency ) ) { return array(); } $min = ( isset( $price['min'] ) && '' !== $price['min'] ) ? (float) $price['min'] : null; $max = ( isset( $price['max'] ) && '' !== $price['max'] ) ? (float) $price['max'] : null; if ( null === $min && null === $max ) { return array(); } if ( 'fixed' === $type || $min === $max ) { return array( '@type' => 'Offer', 'priceCurrency' => $currency, 'price' => (string) ( null !== $min ? $min : $max ), 'url' => $url, 'availability' => 'https://schema.org/InStock', ); } $offer = array( '@type' => 'AggregateOffer', 'priceCurrency' => $currency, 'url' => $url, 'availability' => 'https://schema.org/InStock', ); if ( null !== $min ) { $offer['lowPrice'] = $min; } if ( null !== $max ) { $offer['highPrice'] = $max; } return $offer; } }