Support

Home Forums Event Espresso Premium Troubleshooting Surcharge on Total Amount Reply To: Troubleshooting Surcharge on Total Amount

Reply To: Troubleshooting Surcharge on Total Amount

Author Replies
Amdur Productions # Posted on April 26, 2018 at 2:58 pm

Thanks! This works:


function cortex_ee_apply_transaction_surcharge( EE_Checkout $checkout ) {

	// verify checkout
	if ( ! $checkout instanceof EE_Checkout ) {
		return $checkout;
	}

	// verify cart
	$cart = $checkout->cart;
	if ( ! $cart instanceof EE_Cart ) {
		return $checkout;
	}

	// verify grand total line item
	$grand_total = $cart->get_grand_total();
	if ( ! $grand_total instanceof EE_Line_Item ) {
		return $checkout;
	}

	// Make Sure Surcharge Only Gets applied once
	$existing_surcharge = $grand_total->get_child_line_item( 'cc-surcharge' );
	if ( $existing_surcharge instanceof EE_Line_Item ) {
		return $checkout;
	}

	EE_Registry::instance()->load_helper( 'Line_Item' );
	$pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal( $grand_total );
	$percentage_amount = 4;

	$pre_tax_subtotal->add_child_line_item(
		EE_Line_Item::new_instance( array(
			'LIN_name' => '4% Surcharge',
            'LIN_desc' => '',
            'LIN_unit_price' => 0,
            'LIN_percent' => $percentage_amount,
            'LIN_quantity' => 1,
            'LIN_is_taxable' => false,
            'LIN_total' => (float)($percentage_amount * ($grand_total->total() / 100)),
            'LIN_type' => EEM_Line_Item::type_line_item,
            'LIN_parent' => $grand_total->ID(),
            'LIN_code' => 'cc-surcharge'
		) )
	);
	$grand_total->recalculate_total_including_taxes();
	
	return $checkout;
}
add_filter( 'FHEE__EED_Single_Page_Checkout___initialize_checkout__checkout', 'cortex_ee_apply_transaction_surcharge' );
Do NOT follow this link or you will be banned from the site!