0

How do I get the Woocommerce product price I would get using <?php echo $product->get_price_html(); ?> in a non Timber Wordpress template?

I tried {{ product.price }} but it triggers an error which says:

price was called incorrectly. Product properties should not be accessed directly.

I know that I can get it through the Woocommerce template using {% do action('woocommerce_single_product_summary') %}, but:

  • I want the raw unformatted text, not the content of the Woocommerce template.
  • the Woocommerce action also gets a bunch of other stuff I don't need.

So, how do I access it correctly?

1 Answer 1

0

I ended up adding a custom function to Timber that returns the raw price value. This goes in the functions.php file:

public function add_to_twig( $twig ) {
  $function = new Twig_Function('myprice', function () {
    global $product;
    $myprice = $product->get_price();
    return $myprice;
  });
  $twig->addFunction($function);
 
  return $twig;
}

it can be used in any Twig template with {{ myprice() }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.