2

I was looking for a way to add a deposit on the TOTAL Cart Amount on a Woocommerce site (rather than just adding a deposit to each product line item).

I found the answer to this ingenious thread here: Deposit based on a percentage of total cart amount

Here's the code I ended up using:

add_action( 'woocommerce_cart_calculate_fees', 'booking_deposit_calculation' ); 
function booking_deposit_calculation( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    ## Set HERE your negative percentage (to remove an amount from cart total)
    $percent = -.50; // 50% off (negative)

    // Get cart subtotal excluding taxes
    $cart_subtotal = $cart_object->subtotal_ex_tax;
    // or for subtotal including taxes use instead:
    // $cart_subtotal = $cart_object->subtotal;

    ## ## CALCULATION ## ##
    $calculated_amount = $cart_subtotal * $percent;

    // Adding a negative fee to cart amount (excluding taxes)
    $cart_object->add_fee( __('Deposit calculation', 'woocommerce'), $calculated_amount, false );

}

This creates a default of 50% deposit for every product on the Cart and Checkout page. Brilliant! (Using CSS, I was then able to style the descriptions on the front-end.)

However, I have a few products (one product category) on which I don't want to force this deposit.

So, here's my question:

How can I tweak the code to continue to enforce a default deposit but exclude the deposit from one product category (or the products in this category if I cannot exclude an entire category)?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
CraigK
  • 43
  • 5

1 Answers1

0

In the hooked function below, you will have to set an array of products Ids or (and) product categories, to exclude them. If you don't use one of them you can set a blank array like for example $product_categories = array();

Here is the code:

add_action( 'woocommerce_cart_calculate_fees', 'custom_deposit_calculation', 10, 1 );
function custom_deposit_calculation( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Define the product IDs to exclude
    $product_ids = array( 37, 25, 50 );
    // Define the product categories to exclude (can be IDs, slugs or names)
    $product_categories = array( 'clothing' );
    $amount_to_exclude_with_tax = 0;

    // Iterating through cart items
    foreach ( $cart_object->get_cart() as $cart_item ){
        // If condition match we get the sum of the line item total (excl. tax) 
        if( in_array( $cart_item['product_id'], $product_ids ) || has_term( $product_categories, 'product_cat', $cart_item['product_id'] ) )
            $amount_to_exclude_with_tax += $cart_item['line_total'];
            // OR replace by (for tax inclusion)
            // $amount_to_exclude_with_tax += $cart_item['line_tax'] + $cart_item['line_total'];
    }

    ## Set HERE your negative percentage (to remove an amount from cart total)
    $percent = -0.5; // 50% off (negative)

    // Get cart subtotal excluding taxes
    $cart_subtotal = $cart_object->subtotal_ex_tax - $amount_to_exclude_with_tax;
    // or for subtotal including taxes use instead:
    // $cart_subtotal = $cart_object->subtotal;

    ## ## CALCULATION ## ##
    $calculated_amount = $cart_subtotal * $percent;

    if( $calculated_amount != 0){
        // Adding a negative fee to cart amount (Including taxes)
        $cart_object->add_fee( __('Deposit calculation', 'woocommerce'), $calculated_amount, true );
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested on WooCommerce 3 and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399