add_filter( 'woocommerce_empty_price_html', function( $price, $product ) {
if( ! is_product() ) {
return $price;
}
return sprintf(
'
<form method="POST" action="">
<input type="hidden" name="action" value="ccom_request_price" />
<input type="hidden" name="product_id" value="%d" />
<input type="email" name="billing_email" required="required"
placeholder="email address" aria-label="Email Address" />
<input type="submit" value="%s" />
</form>
',
$product->get_id(),
'Request Price'
);
}, 10, 2 );
add_action( 'template_redirect', function() {
if(
empty( $_REQUEST['action'] )
|| empty( $_REQUEST['billing_email'] )
|| empty( $_REQUEST['product_id'] )
|| $_REQUEST['action'] != 'ccom_request_price'
) {
return;
}
$headers = [
sprintf( 'From: %s <%s>', get_bloginfo( 'name' ), get_bloginfo( 'admin_email' ) ),
sprintf( 'Reply-To: Website Visitor <%s>', esc_attr( $_REQUEST['billing_email'] ) ),
];
$product = wc_get_product( intval( $_REQUEST['product_id'] ) );
$message = sprintf( 'Product: %s', $product->get_title() );
wp_mail( get_bloginfo( 'admin_email' ), 'Price Request', $message, $headers );
wc_add_notice( 'Your request has been sent. Thank you.', 'success' );
} );
Single product with no price place email us button
Adds a Request Price form to single product pages for all products without prices.
How to use
- Log into a staging, development, or locally hosted clone of your site
- Install and activate Code Snippets
- WP Admin > Snippets > Add New
- Copy and paste the code from the Description tab above
- Check to ensure formatting came over properly and no syntax errors show up in the editor
- Customize the code as desired
- Add a meaningful title
- Select whether to run on front-end or back-end (or both)
- Click “Save and Activate”
- Test your site to ensure it works
- Disable if any problems, or recover
- Repeat for live environment
License
All code snippets are licensed GPLv2 (or later) matching WordPress licensing.
Disclaimer of warranty:
Except when otherwise stated in writing the copyright holders and/or other parties provide the program as-is without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose.
Support
- Describe the issue and what you’ve observed.
- Describe your expected outcome(s).
- List steps to reproduce the issue.
- Optionally provide screen-shot or video URLs.
