"Price on Request" for a Category
Hides prices and the Add to Cart button for products in chosen categories and shows a "Request a price" button that links to your contact page.
Opens WPIDE on
Code
$wpide_por = array(
'categories' => array( 'bespoke' ), // Category slugs.
'label' => 'Price on request',
'button' => 'Request a price',
'url' => '/contact/', // Contact page URL.
);
$wpide_por_match = function ( $product ) use ( $wpide_por ) {
$id = $product->get_parent_id() ? $product->get_parent_id() : $product->get_id();
return has_term( $wpide_por['categories'], 'product_cat', $id );
};
add_filter( 'woocommerce_get_price_html', function ( $html, $product ) use ( $wpide_por, $wpide_por_match ) {
return $wpide_por_match( $product ) ? '<span class="wpide-por">' . esc_html( $wpide_por['label'] ) . '</span>' : $html;
}, 20, 2 );
add_filter( 'woocommerce_is_purchasable', function ( $purchasable, $product ) use ( $wpide_por_match ) {
return $purchasable && ! $wpide_por_match( $product );
}, 10, 2 );
add_action( 'woocommerce_single_product_summary', function () use ( $wpide_por, $wpide_por_match ) {
global $product;
if ( $product instanceof WC_Product && $wpide_por_match( $product ) ) {
printf(
'<p><a class="button alt wpide-por-button" href="%s">%s</a></p>',
esc_url( add_query_arg( 'product', rawurlencode( $product->get_name() ), home_url( $wpide_por['url'] ) ) ),
esc_html( $wpide_por['button'] )
);
}
}, 30 );
Notes
The product title is added to the contact link as ?product=, which form plugins can use to prefill a field.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.