Updated structured data for products

- Replaced outdated `priceSpecification` with `AggregateOffer`.
- Restructured data according to Google's latest specifications.
- Use product short_description if available.
This commit is contained in:
Saul Fautley 2017-07-19 18:53:02 +02:00 committed by GitHub
parent 18873ac9a7
commit 68470b51a4
1 changed files with 38 additions and 29 deletions

View File

@ -193,54 +193,63 @@ class WC_Structured_Data {
return; return;
} }
$shop_name = get_bloginfo( 'name' ); $shop_name = get_bloginfo( 'name' );
$shop_url = home_url(); $shop_url = home_url();
$currency = get_woocommerce_currency(); $currency = get_woocommerce_currency();
$markup = array();
$markup['@type'] = 'Product'; $markup = array(
$markup['@id'] = get_permalink( $product->get_id() ); '@type' => 'Product',
$markup['url'] = $markup['@id']; '@id' => get_permalink( $product->get_id() ),
$markup['name'] = $product->get_name(); 'name' => $product->get_name(),
);
if ( apply_filters( 'woocommerce_structured_data_product_limit', is_product_taxonomy() || is_shop() ) ) { if ( apply_filters( 'woocommerce_structured_data_product_limit', is_product_taxonomy() || is_shop() ) ) {
$markup['url'] = $markup['@id'];
$this->set_data( apply_filters( 'woocommerce_structured_data_product_limited', $markup, $product ) ); $this->set_data( apply_filters( 'woocommerce_structured_data_product_limited', $markup, $product ) );
return; return;
} }
if ( '' !== $product->get_price() ) { $markup['image'] = wp_get_attachment_url( $product->get_image_id() );
$markup_offer = array( $markup['description'] = $product->get_short_description() ?: $product->get_description();
'@type' => 'Offer', $markup['sku'] = $product->get_sku();
'priceCurrency' => $currency,
'availability' => 'https://schema.org/' . $stock = ( $product->is_in_stock() ? 'InStock' : 'OutOfStock' ),
'sku' => $product->get_sku(),
'image' => wp_get_attachment_url( $product->get_image_id() ),
'description' => $product->get_description(),
'seller' => array(
'@type' => 'Organization',
'name' => $shop_name,
'url' => $shop_url,
),
);
if ( '' !== $product->get_price() ) {
if ( $product->is_type( 'variable' ) ) { if ( $product->is_type( 'variable' ) ) {
$prices = $product->get_variation_prices(); $prices = $product->get_variation_prices();
$lowest = reset( $prices['price'] ); $lowest = reset( $prices['price'] );
$highest = end( $prices['price'] ); $highest = end( $prices['price'] );
if ( $lowest === $highest ) { if ( $lowest === $highest ) {
$markup_offer['price'] = wc_format_decimal( $product->get_price(), wc_get_price_decimals() ); $markup_offer = array(
'@type' => 'Offer',
'price' => wc_format_decimal( $lowest, wc_get_price_decimals() ),
);
} else { } else {
$markup_offer['priceSpecification'] = array( $markup_offer = array(
'price' => wc_format_decimal( $product->get_price(), wc_get_price_decimals() ), '@type' => 'AggregateOffer',
'minPrice' => wc_format_decimal( $lowest, wc_get_price_decimals() ), 'lowPrice' => wc_format_decimal( $lowest, wc_get_price_decimals() ),
'maxPrice' => wc_format_decimal( $highest, wc_get_price_decimals() ), 'highPrice' => wc_format_decimal( $highest, wc_get_price_decimals() ),
'priceCurrency' => $currency,
); );
} }
} else { } else {
$markup_offer['price'] = wc_format_decimal( $product->get_price(), wc_get_price_decimals() ); $markup_offer = array(
'@type' => 'Offer',
'price' => wc_format_decimal( $product->get_price(), wc_get_price_decimals() ),
);
} }
$markup_offer += array(
'priceCurrency' => $currency,
'availability' => 'https://schema.org/' . ( $product->is_in_stock() ? 'InStock' : 'OutOfStock' ),
'url' => $markup['@id'],
'seller' => array(
'@type' => 'Organization',
'name' => $shop_name,
'url' => $shop_url,
),
);
$markup['offers'] = array( apply_filters( 'woocommerce_structured_data_product_offer', $markup_offer, $product ) ); $markup['offers'] = array( apply_filters( 'woocommerce_structured_data_product_offer', $markup_offer, $product ) );
} }