Add a fee for a specific product category in Woocommerce

后端 未结 2 1908
醉话见心
醉话见心 2020-12-21 23:55

I already found this code here and it works for like 80/90% for me (see below).

This code adds 60 euro to my cart when there is a product from category ID 349 in the

相关标签:
2条回答
  • 2020-12-22 00:04

    I found a solution like the following. Note that I kept adding a fee, because I started from a previous snippet. You can apply an existing discount or calculate a different total according to your needs. In this case, I apply a negative fee - like a discount - according to a >= 300 total value of the cart, whose values would be -25% only for local_pickup shipping method, calculating taxes in addition.

    function discount_to_cat(){
    // thanks to LoicTheAztec's snippet
    $cat_in_cart = false;
    // Loop through all products in the Cart        
    foreach ( WC()->cart->get_cart() as $cart_item ) {
    
        if ( has_term( 'category_to_pick', 'product_cat', $cart_item['product_id'] ) ) {
            $cat_in_cart = true;
            $tot_category_price = $cart_item['data']->get_price();
            $tot_category_qty = $cart_item['quantity'];
            $tot_category =  $tot_category_price * $tot_category_qty;
            break;
        }
    }   global $product;
        $total = WC()->cart->subtotal;
        $discount_label = "";
        if($total >= 300){
            $discount_label=15;     
    }
        $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
        $chosen_shipping = explode(':',$chosen_methods[0]);
    
        if($chosen_shipping[0]=='local_pickup'){
            $discount_label=25;
        }
        $discount_applied = ($total-$tot_category)*$discount_label/100;
        if($discount_label!=""){
            $discount_applied_net = ($discount_applied/1.1); //1.1 according taxes for shipping
            WC()->cart->add_fee( "Discount applied: ($discount_label%)", -$discount_applied_net, false );
        }   
    }
    

    add_action( 'woocommerce_cart_calculate_fees','discount_to_cat' );

    0 讨论(0)
  • 2020-12-22 00:05

    The code you are using is a bit outdated and you should use has_term() Wordpress conditional function to target a product category this way:

    add_action( 'woocommerce_cart_calculate_fees','custom_pcat_fee', 20, 1 );
    function custom_pcat_fee( $cart ) {
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        // Set HERE your categories (can be term IDs, slugs or names) in a coma separated array
        $categories = array('349');
        $fee_amount = 0;
    
        // Loop through cart items
        foreach( $cart->get_cart() as $cart_item ){
            if( has_term( $categories, 'product_cat', $cart_item['product_id']) )
                $fee_amount = 60;
        }
    
        // Adding the fee
        if ( $fee_amount > 0 ){
            // Last argument is related to enable tax (true or false)
            WC()->cart->add_fee( __( "Extra bezorgkosten kunstgras", "woocommerce" ), $fee_amount, false );
        }
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    A fee of 60 will always be added if there is in cart an item from 349 product category ID.

    0 讨论(0)
提交回复
热议问题