The woocommerce product schema give me inaccurate price for when set it up

#898095
  • Resolved Ghazanio
    Rank Math free

    I am using global product schema for all my products, I use variable %wc_price% to set the price in the schema. However, I got a notification (invalid price format) in google search console, I checked the code of the schema and found that the price code is writen like this

    “price”: “$4,318”,
    The product price is 4,318 but I don’t know why this ($), but it seems it causes a problem.
    I searched for here at the support forum and saw a similar problem and the rank math support agent suggest to use the below filter then use variable %price%, instead of %wc_price%

    add_action( ‘rank_math/vars/register_extra_replacements’, function(){
    rank_math_register_var_replacement(
    ‘price’, [
    ‘name’ => esc_html__( ‘Price’, ‘rank-math’ ),
    ‘description’ => esc_html__( ‘Output the price of the product’, ‘rank-math’ ),
    ‘variable’ => ‘price’,
    ‘example’ => price_callback(),
    ],
    ‘price_callback’
    );
    });

    function price_callback() {
    $product = wc_get_product(get_queried_object_id());
    $price = ‘0’;

    if( $product ) {
    $price_vat = wc_get_price_including_tax( $product );
    $price = number_format($price_vat, 2);
    }

    return $price;

    I applied the filter code and replaced the variable, but got a new weird problem. I see that the price of the products are not the same as actual

    For example)
    a product with price: 1,120
    but it appears at the schema code as:
    “price”: “1,491.00”,

    I don’t where is the problem, but I think it is related to the hook filter I used, it might be designed specifically for a certain reason and client si it didn’t work with me.

    I hope you can help me fix it

    Thanks

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

    We apologize for the issue you are facing with the WooCommerce product schema and the inaccurate price formatting. It seems that the use of the global product schema and the variable %wc_price% is causing some complications, especially with the encoding of the dollar sign and the price format.

    The notification you received regarding the invalid price format in Google Search Console is likely due to the encoded dollar sign ($) in your schema. This can indeed lead to issues with how Google interprets your product pricing.

    Regarding the filter code you applied, it appears that there might be a discrepancy in how the price is being calculated or formatted. The price_callback function you provided retrieves the price including tax, which may not match the actual product price if your products are set up differently (e.g., if they are set to display prices excluding tax).

    To troubleshoot this issue:

    1. Check Product Pricing Settings: Ensure that your WooCommerce settings for displaying prices (including or excluding tax) align with what you want to show in your schema.
    2. Modify the Price Callback: You might want to adjust the price_callback function to ensure it retrieves the correct price based on your settings. For example, if you want to get the regular price without tax, you can use:
      $price = $product->get_regular_price();
    3. Debugging: Add some debugging statements within your price_callback function to log or output the values being retrieved. This can help identify where the discrepancy arises.
    4. Testing: After making changes, test your schema using Google’s Rich Results Test tool to see if the price is now formatted correctly.

    If these steps do not resolve the issue, we recommend replying to this ticket so a human agent from the Rank Math Support team can assist you further. Thank you for raising this concern, and we hope to help you get it sorted out soon!

    can you write the whole code please?

    please write the whole code supposing that there is not tax included

    Hello,

    Thank you for contacting support.

    The variable %wc_price% is not designed to be added to the Schema markup as it will include the currency symbol. That variable is used for people who would like to output this value somewhere inside the metadata such as the SEO title or SEO description.

    In this case, you would be better off using the WooCommerce Product Schema for your WooCommerce products as that already includes all the data of your products automatically and doesn’t require any custom variables.

    If you want to use the manual Schema markup with a variable you need to update the code you mentioned before to exclude the tax from the prices.

    That can be achieved with the function wc_get_price_excluding_tax() instead of the one used wc_get_price_including_tax().

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

    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 ‘The woocommerce product schema give me inaccurate price for when set it up’ is closed to new replies.