Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action('wp_footer', function() {
- global $product;
- if( ! avia_woocommerce_version_check( '3.0.0') )
- {
- return;
- }
- if( ! is_product() || ! $product instanceof WC_Product )
- {
- return;
- }
- if( get_post_meta( $product->get_id(), '_aviaLayoutBuilderCleanData', true ) ) {
- $data = avia_generate_product_data();
- if ( $data ) {
- echo '<script type="application/ld+json">' . wc_esc_json( wp_json_encode( $data ), true ) . '</script>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
- }
- }
- }, 10);
- function avia_generate_product_data( $product = null ) {
- if ( ! is_object( $product ) ) {
- global $product;
- }
- if ( ! is_a( $product, 'WC_Product' ) ) {
- return;
- }
- $shop_name = get_bloginfo( 'name' );
- $shop_url = home_url();
- $currency = get_woocommerce_currency();
- $permalink = get_permalink( $product->get_id() );
- $markup = array(
- '@type' => 'Product',
- '@id' => $permalink . '#product', // Append '#product' to differentiate between this @id and the @id generated for the Breadcrumblist.
- 'name' => $product->get_name(),
- 'url' => $permalink,
- 'image' => wp_get_attachment_url( $product->get_image_id() ),
- 'description' => wp_strip_all_tags( do_shortcode( $product->get_short_description() ? $product->get_short_description() : $product->get_description() ) ),
- );
- // Declare SKU or fallback to ID.
- if ( $product->get_sku() ) {
- $markup['sku'] = $product->get_sku();
- } else {
- $markup['sku'] = $product->get_id();
- }
- if ( '' !== $product->get_price() ) {
- // Assume prices will be valid until the end of next year, unless on sale and there is an end date.
- $price_valid_until = date( 'Y-12-31', current_time( 'timestamp', true ) + YEAR_IN_SECONDS );
- if ( $product->is_type( 'variable' ) ) {
- $lowest = $product->get_variation_price( 'min', false );
- $highest = $product->get_variation_price( 'max', false );
- if ( $lowest === $highest ) {
- $markup_offer = array(
- '@type' => 'Offer',
- 'price' => wc_format_decimal( $lowest, wc_get_price_decimals() ),
- 'priceValidUntil' => $price_valid_until,
- 'priceSpecification' => array(
- 'price' => wc_format_decimal( $lowest, wc_get_price_decimals() ),
- 'priceCurrency' => $currency,
- 'valueAddedTaxIncluded' => wc_prices_include_tax() ? 'true' : 'false',
- ),
- );
- } else {
- $markup_offer = array(
- '@type' => 'AggregateOffer',
- 'lowPrice' => wc_format_decimal( $lowest, wc_get_price_decimals() ),
- 'highPrice' => wc_format_decimal( $highest, wc_get_price_decimals() ),
- 'offerCount' => count( $product->get_children() ),
- );
- }
- } else {
- if ( $product->is_on_sale() && $product->get_date_on_sale_to() ) {
- $price_valid_until = date( 'Y-m-d', $product->get_date_on_sale_to()->getTimestamp() );
- }
- $markup_offer = array(
- '@type' => 'Offer',
- 'price' => wc_format_decimal( $product->get_price(), wc_get_price_decimals() ),
- 'priceValidUntil' => $price_valid_until,
- 'priceSpecification' => array(
- 'price' => wc_format_decimal( $product->get_price(), wc_get_price_decimals() ),
- 'priceCurrency' => $currency,
- 'valueAddedTaxIncluded' => wc_prices_include_tax() ? 'true' : 'false',
- ),
- );
- }
- $markup_offer += array(
- 'priceCurrency' => $currency,
- 'availability' => 'http://schema.org/' . ( $product->is_in_stock() ? 'InStock' : 'OutOfStock' ),
- 'url' => $permalink,
- 'seller' => array(
- '@type' => 'Organization',
- 'name' => $shop_name,
- 'url' => $shop_url,
- ),
- );
- $markup['offers'] = array( apply_filters( 'woocommerce_structured_data_product_offer', $markup_offer, $product ) );
- }
- if ( $product->get_rating_count() && wc_review_ratings_enabled() ) {
- $markup['aggregateRating'] = array(
- '@type' => 'AggregateRating',
- 'ratingValue' => $product->get_average_rating(),
- 'reviewCount' => $product->get_review_count(),
- );
- // Markup most recent rating/review.
- $comments = get_comments(
- array(
- 'number' => 1,
- 'post_id' => $product->get_id(),
- 'status' => 'approve',
- 'post_status' => 'publish',
- 'post_type' => 'product',
- 'parent' => 0,
- 'meta_key' => 'rating',
- 'orderby' => 'meta_value_num',
- )
- );
- if ( $comments ) {
- foreach ( $comments as $comment ) {
- $rating = get_comment_meta( $comment->comment_ID, 'rating', true );
- if ( ! $rating ) {
- continue;
- }
- $markup['review'] = array(
- '@type' => 'Review',
- 'reviewRating' => array(
- '@type' => 'Rating',
- 'ratingValue' => $rating,
- ),
- 'author' => array(
- '@type' => 'Person',
- 'name' => get_comment_author( $comment->comment_ID ),
- ),
- );
- }
- }
- }
- return $markup;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement