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?
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
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:Or replace
$product->get_price_html()
byesc_attr( $product->get_price() );
You might need to insert the loop inside it too…Reference: Overriding Templates via a Theme