Code Snippets don’t work.

#892934
Viewing 7 replies - 1 through 7 (of 7 total)
  • Hello,

    We apologize for the issue you are facing with the code snippets not working in your rank-math.php file. There could be several reasons why the snippets are not producing the expected output:

    1. Code Placement: Ensure that the code snippets are placed correctly within the rank-math.php file and that there are no syntax errors.

    2. Theme Compatibility: Sometimes, the active theme may have its own settings or conflicts that prevent the snippets from executing properly. Make sure your theme is compatible with Rank Math.

    3. Plugin Conflicts: Other plugins may conflict with Rank Math or the specific snippets you are trying to use. Try deactivating other plugins temporarily to see if that resolves the issue.

    4. Caching Issues: Even after purging the cache, sometimes server-side caching can still hold onto old data. Ensure that all caches (including server cache) are cleared.

    5. PHP Errors: Check your server’s error logs for any PHP errors that might indicate issues with the code snippets.

    If you have checked all these aspects and the problem persists, we recommend replying to your ticket, and a human agent from the Rank Math Support team will assist you further. Thank you for raising this concern!

    Sources:
    How to Make Your Theme Rank Math Compatible » Rank Math
    Remove Organization Schema from Your Website » Rank Math

    Hello,

    Thank you for your query and we are so sorry about the trouble this must have caused.

    Please try the following code and see if this would work on your product schema since you are using ProductGroup property:

    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'    => 500000,
    			'currency' => 'IRR',
    		],
    		'shippingDestination' => [
    			'@type' => 'DefinedRegion',
    			'addressCountry' => 'IR'
    		]
    	];
    	$data['hasMerchantReturnPolicy'] = [
    		'@context'     => 'https://schema.org/',
    		'@type'        => 'MerchantReturnPolicy',
    		'@id'          => '#merchant_policy',
    		'applicableCountry' => 'IR',
    		'returnPolicyCategory' => 'https://schema.org/MerchantReturnFiniteReturnWindow',
    		'merchantReturnDays' => 7,
    		'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'];
    	}
    
    	return $data;
    }, 99, 2);
    

    Let us know how that goes. Looking forward to helping you.

    Yes, The code you provided is working well for “hasMerchantReturnPolicy”.

    Can you check the currency problem too?
    Problem is described in this topic:
    https://support.rankmath.com/ticket/woocommerce-schema-invalid-currency/

    Hello,

    We would like to test this further on your website but we no longer have access to your dashboard as the username is not working.

    Can you please verify this from your end?

    In the meantime, please replace all the codes with this 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'    => 500000,
    			'currency' => 'IRR',
    		],
    		'shippingDestination' => [
    			'@type' => 'DefinedRegion',
    			'addressCountry' => 'IR'
    		]
    	];
    	$data['hasMerchantReturnPolicy'] = [
    		'@context'     => 'https://schema.org/',
    		'@type'        => 'MerchantReturnPolicy',
    		'@id'          => '#merchant_policy',
    		'applicableCountry' => 'IR',
    		'returnPolicyCategory' => 'https://schema.org/MerchantReturnFiniteReturnWindow',
    		'merchantReturnDays' => 7,
    		'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);

    Looking forward to helping you.

    Rankmath functionality is right and showing the correct structure in all product pages.

    There is 4 currencies that we use in our country, but just the first one is supported in ISO_4217:

    IRR
    IRHR
    IRT
    IRHT

    This currencies can be converted to each other this way: (Their difference is in their zeros)
    1000 IRR = 1 IRHR
    1000 IRT = 1 IRHT
    and
    1 IRT = 10 IRR
    ————–
    Take this product for example:
    https://kazemibike.com/mountain-bike/cube-reaction-c62-pro-2023/

    Price in page is : 162,000,000 IRT
    Price in rankmath schema is : 1,620,000,000 IRR
    Price showing on google search results: 1,620,000,000.00 IRR

    Because prices are so long and confusing , Is it possible that we remove 1 zeros from the price in schema, but keep the currency same as IRR? (price in IRT but currency in IRR)
    I mean it’s correct that our main currency is IRR but it’s better and user friendly to show it like IRT.

    Hello,

    Google will remove the product snippet from the SERPs and can also affect the rankings of your products if you submit incorrect information in the Schema. So, it is recommended to submit the correct price in IRR in the Schema.

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    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 7 replies - 1 through 7 (of 7 total)

The ticket ‘Code Snippets don’t work.’ is closed to new replies.