How to properly echo product price inside another function

I've got two functions I'm using for a project I am working on. One of them modifies a WooCommerce page layout and the other returns "Call for price" on products that have no price set. Here they are below: // Modify category page layout function woocommerce_after_shop_loop_item_title_short_description() { global $product; if ( ! $product->get_short_description() ) return; ?> <div class="category_description_container" itemprop="description"> <h2><?php the_field('product_title'); ?> <span class="non-bold"><?php the_field('product_subtitle'); ?></span></h2> <div class="category_description"> <?php the_field('category_description'); ?> </div> <div class="category_price"> <?php echo apply_filters( 'woocommerce_price', $product->get_price() ) ?> </div> </div> <?php } add_action('woocommerce_after_shop_loop_item_title', 'woocommerce_after_shop_loop_item_title_short_description', 5); // Display "call for price" on products with no price set function wpgeeks_price_override( $price, $product ) { if ( empty( $product->get_price() ) ) { $price = __( 'Call for price', 'textdomain' ); } return $price; } add_filter( 'woocommerce_get_price_html', 'wpgeeks_price_override', 100, 2 ); How can I combine that "Call for price" function into the category page layout function so that when it echoes the price, it echoes the correct price from the "Call for price" function. I'm very new to PHP so I know enough to get by but not enough to figure this one out...yet. Thanks in advance, folks!

Comment (1)

Jese Leos

September 12, 2024

Verified user

Instead of your <div class="category_price"> <?php echo apply_filters( 'woocommerce_price', $product->get_price() ) ?> </div> Use the WC_Product class's built in get_price_html function (Reference) to output the price - which will call your second function automatically: <div class="category_price"> <?php echo $product->get_price_html() ?> </div>

You’ll be in good company