5

I have a store with a category that uses the "virtual" product type in order to avoid shipping. However, there is a service fee that needs to be added to each order that includes a product of that category.

Through various searches I have found different ways to globally add a fee, or to add a fee based on quantity, and have tried to use that to target via category, but can't seem to get it right.

function woo_add_cart_fee() {
global $woocommerce;
//Getting Cart Contents. 
$cart = $woocommerce->cart->get_cart();
//find product category
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
    $_product = $values['data'];
        $terms = get_the_terms( $_product->id, 'product_cat' );
    }
  if ($_categoryid == 23){
      $excost = 6;
  } 
  elseif ($_categoryid == 14){
      $excost = 0;
  }     

$woocommerce->cart->add_fee('Service Charge', $excost, $taxable = false, $tax_class = '');
}   
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
double-beep
  • 5,031
  • 17
  • 33
  • 41
dothedew
  • 61
  • 5
  • Check the following question http://stackoverflow.com/questions/26240591/add-a-fee-to-woocommerce-per-product-based-on-category – Joko Wandiro Jan 28 '15 at 09:20

1 Answers1

0

Original answer (In 2014)

©dothedew (user edited the question and answered in a question at that time)

<?php
// Add Service Fee to Category
function woo_add_cart_fee() {

    $category_ID = '23';
    global $woocommerce;

    foreach ($woocommerce->cart->cart_contents as $key => $values ) {
        // Get the terms, i.e. category list using the ID of the product
        $terms = get_the_terms( $values['product_id'], 'product_cat' );
        // Because a product can have multiple categories, we need to iterate through the list of the products category for a match
        foreach ($terms as $term) {
            // 23 is the ID of the category for which we want to remove the payment gateway
            if($term->term_id == $category_ID){
                $excost = 6;
            }
        }
        $woocommerce->cart->add_fee('Service Charge', $excost, $taxable = false, $tax_class = '');
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

My answer (In 2023)

I've made some update in code using WooCommerce 3.x syntax:

  • Instead of using $woocommerce, which is a global variable, I'm using WC() to get the WooCommerce instance. This is a more modern way of accessing the cart and other objects in WooCommerce.
  • I've changed the way we loop through the cart items using WC()->cart->get_cart() instead of $woocommerce->cart->cart_contents.
  • I'm using the $category_slug variable instead of $category_ID. This is a more user-friendly way of identifying a category and is less likely to change than the ID.
  • I've move add_fee outside the loop

Answer :

<?php
/**
 * Add service fee to a specific category
 */
function woo_add_cart_fee() {
    // Define the category slug and service fee amount
    $category_slug      = 'category-slug';

    $product_count = 0;

    // Loop through each item in the cart
    foreach ( WC()->cart->get_cart() as $cart_item ) {
        // Get the product ID and its categories
        $product_id          = $cart_item['product_id'];
        $product_categories  = get_the_terms( $product_id, 'product_cat' );

        // Check if the product belongs to the specified category
        if ( ! empty( $product_categories ) ) {
            foreach ( $product_categories as $category ) {
                if ( $category_slug === $category->slug ) {
                    $product_count++;
                }
            }
        }
    }

    if ( $product_count > 0 ) {
        $service_fee = 6 * $product_count;
        // Add the service fee to the cart
        WC()->cart->add_fee( __( 'Service Charge', 'woocommerce' ), $service_fee, false );
    }
}
// Add the fee calculation function to the cart
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
Yash
  • 1,020
  • 1
  • 15