Missing field hasMerchantReturnPolicy ❗DOESNT WORK FOR VARIABLE PRODUCTS

#906423
  • Resolved Artur S
    Rank Math free

    Hello. your solution works just for single products . For variation products still warning ❗❗❗
    https://support.rankmath.com/ticket/missing-field-hasmerchantreturnpolicy-2/

    add_filter( “rank_math/snippet/rich_snippet_product_entity”, function( $entity ) {
    // Return policy
    $entity[‘offers’][‘hasMerchantReturnPolicy’][‘@type’] = ‘MerchantReturnPolicy’;
    $entity[‘offers’][‘hasMerchantReturnPolicy’][‘applicableCountry’] = ‘US’;
    $entity[‘offers’][‘hasMerchantReturnPolicy’][‘returnPolicyCategory’] = ‘https://schema.org/MerchantReturnFiniteReturnWindow’;
    $entity[‘offers’][‘hasMerchantReturnPolicy’][‘merchantReturnDays’] = 30;
    $entity[‘offers’][‘hasMerchantReturnPolicy’][‘returnMethod’] = ‘https://schema.org/ReturnByMail’;
    $entity[‘offers’][‘hasMerchantReturnPolicy’][‘returnFees’] = ‘https://schema.org/FreeReturn’;

    // Shipping details
    $entity[‘offers’][‘shippingDetails’][‘@type’] = ‘OfferShippingDetails’;
    $entity[‘offers’][‘shippingDetails’][‘shippingRate’][‘@type’] = ‘MonetaryAmount’;
    $entity[‘offers’][‘shippingDetails’][‘shippingRate’][‘value’] = 5;
    $entity[‘offers’][‘shippingDetails’][‘shippingRate’][‘currency’] = ‘USD’;
    $entity[‘offers’][‘shippingDetails’][‘shippingDestination’][‘@type’] = ‘DefinedRegion’;
    $entity[‘offers’][‘shippingDetails’][‘shippingDestination’][‘addressCountry’] = ‘US’;

    return $entity;
    });

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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.

Viewing 3 replies - 1 through 3 (of 3 total)

The ticket ‘Missing field hasMerchantReturnPolicy ❗DOESNT WORK FOR VARIABLE PRODUCTS’ is closed to new replies.