phpwordpresswoocommercecartfee

Add a Percentage Fee and a Fixed Fee For specific Product type in WooCommerce


I am trying to add TWO fees to all WooCommerce orders when and if the cart has "auction" products in it.

I need to add a "clubbing" fee of 20 for each "auction" product in the cart and an additional 15% commission fee for each auction product.

My problem is: I do not know how calculate these fees since the commission is based on each product's winning price. One can be $100, another $50 - and so forth.

This is what I have tried, which does not seem to work when testing on a localhost with two auction products that each cost $100. The total fee should be $15 + $15 + $20 + $20 = $70 but I get nothing on the checkout page. No fees at all.

This is the code:

add_action( 'woocommerce_cart_calculate_fees', 'commission_and_fees', 10, 1 );
function commission_and_fees( $cart ) {

if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) ) return;
if ( ! is_user_logged_in() ) return;

    // define arguments
    $clubbing_fee = '';
    $percentage_fee_per_item = '';
    
    // false, init
    $is_auction = false;

    // loop through and break if any auction products are found
    foreach ( $cart_object->cart_contents as $item ) {
        
        // when found, break
        if ( $item['data']->is_type( 'auction' ) ) {
            $is_auction = true;
            break;
        }
    }
 
    if ( $is_auction ) {
        
        $percentage_fee_per_item = 15;
        $clubbing_fee = 20;
        $percentage_and_clubbing_fee = $cart_object->cart_contents_total * $percentage_fee_per_item / 100 + $clubbing_fee;
        
        $cart_object->add_fee( "Auction Fees ($percentage_fee_per_item% + Clubbing Fee)", $percentage_and_clubbing_fee, true );
    }
}

Solution

  • There are many mistakes in your code… For example $cart_object is not defined.

    The following will

    The code:

    add_action( 'woocommerce_cart_calculate_fees', 'auctions_commission_and_fees' );
    function auctions_commission_and_fees( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) ) 
            return;
    
        if ( ! ( is_checkout() && ! is_wc_endpoint_url() ) || ! is_user_logged_in() ) 
            return;
    
        $clubbing_fee    = 20; // Fixed (per auction item)
        $commission_rate = 0.15; // Percentage rate: 15%
        $items_qty_count = 0; // Initializing
        $items_subtotal  = 0; // Initializing
    
        // Loop through cart items
        foreach ( $cart->get_cart() as $item ) {
            if ( $item['data']->is_type('auction') ) {
                $items_qty_count += $item['quantity']; // Quantity count
                $items_subtotal  += $item['line_subtotal']; // Subtotal
            }
        }
     
        if ( $items_qty_count > 0 ) {
            // Commission fee (15% on auctions subtotal)
            $cart->add_fee( 
                __('Commission Fee (15%)', 'woocommerce'), 
                $items_subtotal * $commission_rate, 
                true 
            );
    
            // Clubbing fee ($20 per auction item)
            $cart->add_fee( 
                __('Clubbing Fee ($20 per item)', 'woocommerce'), 
                $clubbing_fee * $items_qty_count, 
                true 
            );
        }
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). It should work.