Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wp-seopress/2183f78c865902c5d0efd05413e4c414 to your computer and use it in GitHub Desktop.
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
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