The following code is from a wordpress + woocommerce installation. It's supposed to check every item in the cart and if the item belongs to specific product categories (category ids 79 & 130 add 15€ charge, category 80, 136, 139 add 50€ charge) add extra charges accordingly to category. For simplicity I'll call them category group 15€ and category group 50€.
When one item is added to the cart, belonging to either category group 15€ or category group 50€, the additional charge is added as expected.
When two items are added to the cart, belonging to different category groups, the additional charges are added as expected.
But if more than one items are added to the cart, belonging to the same category group, only one charge per category group is added, which is not desirable. Every item added should incur the relevant category group charge. E.g. 2 items of category group 15€ should add 2 additional charges, 3 items of category group 50€ should add 3 additional charges and so on.
Is there a bug in the code or... ?
function woo_add_cart_fee() {
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) {
// 79 is the ID of the category for which we want to remove the payment gateway
if($term->term_id == '79' || $term->term_id == '130'){
$excost = 15;
$woocommerce->cart->add_fee('Εκτύπωση με απλό μελάνι', $excost, $taxable = false, $tax_class = '');
} else if ($term->term_id == '80' || $term->term_id == '139' || $term->term_id == '136'){
$excost = 50;
$woocommerce->cart->add_fee('Μακέτα & εκτύπωση (απλό μελάνι)', $excost, $taxable = false, $tax_class = '');
}
}
}
}