0

I need a function that replaces the price in WooCommerce with "Request a quote". On my site's homepage I just need the text, but on the product page (which is a custom product from my travel theme) I need it to be a button.

How can I have 2 functions, one that only works on Home and the other one that works in the rest of the site?

In functions.php, something like:

IF IS_HOME:

add_filter('woocommerce_empty_price_html', 'custom_call_for_price');
function custom_call_for_price() {
return 'Request a quote';
}

AND IF IS_NOT_HOME:

add_filter('woocommerce_empty_price_html', 'custom_call_for_price');
function custom_call_for_price() {
$html .= '<a href="#"><input value="Request a quote" type="submit"></a>';
return $html;
}

Can it be done? Thanks!

1

Use this:

function your_function() 
{
    if ( is_front_page() ) {
        $output = ...
    }
    else{
        $output = ...
    }
    return $output;
}

p.s. It's not needed to use is_home(). Whoever needs the technical details, see is_front_page and is_home

5
  • can you please help me integrate the rest of my code in your function, please? i don't see how I can 'cascade' functions...
    – Alex
    Sep 26 '18 at 12:53
  • Thanks, I managed it: add_filter('woocommerce_empty_price_html', 'custom_call_for_price'); function custom_call_for_price() { if(is_front_page()){ return 'Request a quote"'; }else{ $html .= '<a href="#"><input value="Request a quote"" type="submit"></a>'; return $html; } }
    – Alex
    Sep 26 '18 at 12:56
  • I changed is_home to is_front_page because it didn't work. it works with is_front_page and I'm going to leave it like this since it works. I need it for this project only, but will keep in mind what you said for future projects. Thanks again!
    – Alex
    Sep 26 '18 at 13:03
  • 1
    @T.Todua not true, what you are saying in the comments: is_home() is true on the blog page, no matter, if it is the home page of the site or not. the page for your posts will return true on is_home(). is_front_page() will return true, if you change the home page to a certain page in settings>reading and load that page (root)..
    – honk31
    Sep 26 '18 at 18:49
  • @honk31 thanks for further clarification, i made a mistake, your definition is more correct. I'll edit the answer.
    – T.Todua
    Sep 27 '18 at 14:07
0

You can try below code

function sv_change_product_price_display( $price ) {

    if(is_home() || is_front_page()){

            $price ='<button>Request Quote</button>';
    }

    return $price;
}
add_filter( 'woocommerce_get_price_html', 'sv_change_product_price_display' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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