Halloween Sale 15% off Ends in See the plans
PHP

Shipping and Return Policy in Product Structured Data

Adds your shipping cost, delivery time and return policy to the product structured data WooCommerce outputs, which Google can show in shopping results.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_schema_policy = array(
    'country'        => 'GB',  // Two-letter country code you ship to.
    'shipping_cost'  => '3.95',
    'handling_days'  => array( 0, 1 ),
    'transit_days'   => array( 1, 3 ),
    'return_days'    => 30,
    'free_returns'   => true,
);

add_filter( 'woocommerce_structured_data_product', function ( $markup, $product ) use ( $wpide_schema_policy ) {
    if ( empty( $markup['offers'] ) || ! $product->needs_shipping() ) {
        return $markup;
    }

    $currency = get_woocommerce_currency();
    $shipping = array(
        '@type'               => 'OfferShippingDetails',
        'shippingRate'        => array( '@type' => 'MonetaryAmount', 'value' => $wpide_schema_policy['shipping_cost'], 'currency' => $currency ),
        'shippingDestination' => array( '@type' => 'DefinedRegion', 'addressCountry' => $wpide_schema_policy['country'] ),
        'deliveryTime'        => array(
            '@type'        => 'ShippingDeliveryTime',
            'handlingTime' => array( '@type' => 'QuantitativeValue', 'minValue' => $wpide_schema_policy['handling_days'][0], 'maxValue' => $wpide_schema_policy['handling_days'][1], 'unitCode' => 'DAY' ),
            'transitTime'  => array( '@type' => 'QuantitativeValue', 'minValue' => $wpide_schema_policy['transit_days'][0], 'maxValue' => $wpide_schema_policy['transit_days'][1], 'unitCode' => 'DAY' ),
        ),
    );
    $returns  = array(
        '@type'                => 'MerchantReturnPolicy',
        'applicableCountry'    => $wpide_schema_policy['country'],
        'returnPolicyCategory' => 'https://schema.org/MerchantReturnFiniteReturnWindow',
        'merchantReturnDays'   => $wpide_schema_policy['return_days'],
        'returnMethod'         => 'https://schema.org/ReturnByMail',
        'returnFees'           => $wpide_schema_policy['free_returns'] ? 'https://schema.org/FreeReturn' : 'https://schema.org/ReturnFeesCustomerResponsibility',
    );

    foreach ( $markup['offers'] as $i => $offer ) {
        $markup['offers'][ $i ]['shippingDetails']         = $shipping;
        $markup['offers'][ $i ]['hasMerchantReturnPolicy'] = $returns;
    }

    return $markup;
}, 10, 2 );

Notes

Describe your main market in $wpide_schema_policy, and keep the values true to your real offer or Google may ignore them.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.