Insert contact button when woocommerce product price is empty

708 0

In some clients’ WordPress and WooCommerce based ecommerces the need appeared to replace the normal “Add to cart” button with a “Contact” or “Get a quote” button instead, in order to link to the contact section.

To achive this, I tried some WordPress plugins, but their behaviour was not satisfying me because of the layouts or other facts, so in the end the easier and more customizable solution I found was to edit the functions.php file of the child theme to insert this action hook which inserts the button when the WooCommerce product price is empty.

function add_contact_link() {

  global $product;

  if ( $product->get_price() === '' ) :
    echo '<a href="/contact/" class="">Contact</a>';
  endif;
}
add_action( 'woocommerce_single_product_summary', 'add_contact_link', 100 );

This way we can add the classes or insert the html tags we need to have the control on the layout and behaviour we are searching for.

I hope you find this tip useful. Feel free to leave your feedback.

Carlos Pérez

Digital manager, tech lead, product designer, full stack engineer, web and app developer, SEO, digital marketing, automation and AI expert.

Leave a Reply

Your email address will not be published. Required fields are marked *