skip to Main Content

Just like the title said:

How can i show only the sales price in Woocommerce if the regular price is blank?

2

Answers


  1. I would go for something like:

    $regular_price = get_post_meta( get_the_ID(), '_regular_price', true);
    $price_sale = get_post_meta( get_the_ID(), '_sale_price', true);
    if ($regular_price == '') {
        echo $price_sale;
    } else {
        echo $regular_price;
    }
    
    Login or Signup to reply.
  2. add_filter( 'woocommerce_get_price_html',  'change_product_price_html', 10, 2 );
    
    function change_product_price_html( $price_html, $product ) {
      global $post;
      $id = !empty( $product->get_id() ) ? $product->get_id() : $post->ID;
    
      $regular_price = get_post_meta( $id, '_regular_price', true);
      $sale_price = get_post_meta( $id, '_sale_price', true);
    
      $price_html = ( $regular_price == '' ) ? $sale_price : $regular_price;
    
      return $price_html;
    }
    

    Please use this code in your active theme’s functions.php file .

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search