1

My code is working well but for one cat only (here ID 25).
Have difficulty to add a second category (ID 24).

This the code I am using for category ID 25:

function df_add_ticket_surcharge( $cart_object ) {

    global $woocommerce;
    $specialfeecat = 25; // category id for the special fee
    $spfee = 2; // initialize special fee
    $spfeeperprod = 0.0; //special fee per product

    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 * $spfeeperprod;
                }
            }
        endif;  
    }

    if($spfee > 0 ) {

        $woocommerce->cart->add_fee( 'Supp. préparation fruit légumes', $spfee, true, 'standard' );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'df_add_ticket_surcharge' );

How can I do to handle 2 categories in this code?

Thanks

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
johanna75
  • 19
  • 2

0 Answers0