Skip to content

Instantly share code, notes, and snippets.

@wp-seopress
Last active February 18, 2025 10:04
Show Gist options
  • Save wp-seopress/b0516e08105e8c0eb4a78c715abefb58 to your computer and use it in GitHub Desktop.
Save wp-seopress/b0516e08105e8c0eb4a78c715abefb58 to your computer and use it in GitHub Desktop.
Add "MerchantReturnPolicy" and "deliveryTime" to each product offer for the automatic product schema with SEOPress PRO
<?php
//Add "MerchantReturnPolicy" and "deliveryTime" to each product offer (simple and variable) for the automatic product schema with SEOPress PRO
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'
// Check if 'offers' exists in the JSON
if (!isset($json['offers'])) {
return $json;
}
// Get the product offers
$offers = $json['offers'];
// Ensure $offers is an array of offers
if (!is_array($offers) || array_keys($offers) !== range(0, count($offers) - 1)) {
$offers = [$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