wir
1/5/2015 - 4:14 PM

woocommerce-alter-cart-total.php

<?php
/**
 * Programmatically alter the WooCommerce cart discount total (and apply a new discount) before it's calculated 
 * through the woocommerce_calculate_totals hook.
 * 
 * For example, if you want to apply an arbitrary discount after a certain number of products in the cart, 
 * you can loop through that number and increment it as needed.
 * 
 * In this example, I increment the discount by $8.85 after every 15 products added to the cart.
 * 
 * @return void 
 */
function mysite_box_discount( ) {
  
    global $woocommerce;
 
    /* Grab current total number of products */
    $number_products = $woocommerce->cart->cart_contents_count;
   
    $total_discount = 0;
    $my_increment = 15; // Apply another discount every 15 products
    $discount = 8.85;
    
    if ($number_products >= $my_increment) {
        
      /* Loop through the total number of products */
      foreach ( range(0, $number_cards, $my_increment) as $val ) {
        if ($val != 0) {
      		$total_discount += $discount;
      	}
      }
   
      // Alter the cart discount total
      $woocommerce->cart->discount_total = $total_discount;
    }   
}
add_action('woocommerce_calculate_totals', 'mysite_box_discount');