Skip to content

Instantly share code, notes, and snippets.

@tobychung
Last active December 1, 2020 23:12
Show Gist options
  • Save tobychung/c2f5bb4525917cea7b172610cb7da4e1 to your computer and use it in GitHub Desktop.
Save tobychung/c2f5bb4525917cea7b172610cb7da4e1 to your computer and use it in GitHub Desktop.
<?php
public function match( $match, $operator, $value ) {
$value = $this->get_value( $value );
if ( '==' == $operator ) :
// Promotion: If cart match the category and greater than two then shipping free
$count = 0;
foreach ( WC()->cart->get_cart() as $product ) :
if ( has_term( $value, 'product_cat', $product['product_id'] )) :
$count = $count + $product['quantity'];
return ($count >= 2) ? true : false;
endif;
endforeach;
// Backup: If any item in cart matched the category then shipping free
// foreach ( WC()->cart->get_cart() as $product ) :
// if ( has_term( $value, 'product_cat', $product['product_id'] ) ) :
// return true;
// endif;
// endforeach;
elseif ( '!=' == $operator ) :
$match = true;
foreach ( WC()->cart->get_cart() as $product ) :
if ( has_term( $value, 'product_cat', $product['product_id'] ) ) :
return false;
endif;
endforeach;
endif;
return $match;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment