MJJ
MJJ

Reputation: 109

Woocommerce Add Fee Outside of the Cart

I would like to add a fee to a woocommerce order. I have found many instances of this:

$woocommerce->cart->add_fee()

But I need to add a fee to an existing (processing or scheduled) order from within a function.

can i simply call add_fee()

For example, if i wanted to add a fee of $15 called "Option Adjustment" that is not taxable, could i simple do something like add_fee('Option Adjustment', 15, $taxable = false, $tax_class='')

The problem, of course, is that add_fee outside of the cart has no way of telling which order i want to add the fee to.

I have been looking at this: http://woocommerce.wp-a2z.org/oik_api/wc_abstract_orderadd_fee/ which makes me wonder if i can somehow call add_fee from within WC_Abstract_order by using something like:

$order = new WC_Order( $order_id );

But i am not sure what the specifics and syntax would be.

Any help would be very much appreciated!

Upvotes: 2

Views: 1737

Answers (1)

MJJ
MJJ

Reputation: 109

I was able to successfully add a woocommerce fee from my functions.php using the following. Specifically, I call it from inside the "gform_after_submission" action to adjust a scheduled payment if the user edits their price-related options using a Gravity Form.

Note: Requires the Order ID ($order_id) for the woocommerce order that you want to add the fee to

1: Set up the woocommerce fee object

$feename = 'Option Adjustment';
$feeamount = 40;
//The following sets up the fee object in a format accepted by woocommerce
$fee = array('name' => $feename, 'amount' => $feeamount, 'taxable' => false, 'tax_class' => '');
  1. Call the function that adds the fee (#3)

    blb_add_fee($fee, $order_id);

  2. The function that adds the fee

    function blb_add_fee( $fee, $order_id ) {

     $item_id = wc_add_order_item( $order_id, array( 
         'order_item_name' => $fee['name'],  
         'order_item_type' => 'fee' 
     ) ); 
    
     if ( ! $item_id ) { 
         return false; 
     } 
    
     if ( $fee['taxable'] ) { 
         wc_add_order_item_meta( $item_id, '_tax_class', $fee['tax_class'] ); 
     } else { 
         wc_add_order_item_meta( $item_id, '_tax_class', '0' ); 
     } 
    
     wc_add_order_item_meta( $item_id, '_line_total', wc_format_decimal( $fee['amount'] ) ); 
     wc_add_order_item_meta( $item_id, '_line_tax', wc_format_decimal( $fee['tax'] ) ); 
    
     // Save tax data - Since 2.2 
     $tax_data = array_map( 'wc_format_decimal', $fee['tax_data'] ); 
     wc_add_order_item_meta( $item_id, '_line_tax_data', array( 'total' => $tax_data ) ); 
    
     do_action( 'woocommerce_order_add_fee', $order_id, $item_id, $fee ); 
    

    //Remove the following line (blb_calculate_totals) if you dont need to recalculate your totals blb_calculate_totals( $and_taxes = false, $order_id );

     return $item_id; 
    

    }

  3. Lastly, you might want to recalculate your totals (called above using "blb_calculate_totals")

    function blb_calculate_totals( $and_taxes = false, $order_id ) {

     $cart_subtotal = 0; 
     $cart_total = 0; 
     $fee_total = 0; 
     $cart_subtotal_tax = 0; 
     $cart_total_tax = 0; 
    
     /*
     if ( $and_taxes && wc_tax_enabled() ) { 
         $this->calculate_taxes(); 
     } 
     */
    
     // line items 
     $order = new WC_Order( $order_id );
     foreach ( $order->get_items() as $item ) { 
         $cart_subtotal     += wc_format_decimal( isset( $item['line_subtotal'] ) ? $item['line_subtotal'] : 0 ); 
         $cart_total        += wc_format_decimal( isset( $item['line_total'] ) ? $item['line_total'] : 0 ); 
         $cart_subtotal_tax += wc_format_decimal( isset( $item['line_subtotal_tax'] ) ? $item['line_subtotal_tax'] : 0 ); 
         $cart_total_tax    += wc_format_decimal( isset( $item['line_tax'] ) ? $item['line_tax'] : 0 ); 
     } 
    
     //$this->calculate_shipping(); 
    
     foreach ( $order->get_fees() as $item ) { 
         $fee_total += $item['line_total']; 
     } 
    
     $order->set_total( $cart_subtotal - $cart_total, 'cart_discount' ); 
     $order->set_total( $cart_subtotal_tax - $cart_total_tax, 'cart_discount_tax' ); 
    
     $grand_total = round( $cart_total + $fee_total + $order->get_total_shipping() + $order->get_cart_tax() + $order->get_shipping_tax(), wc_get_price_decimals() ); 
    
     $order->set_total( $grand_total, 'total' ); 
    
     return $grand_total; 
    

    }

Upvotes: 3

Related Questions