0
votes

I am new here, although i've been using the website for months to gather some help from others questions.

I am trying to add a fee in woocommerce in the next way:

The site has to check the cart, find the products that meet a category id, calculate a percent fee of each of those products, sum them and charge it.

So far i have managed to check the cart, find the products that meet a category (getting the amount of them) and multiply the ammount for a fixed fee.

As you can see, if i have 3 products that meet the criteria (category id) at £50, £40 and £30, the fee will be 3*10 (the fixed fee is 10).

What i really want is that the site calculate the percent fee from those products (50*10, 40*10, 30*10) and sum them up (500+400+300). Obviously not all products will have the same price.

Here is my code:

function df_add_handling_fee( $cart_object ) {

global $woocommerce;
$specialfeecat = 61; // category id for the special fee
$spfee = 0.00; // initialize special fee
$spfeeperprod = 10; //special fee per product

//Getting Cart Contents. 
$cart = $woocommerce->cart->get_cart();
//Calculating Quantity
foreach($cart as $cart_val => $cid){
$qty += $cid['quantity']; 
}
foreach ( $cart_object->cart_contents as $key => $value ) {

$proid = $value['product_id']; //get the product id from cart
$quantiy = $value['quantity']; //get quantity from cart
$itmprice = $value['data']->price; //get product price

$terms = get_the_terms( $proid, 'product_cat' ); //get taxonomy of the products
if ( $terms && ! is_wp_error( $terms ) ) :
    foreach ( $terms as $term ) {
        $catid = $term->term_id;
        if($specialfeecat == $catid ) {
            $spfee = $spfee + $quantiy * $spfeeperprod;
        }
    }
endif;  
}

if($spfee > 0 ) {

$woocommerce->cart->add_fee( 'Handling Fee', $spfee, true, 'standard' );
}

}

add_action( 'woocommerce_cart_calculate_fees', 'df_add_handling_fee' );

Thanks!

2
You mean you want to calculate the percent of the fee apply to each product or for the total cart (all products sum = total cart)? Is not very clear…LoicTheAztec
I want the cart charge a fee for customizing some products. So every customizable product is in the category id 61. When you order 3 products lets say (£50, £40 and £30), two of them (£50 and £40) are in the category id 61 (customizable). So what i want is the cart to charge a 10% extra on those. So it will calculate the 10% of each, then sum them, and charge it as a fee. So the end basket of those three products would be: Product 1 - £50 Product 2 - £40 Product 3 - £30 ---------------------- Subtotal - £120 Fee - £9 (10% of 50 + 10% of 40) TOTAL - £129 ThanksAlberto Balbuena

2 Answers

0
votes

Nicely presented !

This plugin may solve the problem easy, it allows you to charge extra fees in cart, based on the combination of multiple conditional rules that you configure in your woocommerce store.

I hope it may help!

WooCommerce Conditional Product Fees For Checkout

0
votes

Hopefully this will help:

Product A - Category1 /// Product B - Category2 /// Product C - Category3 //// Product D - Category1

My shopping cart = 2 of product A, 3 of product B, 1 of product C, and 4 of product D

I want to add a fee that calculates that I have a quantity of 7 items from categories 1 & 2 and adds a fixed price amount of $5 per product....

I found the code below online it might be easier... Unfortunately I have bought several plugins but they don't do what I am needing.

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 OF ALL ITEMS IN PREVIOUSLY SPECIFIED CATEBORIES
    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 );


}