Skip to content

Instantly share code, notes, and snippets.

@mattiasghodsian
Last active April 28, 2023 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattiasghodsian/4dcf3995b0df60757afeca8bbaf3f9a4 to your computer and use it in GitHub Desktop.
Save mattiasghodsian/4dcf3995b0df60757afeca8bbaf3f9a4 to your computer and use it in GitHub Desktop.
[WooCommerce] Add missing Schema
/**
* Title: Add missing Schema WooCommerce
* Author: Mattias Ghodsian
* Donate a cup of coffee: https://www.buymeacoffee.com/mattiasghodsian
* Donate Eth: 0xBBB96204E45D11C9799c6B12E6eE6F0d4A071Ef5
**/
function woo_missing_schema_fields($data) {
global $product;
$images = $product->get_gallery_image_ids();
$data['brand'] = "Festmäklarna";
$data['aggregateRating'] = [
"ratingValue" => ( $product->get_average_rating() ? $product->get_average_rating() : '3.0'),
"reviewCount" => ( $product->get_review_count() ? $product->get_review_count() : '1')
];
// $data['gtin8'] = ""; // gtin8, gtin12, gtin13, gtin14, mpn, isbn
if ( ! ( $images ) ) {
$data['image'] = get_site_url()."/wp-content/uploads/images/ingen_bild.png";
}else{
$data['image'] = wp_get_attachment_image_src( $images[0], 'full' )[0];
}
return $data;
}
add_filter( 'woocommerce_structured_data_product', 'woo_missing_schema_fields' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment