Rich Snippets for Yotpo

I've written a quick "fix" for the Yotpo plugin to enable using rich snippet markup manually. Take note that this snippet is only for version 1.1.8 of the Yotpo plugin for WooCommerce.

function wc_yotpo_show_rs() {
    global $product;
    $yotpo_settings = get_option( 'yotpo_settings' );
    $id = $product->get_id();
    $title = $product->get_title();
    $product_url = get_permalink( $id );
    $description = addslashes( wpautop( do_shortcode( $product->get_short_description() ? $product->get_short_description() : $product->get_description() ) ) );
    $availability = 'https://schema.org/' . ( $product->is_in_stock() ? 'InStock' : 'OutOfStock' );
    $price = $product->get_price();
    $price_valid = date( 'Y-12-31', current_time( 'timestamp', true ) + YEAR_IN_SECONDS );
    $currency = get_woocommerce_currency();
    $sku = $product->get_sku();
    $app_key = $yotpo_settings['app_key'];
    $url = "https://api.yotpo.com/products/$app_key/$id/bottomline";
    $json = ( get_headers( $url )[0] == "HTTP/1.1 200 OK" ) ? file_get_contents( $url ) : null;
    if ( !is_null( $json ) ) { $data = json_decode( $json ); }
    if ( !is_null( $data ) && $data->status->code == 200 ) {
        $avg = $data->response->bottomline->average_score ?: 0;
        $total = $data->response->bottomline->total_reviews ?: 0;
        echo '
            <script type="application/ld+json">
                {
                    "@context": "http://schema.org",
                    "@graph": [
                        {
                            "@type":         "Product",
                            "name":          "'. $title .'",
                            "sku":           "'. $sku .'",
                            "itemCondition": "http://schema.org/NewCondition",
                            "description":   "'. $description .'",
                            "offers": {
                                "@type":           "Offer",
                                "url":             "'. $product_url .'",
                                "availability":    "'. $availability .'",
                                "price":           "'. $price .'",
                                "priceCurrency":   "'. $currency .'",
                                "priceValidUntil": "'. $price_valid .'"
                            },
                            "aggregateRating": {
                                "@type":       "AggregateRating",
                                "ratingValue": "'. $avg .'",
                                "reviewCount": "'. $total .'"
                            }
                        }
                    ]
                }
            </script>'; }
}

You can use this function with an action (i.e. add_action) to use it in your theme.


This article was posted under Wordpress
and was tagged ,
This article is related to Yotpo Reviews for WooCommerce (YRFW)