Created
August 11, 2016 08:16
-
-
Save anonymous/861d9491df72e47292ce7b4222a97ec1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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