Hello,
We apologize for the issue you are facing with the hasMerchantReturnPolicy
field not working for variable products. To ensure that the return policy is applied to variable products as well, you need to modify the filter to include the logic for variants.
Here’s an example of how you can adjust your code to handle variable products:
add_filter( 'rank_math/json_ld', function( $data, $jsonld ) {
if ( empty( $data['richSnippet'] ) || ! in_array( $data['richSnippet']['@type'], [ 'Product', 'ProductGroup' ] ) ) {
return $data;
}
// Add Merchant Return Policy
$data['hasMerchantReturnPolicy'] = [
'@context' => 'https://schema.org/',
'@type' => 'MerchantReturnPolicy',
'applicableCountry' => 'US',
'returnPolicyCategory' => 'https://schema.org/MerchantReturnFiniteReturnWindow',
'merchantReturnDays' => 30,
'returnMethod' => 'https://schema.org/ReturnByMail',
'returnFees' => 'https://schema.org/FreeReturn'
];
// For variable products
if ( 'Product' === $data['richSnippet']['@type'] ) {
$data['richSnippet']['offers']['hasMerchantReturnPolicy'] = ['@id' => '#merchant_policy'];
return $data;
}
if ( empty( $data['richSnippet']['hasVariant'] ) ) {
return $data;
}
foreach ( $data['richSnippet']['hasVariant'] as $key => $value ) {
if ( empty( $value['offers'] ) ) {
continue;
}
$data['richSnippet']['hasVariant'][ $key ]['offers']['hasMerchantReturnPolicy'] = [ '@id' => '#merchant_policy' ];
}
return $data;
}, 99, 2);
This code snippet ensures that the hasMerchantReturnPolicy
is included for both simple and variable products. You can customize the values as needed for your store.
For more detailed guidance, you can refer to our documentation on adding Merchant Return and Shipping policies: How to Add Merchant Return and Shipping Policy in WooCommerce?.
Thank you for raising this concern, and we hope this helps resolve the issue! If you have further questions or need additional assistance, please reply to this ticket, and a human agent will assist you.
Sources:
How to Add Merchant Return and Shipping Policy in WooCommerce? » Rank Math
hasMerchantReturnPolicy and shippingDetails can stay same for many months . maybe its good idea to make code that it would add a transient cache ?
Hello,
Thank you for your query and we are so sorry about the trouble this must have caused.
Please try the following code instead:
add_filter('rank_math/json_ld', function ($data, $jsonld) {
if (empty($data['richSnippet']) || ! in_array($data['richSnippet']['@type'], ['Product', 'ProductGroup'])) {
return $data;
}
$data['shippingDetails'] = [
'@context' => 'https://schema.org/',
'@type' => 'OfferShippingDetails',
'@id' => '#shipping_policy',
'deliveryTime' => [
'@type' => 'ShippingDeliveryTime',
'handlingTime' => [
'@type' => 'QuantitativeValue',
'minValue' => 0,
'maxValue' => 1,
'unitCode' => 'DAY',
],
'transitTime' => [
'@type' => 'QuantitativeValue',
'minValue' => 1,
'maxValue' => 5,
'unitCode' => 'DAY'
],
],
'shippingRate' => [
'@type' => 'MonetaryAmount',
'value' => 5,
'currency' => 'USD',
],
'shippingDestination' => [
'@type' => 'DefinedRegion',
'addressCountry' => 'US'
]
];
$data['hasMerchantReturnPolicy'] = [
'@context' => 'https://schema.org/',
'@type' => 'MerchantReturnPolicy',
'@id' => '#merchant_policy',
'applicableCountry' => 'US',
'returnPolicyCategory' => 'https://schema.org/MerchantReturnFiniteReturnWindow',
'merchantReturnDays' => 30,
'returnMethod' => 'https://schema.org/ReturnByMail',
'returnFees' => 'https://schema.org/FreeReturn'
];
if ('Product' === $data['richSnippet']['@type']) {
$data['richSnippet']['offers']['shippingDetails'] = ['@id' => '#shipping_policy'];
$data['richSnippet']['offers']['hasMerchantReturnPolicy'] = ['@id' => '#merchant_policy'];
return $data;
}
if (empty($data['richSnippet']['hasVariant'])) {
return $data;
}
foreach ($data['richSnippet']['hasVariant'] as $key => $value) {
if (empty($value['offers'])) {
continue;
}
$data['richSnippet']['hasVariant'][$key]['offers']['shippingDetails'] = ['@id' => '#shipping_policy'];
$data['richSnippet']['hasVariant'][$key]['offers']['hasMerchantReturnPolicy'] = ['@id' => '#merchant_policy'];
$data['richSnippet']['hasVariant'][$key]['offers']['priceCurrency'] = 'IRT';
}
return $data;
}, 99, 2);
Let us know how that goes.
Looking forward to helping you.
Hello,
Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.
If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.
Thank you.