About Using Price Variable in Schema

#602011
  • Resolved Anonymous
    Rank Math free

    Hello
    I am using Rank Math Pro. I have a situation and need some help. I use Product schema for my Woocommerce products. I am looking for a variable in the Price field in the schema setting. I offer a coupon for my products and want to find a variable to enter the product price with the discount, not the sale or regular price.
    I find a variable %wc_price%, its good, but it just shows the sale price of the product. Is there a way to use a formula for the previous variable?
    something like that; %wc_price% * 0.5

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

    Thank you for contacting Rank Math and bringing your concern to our attention. I’m sorry for any inconvenience this issue may have caused you.

    Yes, the %wc_price% variable will output the discounted price:
    https://rankmath.com/kb/variables-in-seo-title-description/#product-price

    In this case, you may need to register a new variable to output the regular price only. Here’s a filter you can use for that:

    add_action( 'rank_math/vars/register_extra_replacements', function(){
        rank_math_register_var_replacement(
            'reg_price', [
                'name'        => esc_html__( 'reg_price', 'rank-math' ),
                'description' => esc_html__( 'Output regular price', 'rank-math' ),
                'variable'    => 'reg_price',
                'example'     => reg_price(),
            ],
            'reg_price'
        );
    });
    
    function reg_price() {
    if(is_product()) {
    		 $product = wc_get_product();
           return $product->get_regular_price();
    	}
    }

    Here is a guide you can follow to add the filter:
    https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Once done, you can use the %reg_price% variable instead.

    Hope that helps.

    Thank you.

    Anonymous
    Rank Math free

    Thank you very much
    Now, If I want to set a formula to the price that I add at the schema

    I have a category of products on my website, I want to make a global price setting at the schema for this category price. I want the price added in the schema to be half of the regular price.

    So, can I use the following format:
    %reg_price% * 0.5

    One more thing please, can I set the previous setting you created for me on a particular category only on my website?

    Thanks

    Hello,

    In this case, please use this updated filter to achieve those:

    add_action( 'rank_math/vars/register_extra_replacements', function(){
        rank_math_register_var_replacement(
            'reg_price', [
                'name'        => esc_html__( 'reg_price', 'rank-math' ),
                'description' => esc_html__( 'Output regular price', 'rank-math' ),
                'variable'    => 'reg_price',
                'example'     => reg_price(),
            ],
            'reg_price'
        );
    });
    
    function reg_price() {
        $product = wc_get_product();
    	$categories = get_the_terms( $product->ID, 'product_cat' );
    	
    		if(is_product() && $categories[0]->name == 'category-slug') { //change the value to the actual product category slug
           return $product->get_regular_price() * (float)0.5;
    }
    	else {
           return $product->get_regular_price();
    	}
    }

    The price of the specific category of the product will be divided by half, and for the rest of your products, it will output the regular price using that variable.

    Hope that helps.

    Thank you.

    Anonymous
    Rank Math free

    Thank you very much for your help

    Hello,

    We are super happy that this resolved your issue. If you have any other questions in the future, know that we are here to help you.

    If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.

    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 5 replies - 1 through 5 (of 5 total)

The ticket ‘About Using Price Variable in Schema’ is closed to new replies.