Publié par Fabrice le 26 janvier 2017 - Temps de lecture estimé : 7 minutes
Catégories :
Webmastering
- Étiquettes :
données structurées
e-commerce
JSON-LD
SEO
snippet
web sémantique
WooCommerce
WooCommerce intègre en natif les Microdonnées dans les fiches produits, mais si vous souhaitez utiliser le format JSON-LD, voici un petit snippet.
La version 2.7 de WooCommerce intégrera en natif le format JSON-LD.
Je laisse le snippet, cela peut vous être utile !
En découvrant WooCommerce, j'ai remarqué que les données structurées était bien présentes mais au format Microdonnées, format qui n'est pas des plus facile à maintenir dans la durée. Voici donc un petit snippet à mettre dans le fichier functions
de votre thème. Pensez à bien retirer les Microdonnées des pages de templates.
Note : pour les marques, j'utilise les attributs, mais vous pouvez utiliser les catégories, les tags ou une extension spécifique
/************************************
* @ Structured Data JSON-LD for WooCommerce Product
* Adilade / @adilade_web
* Free to use / public domain
************************************/
add_action('wp_head', 'wpadilade_wc_jsonld_product');
function wpadilade_wc_jsonld_product() {
if (is_product()) {
$product = new WC_Product(get_the_ID());
echo '<script type="application/ld+json">{"@context":"http://schema.org",';
echo '"@type":"Product",';
echo '"@id":"'; the_permalink(); echo '",';
echo '"name":"'; the_title(); echo '",';
echo '"url":"'; the_permalink(); echo '",';
// Description
$description = get_the_excerpt();
$description = wp_trim_excerpt($description);
echo '"description": "'.$description.'"';
// Image
if (has_post_thumbnail($product->id)) {
$attachment_ids[0] = get_post_thumbnail_id($product->id);
$attachment = wp_get_attachment_image_src($attachment_ids[0], 'full');
echo ',"image":{';
echo '"@type":"ImageObject",';
echo '"url":"'.$attachment[0].'",';
echo '"width":"'.$attachment[1].'",';
echo '"height":"'.$attachment[2].'"';
echo '}';
}
// SKU - REF
if (wc_product_sku_enabled() && $product->get_sku()) {
echo ',"sku": "'; echo $product->get_sku(); echo '"';
}
// Brands (only if you use attributes as "marque" - replace with your own name if necessary)
$brand_values = get_the_terms($post->ID, 'pa_marque');
if($brand_values) {
$brands = array();
foreach ($brand_values as $brand) {
$brands_json[] = $brand->name;
}
$brands_json = join(', ', $brands_json);
echo ',"brand": "'.$brands_json.'"';
}
// Rating
if (get_option('woocommerce_enable_review_rating' ) != 'no') {
if ($product->get_rating_count() > 0) {
echo ',"aggregateRating": {';
echo '"@type": "AggregateRating",';
echo '"bestRating": "5",';
echo '"ratingValue": "'.$product->get_average_rating().'",';
echo '"ratingCount": "'.$product->get_rating_count().'"';
echo '}';
}
}
// Prices
echo ',"offers": {';
$product_v = new WC_Product_Variable($product);
$prices = $product_v->get_variation_prices(true);
// No variations, or no active variation prices
if ($product->get_price() === '' || empty($prices['price'])) {
echo '"@type": "Offer",';
echo '"price": "'.wc_format_decimal($product->get_price(), 2).'",';
} else {
$min_price = current($prices['price']);
$max_price = end($prices['price']);
$is_free = $min_price == 0 && $max_price == 0;
if ($is_free) {
echo '"@type": "Offer",';
echo '"price": "0.00",';
} elseif($min_price != $max_price) {
echo '"@type": "AggregateOffer",';
echo '"lowPrice": "'.wc_format_decimal($min_price, 2).'",';
echo '"highPrice": "'.wc_format_decimal($max_price, 2).'",';
} else {
echo '"@type": "Offer",';
echo '"price": "'.wc_format_decimal($min_price, 2).'",';
}
}
echo '"priceCurrency": "'.esc_attr(get_woocommerce_currency()).'",';
echo '"availability": "http://schema.org/'; echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; echo '",';
echo '"seller":{';
echo '"@type": "Organization",';
echo '"name":"'; bloginfo('name'); echo '",';
echo '"url":"'.esc_url(home_url('/')).'",';
echo '"description":"'; bloginfo('description'); echo '"';
echo '}';
echo '}';
echo '}</script>'."\n";
}
}