0

I'm not a lot familiar with code, but I have to manage an e-commerce for friends. They ask me to add a surcharge in the cart, for all the products except for 1-2 categories. I found this code that is doing the job, but this add the surcharge to a specific category (86), and I want to add the surcharge to all the categories except the 86. How can I do this? Tried with !=86 and <> but no luck... (I'm sorry, I suppose it's an easy question...but I'm not familiar with code...) thanks for any help

This is the code:

function df_add_ticket_surcharge( $cart_object ) {

    global $woocommerce;
    $specialfeecat = 86; // category id for the special fee
    $spfee = 0.00; // initialize special fee
    $spfeeperprod = 0.05; //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( 'Ticket Concierge Charge', $spfee, true, 'standard' );
    }

}

add_action( 'woocommerce_cart_calculate_fees', 'df_add_ticket_surcharge' );

Fab
  • 11
  • 4
  • Replace `if($specialfeecat == $catid )` with `if($specialfeecat != $catid )` – 7uc1f3r May 16 '20 at 09:23
  • there is a way to exclude more than one category? Because if I write: $specialfeecat = 86, 83; it doesn't work... – Fab May 18 '20 at 12:57
  • Sure, you should use an array instead of a string with multiple IDs, [this answer](https://stackoverflow.com/a/43697812/11987538) actually contains all the code that is relevant to adapt your code to this – 7uc1f3r May 18 '20 at 17:24
  • If you can not figure it out, then adjust your question with what you have tried and give a call ! Regards – 7uc1f3r May 18 '20 at 17:27
  • thanks! I'll check. :-) – Fab May 19 '20 at 14:23
  • It seems with your new question that this one can be removed? Also, don't forget to [accept the answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) I gave to your other question. Regards – 7uc1f3r May 20 '20 at 10:44
  • Accepted. Maybe this one could be useful for someone else! thanks. – Fab May 20 '20 at 12:20

0 Answers0