hasmerchantreturn & shippingdetail schema

#982147
  • Resolved Areg Avak
    Rank Math free

    is this my code right?
    i have downloadable and also physcial product
    add_filter(‘rank_math/snippet/rich_snippet_product_entity’, function($entity) {
    // فقط توی صفحات تک‌محصول اجرا بشه
    if (!is_product()) {
    return $entity;
    }

    global $product;

    // چک کردن وجود محصول
    if (!$product || !is_a($product, ‘WC_Product’)) {
    return $entity;
    }

    // تنظیم پیش‌فرض برای offers اگه وجود نداشته باشه
    if (!isset($entity[‘offers’])) {
    $entity[‘offers’] = [];
    }

    if ($product->is_downloadable()) {
    // محصولات دانلودی
    $entity[‘offers’][‘shippingDetails’] = [
    ‘@type’ => ‘OfferShippingDetails’,
    ‘shippingRate’ => [
    ‘@type’ => ‘MonetaryAmount’,
    ‘value’ => 0,
    ‘currency’ => ‘IRR’
    ],
    ‘shippingDestination’ => [
    ‘@type’ => ‘DefinedRegion’,
    ‘addressCountry’ => ‘IR’
    ],
    ‘deliveryTime’ => [
    ‘@type’ => ‘ShippingDeliveryTime’,
    ‘handlingTime’ => [
    ‘@type’ => ‘QuantitativeValue’,
    ‘minValue’ => 0,
    ‘maxValue’ => 0,
    ‘unitCode’ => ‘DAY’ // تحویل فوری
    ],
    ‘transitTime’ => [
    ‘@type’ => ‘QuantitativeValue’,
    ‘minValue’ => 0,
    ‘maxValue’ => 0,
    ‘unitCode’ => ‘DAY’
    ]
    ]
    ];

    $entity[‘offers’][‘hasMerchantReturnPolicy’] = [
    ‘@type’ => ‘MerchantReturnPolicy’,
    ‘applicableCountry’ => ‘IR’,
    ‘returnPolicyCategory’ => ‘https://schema.org/MerchantReturnNotPermitted’,
    ‘merchantReturnDays’ => 0
    ];
    } else {
    // محصولات فیزیکی
    $entity[‘offers’][‘shippingDetails’] = [
    ‘@type’ => ‘OfferShippingDetails’,
    ‘shippingRate’ => [
    ‘@type’ => ‘MonetaryAmount’,
    ‘value’ => 50000, // قابل تغییر
    ‘currency’ => ‘IRR’
    ],
    ‘shippingDestination’ => [
    ‘@type’ => ‘DefinedRegion’,
    ‘addressCountry’ => ‘IR’
    ],
    ‘deliveryTime’ => [
    ‘@type’ => ‘ShippingDeliveryTime’,
    ‘handlingTime’ => [
    ‘@type’ => ‘QuantitativeValue’,
    ‘minValue’ => 1,
    ‘maxValue’ => 2,
    ‘unitCode’ => ‘DAY’
    ],
    ‘transitTime’ => [
    ‘@type’ => ‘QuantitativeValue’,
    ‘minValue’ => 1,
    ‘maxValue’ => 5,
    ‘unitCode’ => ‘DAY’
    ]
    ]
    ];

    $entity[‘offers’][‘hasMerchantReturnPolicy’] = [
    ‘@type’ => ‘MerchantReturnPolicy’,
    ‘applicableCountry’ => ‘IR’,
    ‘returnPolicyCategory’ => ‘https://schema.org/MerchantReturnFiniteReturnWindow’,
    ‘merchantReturnDays’ => 7,
    ‘returnMethod’ => ‘https://schema.org/ReturnByMail’,
    ‘returnFees’ => ‘https://schema.org/ReturnShippingFees’
    ];
    }

    return $entity;
    });

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello,

    I apologize for the issue you are facing. It looks like you are trying to implement the schema for both downloadable and physical products in WooCommerce. Your code snippet seems to be on the right track, but there are a few things to note:

    1. HTML Entities: Ensure that you replace the HTML entities like > with the actual > character in your PHP code. This is crucial for the code to function correctly.

    2. Shipping Details for Physical Products: You should also include the shipping details for physical products in the else part of your code, which seems to be truncated. Make sure to define the shipping details similarly as you did for downloadable products, but with appropriate values for physical items.

    3. Return Policy: The return policy for downloadable products is set to not permitted (MerchantReturnNotPermitted). If you have a different policy for physical products, ensure that it is defined accordingly in the else block.

    Here’s a brief example of how you might structure the else block for physical products:

    else {
        // محصولات فیزیکی
        $entity['offers']['shippingDetails'] = [
            '@type' => 'OfferShippingDetails',
            'shippingRate' => [
                '@type' => 'MonetaryAmount',
                'value' => 200,
                'currency' => 'IRR'
            ],
            'shippingDestination' => [
                '@type' => 'DefinedRegion',
                'addressCountry' => 'IR'
            ],
            'deliveryTime' => [
                '@type' => 'ShippingDeliveryTime',
                'handlingTime' => [
                    '@type' => 'QuantitativeValue',
                    'minValue' => 1,
                    'maxValue' => 3,
                    'unitCode' => 'DAY'
                ],
                'transitTime' => [
                    '@type' => 'QuantitativeValue',
                    'minValue' => 1,
                    'maxValue' => 5,
                    'unitCode' => 'DAY'
                ]
            ]
        ];
    
        $entity['offers']['hasMerchantReturnPolicy'] = [
            '@type' => 'MerchantReturnPolicy',
            'applicableCountry' => 'IR',
            'returnPolicyCategory' => 'https://schema.org/MerchantReturnFiniteReturnWindow',
            'merchantReturnDays' => 30,
            'returnMethod' => 'https://schema.org/ReturnByMail',
            'returnFees' => 'https://schema.org/FreeReturn'
        ];
    }
    1. Testing: After making these adjustments, test your schema using Google’s Rich Results Test tool to ensure everything is working as expected.

    If you have any further questions or need more assistance, feel free to reply to this ticket, and a human agent from the Rank Math Support team will assist you further!

    Sources:
    How to Add Merchant Return and Shipping Policy in WooCommerce? » Rank Math

    Thanks

    Hello Rank Math Support Team,
    I hope you’re doing well. Thank you for your previous help with my schema code for WooCommerce products. I have a quick question about a specific line I’ve added to the code to ensure it works smoothly:

    // Set a default for offers if it doesn’t exist
    if (!isset($entity[‘offers’])) {
    $entity[‘offers’] = [];
    }

    I included this check to avoid potential “undefined index: offers” errors in case the $entity[‘offers’] array isn’t already defined by Rank Math. Since my site includes both downloadable and physical products, I wanted to make sure the code is robust. Is this line necessary, or does Rank Math always guarantee that $entity[‘offers’] is defined in the rank_math/snippet/rich_snippet_product_entity filter? Should I keep it in my code, or is it safe to remove it?

    I’d really appreciate your advice on this. Thank you so much for your time and support!

    Hello,

    Thank you for your patience.

    You should keep it in your code since the offers property is not created if they are non-existing on your products, as Rank Math automatically fetches the data from WooCommerce.

    Looking forward to helping you.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)

The ticket ‘hasmerchantreturn & shippingdetail schema’ is closed to new replies.