Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ben-heath/0ca4b7ea86b0e405efc71973fd0794f5 to your computer and use it in GitHub Desktop.
Save ben-heath/0ca4b7ea86b0e405efc71973fd0794f5 to your computer and use it in GitHub Desktop.
Hide WooCommerce Product Price and Add to Cart button of Specific products
// Add this to the child theme's functions.php
// This will hide the Price of the product, and swap it out with text of your choice. It will also remove the add to cart button.
// It could probably be more elegant, since some of the code is duplicated between the 2 functions, but it works just fine as is.
// Remove add to cart functionality for specific products
add_filter('woocommerce_is_purchasable', 'my_woocommerce_is_purchasable', 10, 2);
function my_woocommerce_is_purchasable($is_purchasable, $product) {
$skus = array('40-50-134','40-50-132','RB1000','RB1008','RB1009','RP1000','90-15-008','RP1009','RB1010','RB1011','RB1014','RB1016','RP1034','RP1036','40-50-125','RB1013','DB5018','DB5016','DB5006','XR5048','RG1000','DB5025','40-50-115','40-50-114','40-50-117','40-50-110','40-50-136','40-50-121','40-50-108','40-50-113','40-50-111','40-50-105','40-50-106');
$id = array();
foreach ($skus as $sku) {
$id[] = wc_get_product_id_by_sku( $sku );
}
if(in_array($product->id, $id) && $is_purchasable) return false;
else return true;
}
// Remove Price, swap with text
add_filter('woocommerce_get_price_html', 'show_price_logged');
function show_price_logged($price){
global $product;
$skus = array('40-50-134','40-50-132','RB1000','RB1008','RB1009','RP1000','90-15-008','RP1009','RB1010','RB1011','RB1014','RB1016','RP1034','RP1036','40-50-125','RB1013','DB5018','DB5016','DB5006','XR5048','RG1000','DB5025','40-50-115','40-50-114','40-50-117','40-50-110','40-50-136','40-50-121','40-50-108','40-50-113','40-50-111','40-50-105','40-50-106');
$id = array();
foreach ($skus as $sku) {
$id[] = wc_get_product_id_by_sku( $sku );
}
if(in_array($product->id, $id)){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
$price = 'Call For Pricing';
return $price;
}
else
{
return $price;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment