Automatically populate custom fields value to Product Scheme fields

#915196
  • 中村 真由美
    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 4 replies - 1 through 4 (of 4 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

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

You must be logged in to reply to this ticket.