I tried for a while to get this working, but I did not find a solution that does exactly what we need, and I am far from a PHP expert, so I got a little lost.
We use WooCommerce and WooTickets. The goal is to add a 5% service charge fee for products in the Tickets category only (ID: 34).
We found this code joke that adds a fixed cost based on product category:
function woo_add_cart_fee() {
$category_ID = '23';
global $woocommerce;
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
$terms = get_the_terms( $values['product_id'], 'product_cat' );
foreach ($terms as $term) {
if($term->term_id == $category_ID){
$excost = 6;
}
}
$woocommerce->cart->add_fee('Service Fee', $excost, $taxable = false, $tax_class = '');
}
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
The main problem with this solution is that it adds a fixed cost, while we need an interest value.
We also found this piece of code from WooThemes themselves:
add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
function woocommerce_custom_surcharge() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = 0.05;
$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
$woocommerce->cart->add_fee( 'Service Fee', $surcharge, true, 'standard' );
}
But again, there are several problems with this solution ...
1)
2) , , 5% - "", .