Sure, I’m happy to share my experience and solution.
As I said earlier, because only some products are actually sold online, I needed to figure out a way to remove the quantity and add to cart button from a subset of products.
The products are all categorized, and each category has sub-categories. So I needed to check all single product categories to see if its sub-categories are children of categories that are not sold online.
I searched and searched for solutions based on sub-categories, but all of those solutions required manually creating an array of sub-categories and I needed a solution that would query the db for all current sub-categories because additional sub-categories would be created as time went on.
This is what I came up with:
add_action('wp', 'niba_remove_add_to_cart_from_category' );
// allow querying of the WP database
global $wpdb;
// Find all children of Rug category parents 19 & 20 (collection & style)
$catproducts = $wpdb->get_results("
SELECT t.slug, tt.term_id
FROM wp_terms AS t
INNER JOIN wp_term_taxonomy AS tt
ON t.term_id = tt.term_id
WHERE tt.parent IN (19)
OR tt.parent IN (20)
", OBJECT );
// create an array of category slugs of the children
$catnames = array();
foreach( $catproducts as $catproduct ) :
$catnames[] = $catproduct->slug;
endforeach;
function niba_remove_add_to_cart_from_category() {
// only do this if this is a product page
if ( is_product() ) {
// make the current product and the array of children available to this
// function
global $catnames;
global $product;
// Get the categories of the current product
$terms = get_the_terms( get_the_ID(), 'product_cat' );
// if the product has categories create an array of those categories
if ( $terms && ! is_wp_error( $terms ) ) :
// make array of the product's categories
$prodcats = array();
foreach ( $terms as $term ) {
// make the array from the slugs
$prodcats[] = $term->slug;
}
endif;
// search for a match in the two arrays: $prodcats and $catnames
// to determine if this is a rug collection or style
$arr_common = array_intersect( $catnames, $prodcats );
// check if $arr_common has any values
// if it does then don't show the quantities or add to cart button and make this product umpurchasable
if ( $arr_common ) {
add_filter( 'woocommerce_is_purchasable', '__return_false');
add_filter( 'body_class', function( $classes ) {
return array_merge( $classes, array( 'hide-add-to-cart ' ) );
} );
}
}
}
Here’s my associated css:
body.hide-add-to-cart .woocommerce-variation-add-to-cart.variations_button { display:none !important; }
Any suggestions for how this could be improved would be most welcome. I muddled through as best I could. Anyway, this code accomplishes what I needed.
I hope this helps others seeking a similar solution.