Add product specific fee, not cart-wide fee
-
I need to give flat fees to specific products so that they show up on the line item totals. It appears that I can do this through use of a hook (woocommerce_before_calculate_totals or woocommerce_after_calculate_totals) and by altering the cart items in the WC_Cart object. However after I do this, any changes I make to line_subtotal and line_total are wiped away by the time everything is spit out to the cart.
Is there a way to do this? Here is the code I’m using:
add_action( 'woocommerce_before_calculate_totals', 'calculate_product_totals_with_hidden_tm_epo_fees' ); function calculate_product_totals_with_hidden_tm_epo_fees( $cart_object ){ $cart_contents = $cart_object->get_cart_contents(); foreach ( $cart_contents as $cart_content ) { if (sizeof($cart_content['tmcartfee']) > 0){ $cart_content['tmcartfee_hidden'] = $cart_content['tmcartfee']; $cart_content['tmcartfee'] = array(); $cart_content['tmdata']['tmcartfee_data'] = array(); //Fees are hidden and overridden. Now you calculate everything in: // 'woocommerce_before_calculate_totals' foreach ( $cart_content['tmcartfee_hidden'] as $fee ) { $cart_content['line_subtotal'] += $fee['price']; $cart_content['line_total'] += $fee['price']; } } } $cart_object->set_cart_contents($cart_contents); }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Add product specific fee, not cart-wide fee’ is closed to new replies.