• Hi there ??

    In my WordPress installation I use woocommerce with gravityforms.
    I have to set the price for my products to zero, because price is calculated with gravity forms.
    Now I have the problem, that in
    <meta itemprop="price" content="<?php echo esc_attr( $product->get_display_price() ); ?>" />
    the price is displayed as 0.
    How can I echo the calculated price into the itemprop=”price” ?

    Thank you very much for your help!
    Best wishes
    Malte

Viewing 2 replies - 1 through 2 (of 2 total)
  • Just wanted to share this with you as i was looking at the same exact problem.

    Add this to your /theme/woocommerce/single-products/price.php

    //get the regular price of the product, but of a simple product
    $regular_price = get_post_meta( get_the_ID(), '_regular_price', true);
    $sale_price = get_post_meta( get_the_ID(), '_price', true);
    
    if ($regular_price >= $sale_price){
    $available_variations = $product->get_available_variations();
    
    $variation_id=$available_variations[0]['variation_id']; 
    $variable_product1= new WC_Product_Variation( $variation_id );
    $regular_price = $variable_product1 ->sale_price;
    }
    if ($sale_price >= $regular_price){
    $available_variations = $product->get_available_variations();
    
    $variation_id=$available_variations[0]['variation_id']; 
    $variable_product1= new WC_Product_Variation( $variation_id );
    $regular_price = $variable_product1 ->regular_price;
    }

    Then find <meta itemprop="price" content="<?php echo esc_attr( $product->get_price() ); ?>" /> and replace it with <meta itemprop="price" content="<?php echo esc_attr( $regular_price ); ?>" />

    That should now work for single, and variable products.

    The above code works only for products that are variable, need to find a fix for ‘simple’ products. Anybody who could chip in?

    This seems to work. Care to check?

    // price fix for schema showing zero when variable
    if($product->product_type=='variable') {
    	$available_variations = $product->get_available_variations();
    	$variation_id=$available_variations[0]['variation_id']; 
    	$variable_product1= new WC_Product_Variation( $variation_id );
    	$regular_price = $variable_product1 ->regular_price;
    	$sales_price = $variable_product1 ->sale_price;
    		if ($sales_price == ""){
    			$display_price = $regular_price;
    		}
    		else {
    			$display_price = $sales_price;
    		}
    }
    else{
    	$display_price = $product->get_price();
    }

    Make sure to change this <meta itemprop="price" content="<?php echo esc_attr( $regular_price ); ?>" /> to <meta itemprop="price" content="<?php echo esc_attr( $display_price ); ?>" />

    • This reply was modified 7 years, 10 months ago by jaysnl.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Variable price for schema.org’ is closed to new replies.