简体   繁体   中英

Add a percent fee to WooCommerce per product, based on category

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!

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

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 );


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM