I get this error when trying to render the product price before the main content on the page.
Fatal error: Uncaught Error: Call to a member function get_price_html() on string in /wp-content/plugins/woocommerce/templates/single-product/price.php:27
Here is my code:
//the remove action works fine
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
//this breaks it
add_action('woocommerce_before_main_content', 'woocommerce_template_single_price', 40);
I assume that because I'm trying to get the price before the main content I need to make sure the global $product
is loaded.
How would I ensure global $product
is loaded?
I don't think that what you wan't to do is possible. You are moving the rendering of price.php before the loop and outside the add-to-cart form. Then get_price_html()
need the variable $price
, not available in here.
To do that, I suppose you need to change in price.php
template with something like:
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $product;
$price = esc_attr( $product->get_price() );
// $price = woocommerce_price( $product->regular_price ); // or this one
// $price = woocommerce_price( $product->sale_price ); // or this one too
?>
<div itemprop="offers" itemscope itemtype="http://schema.org/Offer">
<p class="price"><?php echo $product->get_price_html(); ?></p>
<meta itemprop="price" content="<?php echo esc_attr( $product->get_price() ); ?>" />
<meta itemprop="priceCurrency" content="<?php echo esc_attr( get_woocommerce_currency() ); ?>" />
<link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />
</div>
Or replace $product->get_price_html()
by esc_attr( $product->get_price() );
You might need to insert the loop inside it too…
Reference: Overriding Templates via a Theme
Just copy the code from the single product price template and stick that into your hooked function.
https://github.com/woothemes/woocommerce/blob/master/templates/single-product/price.php
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
function price_in_title_row() {
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $product;
?>
<div class="price_in_title_row" itemprop="offers" itemscope itemtype="http://schema.org/Offer">
<h3><?php echo $product->get_price_html(); ?></h3>
<meta itemprop="price" content="<?php echo esc_attr( $product->get_display_price() ); ?>" />
<meta itemprop="priceCurrency" content="<?php echo esc_attr( get_woocommerce_currency() ); ?>" />
<link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />
</div>
}
add_action('woocommerce_before_single_product', 'price_in_title_row', 50);