Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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