Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plugin-republic/d59e0cc773e1a6045944a1cb3dba0a4d to your computer and use it in GitHub Desktop.
Save plugin-republic/d59e0cc773e1a6045944a1cb3dba0a4d to your computer and use it in GitHub Desktop.
Exclude all products within a category from global role-based rules
<?php
// Exclude all products within a category from global role-based rules
function wcfad_exclude_categories_from_role_based_rules( $excluded ) {
// Exclude all products in a category
$excluded = wc_get_products(
array(
'category' => array( 'fruit', 'vegetables' ),
'return' => 'ids'
)
);
// Include variations
if( $excluded ) {
foreach( $excluded as $excluded_id ) {
if( WC_Product_Factory::get_product_type( $excluded_id ) == 'variable' ) {
$product = wc_get_product( $excluded_id );
$children = $product->get_children();
$excluded = array_merge( $excluded, $children );
}
}
}
return array_values( $excluded );
}
add_filter( 'wcfad_exclude_from_role_based_rules', 'wcfad_exclude_categories_from_role_based_rules' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment