2

I would like to add a fee to certain categories if the total number of products from all the defined category is 1 or separate fee if the quantity is 2-9.. (IE. + $10 to the product if they only order 1, +$5 per product if they order 2-9 items).

I started on this base: Custom Fee based on product dimensions and categories

I have maid some changes, but I can't get it working and I am stuck.

Here is my code:

add_action( 'woocommerce_cart_calculate_fees','custom_applied_fee');
function custom_applied_fee() {

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

    // Set HERE your categories (can be an ID, a slug or the name… or an array of this)
    $category1 = 'plain';
    $category2 = 'plywood';

    // variables initialisation
    $fee = 0;

    // Iterating through each cart item
    foreach(WC()->cart->get_cart() as $cart_item){
        // Get the product object
        $product = new WC_Product( $cart_item['product_id'] );
        $quantiy = $value['quantity']; //get quantity from cart

        // Initialising variables (in the loop)
        $cat1 = false; $cat2 = false;

        // ## CALCULATIONS ## (Make here your conditional calculations)
        $quantity = GET TOTAL QUANTITY
        if($quantity <= 1){
            $fee += 10 * $quanity;
        } elseif($quantity > 1 && $dimention <= 9){
            $fee += 5 * $quanity;
        } elseif($dimention > 10){
            $fee += 1 * $quanity;
        }
    }

    // Adding the fee
    if ( $fee != 0 )
        WC()->cart->add_fee( $fee_text, $fee, true );
}

What I am doing wrong? How can I get this working?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
SHON WILLIAMS
  • 29
  • 1
  • 6

2 Answers2

2

Updated 2

Your needs are not so clear, so I have tried to do my best. I have understood that you want to add:

  • An fixed initial fee when there is 1 cart item from defined product category(ies).
  • A calculated fee based on total cart item count when there is (from 2 to 9) cart items from defined product category(ies).

Here is the corresponding code for that:

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

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

    // Set HERE your categories (can be an ID, a slug or the name… or an array of this)
    $product_categories = array('clothing', 'disc');
    $cat_qty = 0;
    $cart_total_qty = $cart_object->get_cart_contents_count();

    // Iterating through each cart item
    foreach( $cart_object->get_cart() as $cart_item ){
        $product = $cart_item['data']; // The product object
        $item_qty = $cart_item['quantity']; // Item quantity

        // Counting quanties for the product categories
        if( has_term( $product_categories, 'product_cat', $cart_item['data']->get_id() ) )
            $cat_qty += $cart_item['quantity'];
    }

    ##  --  --  --  --  CALCULATIONS AND CONDITIONAL FEES  --  --  --  --  ##

    // 1. A fee for the product defined categories for 1 cart item only
    if($cat_qty == 1) // updated HERE
    {
        $fee_amount = 10;
        $cart_object->add_fee( __( "Fee (1)" ), $fee_amount, true );
    }

    // 2. Or a calculated fee for the defined product categories (qty from 2 to 9)
    if( $cat_qty >= 2 && $cat_qty <= 9 )
    {
        $amount_by_item = 5; // amount by item (updated)
        $calculated_fee = $amount_by_item * $cat_qty;  // calculation (updated)
        $cart_object->add_fee( __( "Fee (2 to 9)" ), $calculated_fee, true );
    }
}

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

This code is tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • @SHONWILLIAMS Updated just as you suggested. But just replaced `if($cat_qty = 1)` by `if($cat_qty == 1)`… Sorry for this misunderstanding. – LoicTheAztec Sep 07 '17 at 18:41
  • @SHONWILLIAMS Sorry again for the misunderstanding, not easy to understand when native language is different and people try to write in english the best as they can. (If you can remove some of the comments that are not necessary anymore, it could be nice) Thanks. – LoicTheAztec Sep 07 '17 at 19:21
1

Thanks LoicTheAztec I think with a few minor edits your code will work...

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

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

// Set HERE your categories (can be an ID, a slug or the name… or an array of this)
$product_categories = array('clothing', 'disc');
$cat_qty = 0;
$cart_total_qty = $cart_object->get_cart_contents_count();

// Iterating through each cart item
foreach( $cart_object->get_cart() as $cart_item ){
    $product = $cart_item['data']; // The product object
    $item_qty = $cart_item['quantity']; // Item quantity

    // Counting quanties for the product categories
    if( has_term( $product_categories, 'product_cat', $cart_item['data']->get_id() ) )
        $cat_qty += $cart_item['quantity'];
}

##  --  --  --  --  CALCULATIONS AND CONDITIONAL FEES  --  --  --  --  ##

// 1. The 1st fee for the product defined categories (qty = 1)
if($cat_qty = 1)
{
    $fee_amount = 10;
    $cart_object->add_fee( __( "Fee (1)" ), $fee_amount, true );
}

// 2. The Separate  additional fee for the defined product categories (qty from 2 to 9)
if( $cat_qty >= 2 && $cat_qty <= 9 )
{
    $amount_by_item = 5; // amount by item
    $calculated_fee = $cat_qty * $amount_by_item;  // calculation
    $cart_object->add_fee( __( "Fee (2 to 9)" ), $calculated_fee, true );
}

}

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
SHON WILLIAMS
  • 29
  • 1
  • 6