WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce - excelling eCommerce
[resolved] Code to output price? (10 posts)

  1. yalrighty
    Member
    Posted 11 months ago #

    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/

  2. yalrighty
    Member
    Posted 11 months ago #

    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

  3. Roy Ho
    Member
    Posted 11 months ago #

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

    global $product;
  4. yalrighty
    Member
    Posted 11 months ago #

    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.

  5. yalrighty
    Member
    Posted 11 months ago #

    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

  6. yalrighty
    Member
    Posted 11 months ago #

    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.

  7. Roy Ho
    Member
    Posted 11 months ago #

    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();
  8. yalrighty
    Member
    Posted 11 months ago #

    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.

  9. Roy Ho
    Member
    Posted 11 months ago #

    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...

  10. JS Morisset
    Member
    Posted 11 months ago #

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

    js.

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.