Skip to content

Instantly share code, notes, and snippets.

Created August 11, 2016 08:16
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 anonymous/861d9491df72e47292ce7b4222a97ec1 to your computer and use it in GitHub Desktop.
Save anonymous/861d9491df72e47292ce7b4222a97ec1 to your computer and use it in GitHub Desktop.
<?php
// You can copy&paste the contents of the script to your (child) themes functions.php. I recommend doing it over FTP.
remove_all_filters( 'wafs_match_condition_category' );
add_filter( 'wafs_match_condition_category', 'new_wafs_match_condition_category', 10, 3 );
/**
* Stock status.
*
* Match the stock status to one or more products in the cart.
*
* @global object $woocommerce WooCommerce object.
*
* @param bool $match Current match value.
* @param string $operator Operator selected by the user in the condition row.
* @param mixed $value Value given by the user in the condition row.
* @return BOOL Matching result, TRUE if results match, otherwise FALSE.
*/
function new_wafs_match_condition_category( $match, $operator, $value ) {
if ( ! isset( WC()->cart ) ) return;
$term = get_term_by( 'slug', $value, 'product_cat' );
$term_childs = get_term_children( $term->term_id, 'product_cat' );
$term_slugs = array( $term->slug );
foreach ( $term_childs as $child ) :
$term = get_term_by( 'id', $child, 'product_cat' );
$term_slugs[] = $term->slug;
endforeach;
if ( '==' == $operator ) :
foreach ( WC()->cart->cart_contents as $product ) :
// Get product terms
$product_terms = get_the_terms( $product['product_id'], 'product_cat' );
$product_terms = wp_list_pluck( $product_terms, 'slug' );
if ( count( array_intersect( $product_terms, $term_slugs ) ) >= 1 ) :
$match = true;
endif;
endforeach;
elseif ( '!=' == $operator ) :
$match = true;
foreach ( WC()->cart->cart_contents as $product ) :
// Get product terms
$product_terms = get_the_terms( $product['product_id'], 'product_cat' );
$product_terms = wp_list_pluck( $product_terms, 'slug' );
if ( count( array_intersect( $product_terms, $term_slugs ) ) >= 1 ) :
$match = true;
endif;
endforeach;
endif;
return $match;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment