Add a fee to WooCommerce based on multiple product categories

I found this great snippet from another post on Stackoverflow, to add a custom fee to the WooCommerce cart based on a category but am having trouble modifying it to:

1) Apply a registration fee to multiple categories
2) Calculate the registration fee based on multiple products from the same category in the cart.
3) Calculate the registration fee based on multiple products with a registration fee from difeerent categories.

Read More

Any help and/or suggestions would be greatly appreciated.

*Code modified slightly to stay relevant to the question I’m asking.

function my_reg_fee( $cart_object ) {

global $woocommerce;
$specialfeecat = 60; // category id for the special fee
$spfee = 85; // initialize special fee

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 taxonamy of the prducts
    if ( $terms && ! is_wp_error( $terms ) ) :
        foreach ( $terms as $term ) {
            $catid = $term->term_id;
            if($specialfeecat == $catid ) {
                $spfee = $spfee + $itmprice * $quantiy;
            }
        }
    endif;  
}

if($spfee > 0 ) {

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

}

add_action( 'woocommerce_cart_calculate_fees', 'my_reg_fee' );

Related posts

Leave a Reply