Automatically populate custom fields value to Product Scheme fields

#915196
  • Resolved 中村 真由美
    Rank Math free

    Hi team,

    Thanks as always.
    I would love to apply Product scheme to these custom posts called “inventory” (See sensitive data section for URL)

    As “inventory” posts already has price, availability values and such, so I’d love them to be automatically applied to Scheme fields, without setting them manually in RankMath settings.

    Do I need to tweak php code to do this?
    To manage the custom fields we use SCF not ACF, and a custom field “inventory_situation” contains availability(“soldout” is a value when unavailable), “inventory_price” contains its price information in JPY.

    Here’s a mock code, that off course didn’t work 😛


    add_action('save_post_inventory', function($post_id) {
    // retrieve custom fields value
    $situation = get_post_meta($post_id, 'inventory_situation', true);
    $price = get_post_meta($post_id, 'inventory_price', true);

    // Rank Mathのスキーマデータを取得
    $schema = get_post_meta($post_id, 'rank_math_schema_Product', true);

    if ($schema) {
    // スキーマデータを更新
    $schema['price'] = $price;
    $schema['availability'] = ($situation == 'soldout') ? 'OutOfStock' : 'InStock';

    // 更新したスキーマデータを保存
    update_post_meta($post_id, 'rank_math_schema_Product', $schema);
    }
    });

    Thanks for your help, in advance
    Mayumi

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

    Thank you for contacting support.

    It’s possible to change the values of the Product Schema using the data from ACF fields but you would need to use a different filter from our plugin to change that data.

    The filter that can be used for that purpose is the following: https://rankmath.com/kb/filters-hooks-api-developer/#change-post-schema-data

    Please note that this only changes the Schema if it’s already available on the page so you would need to first add it manually to the pages and then change the data dynamically by using that filter and performing the fetching of the data inside that filter and associate it with the properties which reside inside the $entity variable.

    Don’t hesitate to get in touch if you have any other questions.

    Hi Miguel,

    Thanks for your reply! Following your guide, I tried using add_filter(‘rank_math/snippet/rich_snippet_product_entity’, …) in the code below:

    
    add_filter('rank_math/snippet/rich_snippet_product_entity', function($entity) {
        $product_id = $entity['@id']; // Get the product ID
        
        $product_info = get_custom_product_price($product_id);
        
        if ($product_info['price'] !== null) {
            $entity['offers']['price'] = $product_info['price'];
            $entity['offers']['priceCurrency'] = 'JPY';
        }
        
        $entity['offers']['availability'] = 'https://schema.org/' . $product_info['availability'];
        
        return $entity;
    });
    
    function get_custom_product_price($product_id) {
        // Get inventory status
        $inventory_situation = SCF::get('inventory_situation', $product_id);
        
        // Get price
        $inventory_price = SCF::get('inventory_price', $product_id);
        
        // Set price based on inventory status
        if ($inventory_situation === 'nomal') {
            $price = $inventory_price;
        } else {
            // If sold out, don't set a price
            $price = null;
        }
        
        return [
            'price' => $price,
            'availability' => $inventory_situation === 'nomal' ? 'InStock' : 'OutOfStock'
        ];
    }
    

    When checking via validator tool, its scheme markup doesn’t show up for each custom post, such as
    https://wagashi.fida.team/?inventory=%e3%83%9d%e3%83%ab%e3%82%b7%e3%82%a7
    The Product scheme is properly applied to this custom post type in RankMath dashboard.

    Anything is wrong with my code or any settings is missing?
    I appreciate your warm help!

    Regards,
    Mayumi

    Hello,

    The @id property in the Schema is not the same as the product ID. You should use the get_queried_object_id() function to get the product ID, and the filter should work fine.

    Hope that helps and please do not hesitate to let us know if you need our assistance with anything else.

    Hi Jaideep,

    Thanks for your note! Following the advise, I updated the code like below:

    add_filter('rank_math/snippet/rich_snippet_product_entity', function($entity) {
        $product_id = get_queried_object_id(); // Get the current post ID
        
        $product_info = get_custom_product_price($product_id);
        
        if ($product_info['price'] !== null) {
            $entity['offers']['price'] = $product_info['price'];
            $entity['offers']['priceCurrency'] = 'JPY';
        }
        
        $entity['offers']['availability'] = 'https://schema.org/' . $product_info['availability'];
        
        return $entity;
    });
    
    function get_custom_product_price($product_id) {
        // Get inventory status
        $inventory_situation = SCF::get('inventory_situation', $product_id);
        
        // Get price
        $inventory_price = SCF::get('inventory_price', $product_id);
        
        // Set price based on inventory status
        if ($inventory_situation === 'nomal') {
            $price = $inventory_price;
        } else {
            // If sold out, don't set a price
            $price = null;
        }
        
        return [
            'price' => $price,
            'availability' => $inventory_situation === 'nomal' ? 'InStock' : 'OutOfStock'
        ];
    }

    However, it still doesn’t work properly… Anything wrong with this code?

    Regards,
    Mayumi

    Sorry for confusing, the code works fine!
    However, the Product scheme values are not visible just after adding new posts with price and availability custom fields. Looks like it takes some time for these values to be populated to Scheme.

    So could I ask what is the trigger? How long should I expect for Scheme values to be updated after I update the custom fields value?

    Regards,
    Mayumi

    One more question related – in my sample code, I set price to “null” when its out of stock. But in this case, Google Rich Result test retuens error (see attached).
    screenshot

    What do you recommend to set as price when its not available?

    Mayumi

    Hello,

    The filter runs after the page loads, so it will be applied once you save/publish the page and check the values using the Rich result test tool.

    Regarding the price, you should remove the property from the offers if it is not available.

    Here’s a link for more information:
    https://support.google.com/webmasters/thread/2444180/schema-mark-up-offers-when-price-only-available-upon-request?hl=en

    Looking forward to helping you.

    Thanks for your help!
    It was really helpful 🙂

    Hello,

    We are super happy that this resolved your issue.

    If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.

    Thank you.

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

The ticket ‘Automatically populate custom fields value to Product Scheme fields’ is closed to new replies.