Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I combine these two PHP functions into one?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading