Skip to content

Instantly share code, notes, and snippets.

@JeroenSormani
Last active August 29, 2015 14:21
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 JeroenSormani/777fbfa3a4f4ccf4a0dd to your computer and use it in GitHub Desktop.
Save JeroenSormani/777fbfa3a4f4ccf4a0dd to your computer and use it in GitHub Desktop.
Custom WAS subtotal match. Exclude virtual product amount in subtotal.
<?php
remove_all_filters( 'was_match_condition_subtotal' );
add_filter( 'was_match_condition_subtotal', 'new_was_match_condition_subtotal', 10, 3 );
/**
* Custom subtotal matching.
*
* Match the subtotal amount without virtual products.
*
* @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_was_match_condition_subtotal( $match, $operator, $value ) {
if ( ! isset( WC()->cart ) ) return;
$subtotal = 0;
foreach( WC()->cart->get_cart() as $item ) :
if ( 'no' == $item['data']->virtual ) :
$subtotal += $item['line_total'];
endif;
endforeach;
if ( '==' == $operator ) :
$match = ( $subtotal == $value );
elseif ( '!=' == $operator ) :
$match = ( $subtotal != $value );
elseif ( '>=' == $operator ) :
$match = ( $subtotal >= $value );
elseif ( '<=' == $operator ) :
$match = ( $subtotal <= $value );
endif;
return $match;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment