• Resolved chrispytoes

    (@chrispytoes)


    I currently have this code below to add the discount tax back onto the cart as a fee. My client for this site wants coupons applied after tax because of vendor restrictions on sales.

    
    function lws_apply_coupon_price($cart) {
    	$discount_tax = $cart->get_discount_tax();
    
    	if(count($cart->get_applied_coupons()) > 0) {
    		$cart->add_fee('Discount Tax', (float)$discount_tax, false);
    	}
    }
    add_action('woocommerce_cart_calculate_fees', 'lws_apply_coupon_price');
    

    However, this does not work when the coupon was applied from the admin panel. What is the hook to add fees to an order after saving from the admin page?

    And how do I get the user’s cart object from only the order ID? Not MY cart object but the cart object of the user that placed the order.

    • This topic was modified 4 years, 3 months ago by chrispytoes.
Viewing 1 replies (of 1 total)
  • Thread Starter chrispytoes

    (@chrispytoes)

    I solved it with this:

    
    	function lws_apply_coupon_price_admin($order_id) {
    		$order = wc_get_order($order_id);
    		$codes = $order->get_coupon_codes();
    
    		if(count($codes) > 0) {
    			$fees = $order->get_fees();
    			$has_discount_tax = false;
    
    			foreach($fees as $fee) {
    				if($fee->get_name() === 'Discount Tax') {
    					$has_discount_tax = true;
    					break;
    				}
    			}
    
    			if(!$has_discount_tax) {
    				$amount = (float)$order->get_discount_tax();
    
    				$fee = new WC_Order_Item_Fee();
    				$fee->set_name('Discount Tax');
    				$fee->set_amount($amount);
    				$fee->set_tax_class('');
    				$fee->set_tax_status('none');
    				$fee->set_total($amount);
    
    				$order->add_item($fee);
    				$order->calculate_totals();
    				$order->save();
    			}
    		}
    	}
    	add_action('woocommerce_saved_order_items', 'lws_apply_coupon_price_admin');
    
Viewing 1 replies (of 1 total)
  • The topic ‘Add fees to order from the admin save hook’ is closed to new replies.