Your cart is currently empty!
Single product with no price place email us button
Adds a Request Price form to single product pages for all products without prices.
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' );
} );
Instructions for Single product with no price place email us button
- Log into a staging or locally hosted clone of your site.
- Install and activate Code Snippets plugin.
- WP Admin > Snippets > Add New
- Copy and paste the code from the section above.
- Check to ensure formatting came over properly.
- 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.
Need help modifying Single product with no price place email us button?
Contact me. I can help with fitting projects or refer to my partner.
License
All code snippets are licensed GPLv2 (or later) matching WordPress licensing.
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.
Disclaimer of warranty