Home / Admin / Add deliveryTime schema to shippingDetails schema
Duplicate Snippet

Embed Snippet on Your Site

Add deliveryTime schema to shippingDetails schema

This snippet adds deliveryTime schema to shippingDetails schema that's present inside the Product > Offers schema

<10
Code Preview
php
<?php
add_filter( 'aioseo_schema_output', function ( $schema ) {
    foreach ( $schema as &$schemaItem ) {
        if ( isset( $schemaItem['@type'] ) && 'Product' === $schemaItem['@type'] ) {
			if(!empty($schemaItem["offers"]["shippingDetails"])){
				// Loop inside the Offers > shippingDetails schema
				foreach( $schemaItem["offers"]["shippingDetails"] as &$schemaItemChild ){
				// Check for OfferShippingDetails schema where we need to add the deliveryTime schema
				if( isset( $schemaItemChild['@type'] ) && 'OfferShippingDetails' === $schemaItemChild['@type'] ){
					// Adding the deliveryTime schema
					$schemaItemChild["deliveryTime"] = [
						"@type" => "ShippingDeliveryTime",
						"handlingTime" => [
							"@type" => "QuantitativeValue",
							"minValue" => 1,
							"maxValue" => 2,
							"unitCode" => "d"
						],
						"transitTime" => [
							"@type" => "QuantitativeValue",
							"minValue" => 1,
							"maxValue" => 3,
							"unitCode" => "d"
						]
					];
				}
			}		
			}	
        }
    }
    return $schema;
}, 10, 2 );

Comments

Add a Comment