Support

Home Forums Event Espresso Premium Zero Surcharge with Promo Code

Zero Surcharge with Promo Code

Posted: November 5, 2018 at 6:05 pm


ams4cme

November 5, 2018 at 6:05 pm

I have a printed-syllabus-fee that users can elect to add on to their order to receive a printed version of the event syllabus. While this works perfectly when no promotional code has been applied, when a promo code is active, I want to zero out the cost of the printed syllabus fee. The below code seems to accomplish this, but when I checkout, there is still at $75 charge that remains “unpaid.” That leads me to believe the old line item still exists. Is there some way to remove a line item? Something like the inverse of the $pre_tax_subtotal->add_child_line_item call?

function bc_ee_apply_transaction_surcharge( EE_Checkout $checkout ) {

//If the promotions add-on is not active we don’t need any of this to run.
if( class_exists(‘EEM_Promotion’) ) {
//$checkout = EE_Registry::instance()->SSN->checkout();
if ( $checkout instanceof EE_Checkout ) {
$transaction = $checkout->transaction;
if ( $transaction instanceof EE_Transaction ) {
//Create an array to hold the event ID’s.
$EVT_IDs = array();
//Pull the event ID’s from the registrations.
foreach ( $transaction->registrations() as $registration ) {
if ( $registration instanceof EE_Registration ) {
$event = $registration->event();
if ( $event instanceof EE_Event ) {
$EVT_IDs[] = $event->ID();
}
}
}
//We only need each event ID once.
$EVT_IDs = array_unique($EVT_IDs);

//Pull any promotions linked to the events in the cart.
$promotions = EEM_Promotion::instance()->count(
array(
array(
‘Promotion_Object.POB_type’ => ‘Event’,
‘Promotion_Object.OBJ_ID’ => array( ‘IN’, $EVT_IDs )
)
)
);

// if Promo Active, then set surcharge to zero
if( $promotions != 0 ) {

// SURCHARGE DETAILS – EDIT THIS
$surcharge_details = apply_filters(
‘FHEE__bc_ee_apply_transaction_surcharge__surcharge_details’,
array(
// name for surcharge that will be displayed, ie: ‘printing fee’
‘name’ => ‘Printed syllabus fee’,
// unique code used to identify surcharge in the db, ie: ‘print-at-home-fee’
‘code’ => ‘printed-syllabus-fee’,
// ‘fee for printing tickets’
‘description’ => ‘fee for printed syllabus’,
// surcharge amount
‘unit_price’ => 0.00,
// whether or not tax is applied on top of the surcharge
‘taxable’ => false,
)
);

/*
EE_Registry::instance()->load_helper( ‘Line_Item’ );
$pre_tax_subtotal = EEH_Line_Item::get_pre_tax_subtotal( $grand_total );
$pre_tax_subtotal->add_child_line_item(
EE_Line_Item::new_instance( array(
‘LIN_name’ => $surcharge_details[ ‘name’ ],
‘LIN_desc’ => $surcharge_details[ ‘description’ ],
‘LIN_unit_price’ => (float) $surcharge_details[ ‘unit_price’ ],
‘LIN_quantity’ => 1,
‘LIN_is_taxable’ => $surcharge_details[ ‘taxable’ ],
‘LIN_order’ => 0,
‘LIN_total’ => (float) $surcharge_details[ ‘unit_price’ ],
‘LIN_type’ => EEM_Line_Item::type_line_item,
‘LIN_code’ => $surcharge_details[ ‘code’ ],
) )
);
$grand_total->recalculate_total_including_taxes();

*/
} // end if ( $promotions != 0 )

} // end if ( $transaction instanceof EE_Transaction )
} // end if ( $checkout instanceof EE_Checkout )
} // end if( class_exists(‘EEM_Promotion’) )
else {
// DEFAULT SURCHARGE DETAILS – EDIT THIS
$surcharge_details = apply_filters(
‘FHEE__bc_ee_apply_transaction_surcharge__surcharge_details’,
array(
// name for surcharge that will be displayed, ie: ‘printing fee’
‘name’ => ‘Printed syllabus fee’,
// unique code used to identify surcharge in the db, ie: ‘print-at-home-fee’
‘code’ => ‘printed-syllabus-fee’,
// ‘fee for printing tickets’
‘description’ => ‘fee for printed syllabus’,
// surcharge amount
‘unit_price’ => 75.00,
// whether or not tax is applied on top of the surcharge
‘taxable’ => false,
)
);

}

// STOP EDITING
// apply the surcharge ?
if ( ! apply_filters( ‘FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge’, false ) ) {
return $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;
}
// has surcharge already been applied ?
$existing_surcharge = $grand_total->get_child_line_item( $surcharge_details[ ‘code’ ] );
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 );
$pre_tax_subtotal->add_child_line_item(
EE_Line_Item::new_instance( array(
‘LIN_name’ => $surcharge_details[ ‘name’ ],
‘LIN_desc’ => $surcharge_details[ ‘description’ ],
‘LIN_unit_price’ => (float) $surcharge_details[ ‘unit_price’ ],
‘LIN_quantity’ => 1,
‘LIN_is_taxable’ => $surcharge_details[ ‘taxable’ ],
‘LIN_order’ => 0,
‘LIN_total’ => (float) $surcharge_details[ ‘unit_price’ ],
‘LIN_type’ => EEM_Line_Item::type_line_item,
‘LIN_code’ => $surcharge_details[ ‘code’ ],
) )
);
$grand_total->recalculate_total_including_taxes();
return $checkout;
}
add_filter( ‘FHEE__EED_Single_Page_Checkout___initialize_checkout__checkout’, ‘bc_ee_apply_transaction_surcharge’ );


Josh

  • Support Staff

November 5, 2018 at 6:56 pm

Hi,

I’m sorry for the delay, I see you’ve purchased priority support with this question.

I did some checking, and it looks like the delete_child_line_item() method will do what you’re looking to do. Its source is here:

https://github.com/eventespresso/event-espresso-core/blob/master/core/db_classes/EE_Line_Item.class.php#L764

The support post ‘Zero Surcharge with Promo Code’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso