Gustavo
Gustavo

Reputation: 95

Make woocommerce function when price set = 0

Hi sometimes I use in woocommerce products without price and I want add to empty price label or add a new label with a text "not available"

I try to make a hook but I'm not lucky and my skills are low. The condition would be something:

if( $product->get_price() == 0 ||  $product->get_price() == '') {
     echo '<p class="label_not_available">Not Available</p>';
}

Thanks in advance

Upvotes: 1

Views: 2101

Answers (2)

Gustavo
Gustavo

Reputation: 95

Thanks for preview answer, it's helped to make the hook with a correct syntax. Exactly I was looking to make this, and I think that work fine.

add_filter( 'woocommerce_get_price_html', 'custom_price_html', 10, 2 );

function custom_price_html( $price, $product ) {
  if( $product->get_price() == 0 ||  $product->get_price() == '') {
  $custom_text = '<p class="label_not_available">Not Available</p>';
  }
return $price . $custom_text;
}

I don't use free price because it is result of an API that returns the value price set = 0 sometimes when a product not available.

Thank you very much

Upvotes: 3

Nitish Thakrar
Nitish Thakrar

Reputation: 515

add_filter( 'woocommerce_variable_free_price_html', 'hide_free_price_notice', 10, 2 );
add_filter( 'woocommerce_variation_free_price_html', 'hide_free_price_notice', 10, 2 );
add_filter( 'woocommerce_free_price_html',          'hide_free_price_notice', 10, 2 );

/**
 * Hides the 'Free!' price notice
 */
function hide_free_price_notice( $price, $product ) {
  return '<p class="label_not_available">Not Available</p>';
}

Try this if it helps you. See Description Here.

Upvotes: 2

Related Questions