Last active
December 4, 2021 11:18
-
-
Save seojacky/38eb308a4eca61d6dd9adb9d6547ef9f to your computer and use it in GitHub Desktop.
product.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Remove the generated product schema markup from Product Category and Shop pages. | |
*/ | |
/* | |
* ОЧень спорно!!!!! Зачем убирать????? | |
*/ | |
/*add_action( 'woocommerce_init', 'wc_remove_product_schema_product_archive' ); | |
function wc_remove_product_schema_product_archive() { | |
remove_action( 'woocommerce_shop_loop', array( WC()->structured_data, 'generate_product_data' ), 10, 0 ); | |
}*/ | |
/* Добавляем в разметку Product параметры brand, itemCondition, mpn */ | |
add_filter( 'woocommerce_structured_data_product', 'filter_woocommerce_structured_data_product', 10, 2 ); | |
function filter_woocommerce_structured_data_product( $markup, $product ) { | |
$markup['brand'] = array( | |
'@type' => 'Brand', | |
'name' => get_bloginfo( 'name' ), | |
); | |
$markup['itemCondition'] = 'new'; | |
$markup['mpn'] = $markup['sku']; | |
return $markup; | |
}; | |
/* Добавляет в разметку aggregateRating ложный отзыв 'Спасибо!' для всех продуктов, где нет отзыва*/ | |
add_filter( 'woocommerce_structured_data_product', 'func_for_zero_rating_woocommerce_structured_data_product', 10, 2 ); | |
function func_for_zero_rating_woocommerce_structured_data_product( $markup, $product ) { | |
global $product; | |
if ( empty( $markup[ 'aggregateRating' ] )&& ($product->get_average_rating() == 0)) { //если пустое | |
$markup['aggregateRating'] = array( | |
'@type' => 'AggregateRating', | |
'ratingValue' => '5', | |
'reviewCount' => '1', | |
'bestRating' => '5', | |
'worstRating' => '1', | |
); | |
$markup['review'] = array( | |
'@type' => 'Review', | |
'reviewBody' => 'Спасибо!', | |
'datePublished' => $product->get_date_modified()->date('Y-m-d H:i:s'), | |
); | |
$markup['review']['reviewRating'] = array( | |
'@type' => 'Rating', | |
'bestRating' => '5', | |
'ratingValue' => '5', | |
'worstRating' => '1', | |
); | |
$markup['review']['author'] = array( | |
'@type' => 'Person', | |
'name' => 'User', | |
); | |
} | |
return $markup; | |
} | |
/* Добавляет картинку в микроразметку (Отсутствует поле "image")*/ | |
add_filter( 'woocommerce_structured_data_product', 'add_image_woocommerce_structured_data_product', 10, 2 ); | |
function add_image_woocommerce_structured_data_product( $markup, $product ) { | |
global $product; | |
if ( empty( $markup[ 'image' ] )) { | |
$markup['image'] = get_site_url().'/wp-content/uploads/woocommerce-placeholder.png'; | |
} | |
return $markup; | |
} | |
// This below adds aggregateRating information if this has not already been set in the JSON data and if the average rating is not zero | |
//Это ниже добавляет информацию aggregateRating, если она еще не была установлена в данных JSON и если средний рейтинг не равен нулю | |
add_filter( 'woocommerce_structured_data_review', 'filter_woocommerce_structured_data_review', 10, 2 ); | |
function filter_woocommerce_structured_data_review( $markup, $comment ) { | |
global $product; | |
$markup['itemReviewed'] = array( | |
'@type' => 'Product', | |
'name' => get_the_title( $comment->comment_post_ID ), | |
'brand' => get_bloginfo( 'name' ), | |
'sku' => $product->get_id(), | |
'mpn' => $product->get_id(), | |
'image' => wp_get_attachment_image_url( $product->get_image_id(), 'full' ), | |
'description' => mb_strimwidth(wp_strip_all_tags( $product->get_description() ), 0, 600, "..."), | |
//'review' => get_comment_text( $comment->comment_ID ), | |
'offers' => array( | |
"@type"=> "Offer", | |
"availability"=> "http://schema.org/InStock", | |
"price"=> $product->get_price(), | |
"priceCurrency"=> "RUB", | |
"priceValidUntil"=> "2029-12-31", | |
"url"=> get_permalink( $product->get_id() ), | |
), | |
'review' => array( | |
"@type"=> "Review", | |
'author' => array( | |
'@type' => 'Person', | |
'name' => get_comment_author( $comment->comment_ID ), | |
), | |
), | |
'aggregateRating' => array( | |
'@type' => 'AggregateRating', | |
'ratingValue' => $product->get_average_rating(), | |
'reviewCount' => $product->get_review_count(), | |
'bestRating' => '5', | |
'worstRating' => '0', | |
), | |
); | |
return $markup; | |
} | |
// https://github.com/woocommerce/woocommerce/issues/22896#issuecomment-468931155 | |
// This filters the structured data for a product | |
// This below adds aggregateRating information if this has not already been set in the JSON data and if the average rating is not zero | |
// Это ниже добавляет информацию aggregateRating (bestRating/worstRating), если она еще не была установлена в данных JSON и если средний рейтинг не равен нулю | |
add_filter( 'woocommerce_structured_data_product', 'filter_2_woocommerce_structured_data_product', 10, 2 ); | |
function filter_2_woocommerce_structured_data_product( $markup, $product ) { | |
if ( empty( $markup[ 'aggregateRating' ] )&& ($product->get_average_rating()!= 0)) { | |
$markup['aggregateRating'] = array( | |
'@type' => 'AggregateRating', | |
'ratingValue' => $product->get_average_rating(), | |
'reviewCount' => $product->get_review_count(), | |
'bestRating' => '5', | |
'worstRating' => '0', | |
); | |
} | |
return $markup; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment