• I’m trying to write a filter or hook that will modify the total amount of the shopping cart depending on the total overall amount of items in the shopping cart. For example, if there are three items in the shopping cart, I want to deduct twenty dollars from the total order. Below is the code I have so far, any help is greatly appreciated!

    add_filter('woocommerce_cart_contents_total', 'bundle_deals');
    
    function bundle_deals( $cart_contents_total, $cart_contents_count) {
    global $woocommerce;
    if ($woocommerce->cart->get_cart()->cart_contents_count <= 3) {
    $cart_contents_total = $woocommerce->cart->get_cart()->cart_contents_total - 20.00;
    }
    
    return $cart_contents_total;
    }

    https://www.ads-software.com/plugins/woocommerce/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Russ Powers

    (@russwebguy)

    I’ve tried this plugin before but can’t get it to work the way I need it to. I have to deduct 20 from the cart if there are 3 of any item with any combination of variations. It can’t be deducted separately from each product. but then when a fourth item is added, 30 is deducted from the total. No more should be deducted until 6 items are in the cart, then 40 is deducted. when 7 items are in the cart, 50 is deducted. 8 items, 60. etc. etc.

    I can easily program this logic, it’s just calling and returning the cart total I can’t get a handle on..

    The ‘woocommerce_cart_contents_total’ filter is called from a mini cart in the header or maybe from some widgets, but not on the cart page, so in your case code flow does not reach your function. Put “print ‘HERE’;” in your function to test this opinion.

    I think the filter to use is ‘woocommerce_cart_total’.

    Your comparison in the if statement should be > 2, not <= 3

    The object to get cart_contents_count should not include get_cart(), ie:

    $woocommerce->cart->cart_contents_count

    The passed parameter $cart_contents_total is a string which includes the currency symbol, so you you would need to temporarily remove the currency symbol from it before doing the maths, then put it back afterwards.

    If it doesn’t work, use var_dump($variable); after each line one at a time to check that the $variable contains what you think it should.

    Best of luck!

    Thread Starter Russ Powers

    (@russwebguy)

    I found a solution by programmatically adding and removing coupons based on the total number of items in the cart

    Below is a snippet of the code I used in case anyone would like to try this out

    add_action('woocommerce_before_cart_table', 'bundle_deal');
    function bundle_deal( ) {
        global $woocommerce;
    	$totalItems = WC()->cart->cart_contents_count;
    	if ( $totalItems < 3 ) {
    	WC()->cart->remove_coupon( '4 or more item bundle deal' );
    	WC()->cart->remove_coupon( '3 item bundle deal' );
    
    	}
        if( $totalItems > 2 && $totalItems < 4 ) {
       	 WC()->cart->remove_coupon( '4 or more item bundle deal' );
            $coupon_code = '3 item bundle deal';
            if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ))) {
    
            }
            echo '<div class="woocommerce_message"><strong>Bundle Deal Applied!</strong></div>';
        }
        if( $totalItems > 3 ) {
        	WC()->cart->remove_coupon( '3 item bundle deal' );
            $coupon_code = '4 or more item bundle deal';
            if (!$woocommerce->cart->add_discount( sanitize_text_field( $coupon_code ))) {
    
            }
            echo '<div class="woocommerce_message"><strong>Bundle Deal Applied!</strong></div>';
        }
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Modifying Order Total or Subtotal Depending on Number of Items in Cart’ is closed to new replies.