Created
April 25, 2024 15:05
-
-
Save wp-seopress/2183f78c865902c5d0efd05413e4c414 to your computer and use it in GitHub Desktop.
How to solve "missing fields" errors in Google Structured Data Types (schemas) - eg with product schema
This file contains 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
add_filter('seopress_schemas_auto_product_json', 'sp_schemas_auto_product_json'); | |
function sp_schemas_auto_product_json($json) { | |
// Returns schema as an array with 'property' => 'value' | |
// Get the product offers | |
$offers = $json['offers']; | |
// Add sub-type MerchantReturnPolicy and deliveryTime to each offer | |
if (!empty($offers)) { | |
foreach ($offers as $key => $offer) { | |
//hasMerchantReturnPolicy | |
$offers[$key]['hasMerchantReturnPolicy'] = | |
[ | |
'@type' => 'MerchantReturnPolicy', | |
'applicableCountry' => 'FR', | |
'returnPolicyCategory' => 'https://schema.org/MerchantReturnFiniteReturnWindow', | |
'merchantReturnDays' => 120, | |
'returnMethod' => 'https://schema.org/ReturnByMail', | |
'returnFees' => 'https://schema.org/FreeReturn' | |
]; | |
// Check if 'shippingDetails' is present in the offer | |
if (isset($offer['shippingDetails'])) { | |
// Iterate through shipping details and add 'deliveryTime' | |
foreach ($offer['shippingDetails'] as $k => $shippingDetail) { | |
$offers[$key]['shippingDetails'][$k]['deliveryTime'] = | |
[ | |
'@type' => 'ShippingDeliveryTime', | |
'handlingTime' => [ | |
'@type' => 'QuantitativeValue', | |
'minValue' => 0, | |
'maxValue' => 1, | |
'unitCode' => 'DAY' | |
], | |
'transitTime' => [ | |
'@type' => 'QuantitativeValue', | |
'minValue' => 1, | |
'maxValue' => 5, | |
'unitCode' => 'DAY' | |
] | |
]; | |
} | |
} | |
} | |
} | |
// Update the original JSON with modified offers | |
$json['offers'] = $offers; | |
return $json; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment