WordPress.org

Support

Support » Plugins and Hacks » [Resolved] Code to output price?

[Resolved] Code to output price?

  • Hi. I have woocommerce and have open graph protocol and Social SEO booster. But they do not generate a price open graph tag.

    I wish to generate the following:

    <meta property=”og:price:amount” content=”15.00″ /> where 15.00 is the price of the product being viewed. This is so that I can have Pinterest Rich Pins.

    I have found the following codes looking thru the php files but neither seem to work for me?

    <?php echo $product->get_price_html(); ?>
    
    <?php echo get_woocommerce_single_product_price(); ?>

    It is probably something simple, but I am new to WP, Woocommerce, and not really a good php programmer yet.

    http://wordpress.org/plugins/woocommerce/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Still no luck on my end. Getting frustrating lol I either cause a while screen of death or the tag doesn’t show up. I hope someone can help me

    Roy Ho

    @splashingpixelscom

    You need to globalize the variable on that page for you to access its methods.

    global $product;

    ok so would it be like this?

    <?php global $product ?>

    Then

    <meta property="og:price:amount" content="<?php echo $product->get_price_html(); ?>" />

    Sorry I’m really not a php programmer as you can probably tell.

    I tried what I put above and the meta tag generates but it is coming out like this when I go to view page source

    <meta property="og:price:amount" content="<span class="amount">$67.95</span>" />

    So I am almost there … just need to change it so that it doesn’t spit out the span tags … I just need the price with no dollar sign

    I got it with the following code

    <?php global $product ?>
    <meta property="og:price:amount" content="<?php echo $product->get_price(); ?>" />

    I put that in the content-single-product.php file and it works fine and generates the proper open graph tag. But how do I move it so that it appears between the <head></head> tags with the other open graph tags? I tried putting it in header.php but it caused white screen of death.

    Roy Ho

    @splashingpixelscom

    If you’re doing it in the header, I am not sure if product has been globalized yet…

    So the other thing you can do is use $post global and create a new product object like so:

    global $post;
    
    $product = get_product( $post->ID );
    
    echo $product->get_price();

    Thank you so very very much!!!! That worked like a charm. I can’t thank you enough I have been confused on this for 2 days now lol.

    Roy Ho

    @splashingpixelscom

    Also you might want to add a conditional check as I don’t think you want that to be outputed on every page…

    if ( is_product() ) {
    // do your code here
    }

    This will make sure the page is on single product page…

    Have a look at NGFB Open Graph+ v6.6 coming out this week-end. 😉

    js.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘[Resolved] Code to output price?’ is closed to new replies.