Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter( 'rank_math/snippet/rich_snippet_product_entity', function( $entity ) {
- if ( ! is_product() ) {
- return $entity;
- }
- $product = wc_get_product( get_the_ID() );
- if ( ! $product->is_type( 'variable' ) ) {
- return $entity;
- }
- $variations = $product->get_available_variations();
- if ( ! empty( $variations ) ) {
- $offers = [];
- foreach ( $variations as $variation ) {
- $price_valid_until = get_post_meta( $variation['variation_id'], '_sale_price_dates_to', true );
- if (!$variation['attributes']['attribute_pa_color'] && !$variation['attributes']['attribute_pa_size']) {
- $offers[] = [
- '@type' => 'Offer',
- 'description' => strip_tags( $variation['variation_description'] ),
- 'price' => $variation['display_price'],
- 'priceCurrency' => get_woocommerce_currency(),
- 'availability' => $variation['is_in_stock'] ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock',
- 'itemCondition' => 'NewCondition',
- 'priceValidUntil' => $price_valid_until ? date_i18n( 'Y-m-d', $price_valid_until ) : '2025-12-31',
- 'url' => $product->get_permalink(),
- 'sku' => $variation['sku'],
- ];
- } elseif (!$variation['attributes']['attribute_pa_color']) {
- $offers[] = [
- '@type' => 'Offer',
- 'description' => strip_tags( $variation['variation_description'] ),
- 'price' => $variation['display_price'],
- 'priceCurrency' => get_woocommerce_currency(),
- 'availability' => $variation['is_in_stock'] ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock',
- 'itemCondition' => 'NewCondition',
- 'priceValidUntil' => $price_valid_until ? date_i18n( 'Y-m-d', $price_valid_until ) : '2025-12-31',
- 'url' => $product->get_permalink(),
- 'sku' => $variation['sku'],
- 'size' => $variation['attributes']['attribute_pa_kok] //name of the attribute
- ];
- } elseif (!$variation['attributes']['attribute_pa_size']) {
- $offers[] = [
- '@type' => 'Offer',
- 'description' => strip_tags( $variation['variation_description'] ),
- 'price' => $variation['display_price'],
- 'priceCurrency' => get_woocommerce_currency(),
- 'availability' => $variation['is_in_stock'] ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock',
- 'itemCondition' => 'NewCondition',
- 'priceValidUntil' => $price_valid_until ? date_i18n( 'Y-m-d', $price_valid_until ) : '2025-12-31',
- 'url' => $product->get_permalink(),
- 'sku' => $variation['sku'],
- 'color' => $variation['attributes']['attribute_pa_vari'] //name of the attribute
- ]; } else {
- $offers[] = [
- '@type' => 'Offer',
- 'description' => strip_tags( $variation['variation_description'] ),
- 'price' => $variation['display_price'],
- 'priceCurrency' => get_woocommerce_currency(),
- 'availability' => $variation['is_in_stock'] ? 'https://schema.org/InStock' : 'https://schema.org/OutOfStock',
- 'itemCondition' => 'NewCondition',
- 'priceValidUntil' => $price_valid_until ? date_i18n( 'Y-m-d', $price_valid_until ) : '2025-12-31',
- 'url' => $product->get_permalink(),
- 'sku' => $variation['sku'],
- 'color' => $variation['attributes']['attribute_pa_vari'], //name of the attribute
- 'size' => $variation['attributes']['attribute_pa_koko'] //name of the attribute
- ];
- }
- }
- }
- $entity['offers'] = $offers;
- return $entity;
- } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement