So, I have a store with a category that uses the type of “virtual” product to avoid delivery. However, there is a service charge that must be added to each order that includes a product in this category.
Through various searches, I found different ways to globally add a fee or add a fee based on quantity, and tried to use it to target through a category, but it doesn't seem to be that way. Any help would be appreciated.
function woo_add_cart_fee() {
global $woocommerce;
$cart = $woocommerce->cart->get_cart();
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' );
Well, since this was my first post, I can’t answer my question for a while. I finally got this to work with the following snippet, but feel free to improve if you find problems:
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 Charge', $excost, $taxable = false, $tax_class = '');
}
}
add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );
, , .