Support

Home Forums Event Espresso Premium Add surcharge if multi select checkbox question checkbox

Add surcharge if multi select checkbox question checkbox

Posted: November 20, 2017 at 3:35 am


inside

November 20, 2017 at 3:35 am

I’ve been researching the ability to do this with EE4, and found this function:
https://github.com/eventespresso/ee-code-snippet-library/blob/master/checkout/bc_ee_add_product_surcharge.php
I just wanted to use a multi select checkbox question instead of select box in the registration form.
Is it possible?


Tony

  • Support Staff

November 20, 2017 at 2:40 pm

Hi there,

Yes, it should be possible.

When you say you want to use a multi checkbox, how exactly are you wanting that to work? Is each checkbox to add another value?

You’ll likely need the help of a developer but I can help point you in the right direction if you can provide more details.


inside

November 21, 2017 at 1:04 am

I have a registration form with multi checkbox question.
The options in this question is “Session 1″ ,”Session 2″,”Session 3″,”Session 4″,”Session 5″
For each selected session the fee will be the same for all :100 QAR
So I did the below code.

but i get only one additional session fee event if i choose more than one sessions

function bc_ee_determine_whether_to_apply_surcharge() {
// CHANGE $surcharge_QST_ID VALUE TO MATCH THE ID OF YOUR QUESTION
$surcharge_QST_ID = 69;
if ( isset( $_REQUEST[ ‘ee_reg_qstn’ ] ) ) {
foreach ( $registrations as $QST_ID => $response ) {

//write_log(‘QST_ID’.$surcharge_QST_ID);
//write_log(‘surcharge_QST_ID’.$surcharge_QST_ID);

//write_log($response);

if ( $QST_ID === $surcharge_QST_ID ) {

write_log(‘THIS IS THE START OF MY CUSTOM DEBUG’);
//write_log($response);
// [0] => Session 1
// [1] => Session 2
// [2] => Session 3
// [3] => Session 4
// [4] => Session 5

$result = count($response);
write_log($result);
for ($i = 0; $i < $result; $i++) {
write_log(‘value in array ‘.$response[$i]);
// value in array Session 3
// value in array Session 4

// apply the surcharge
add_filter( ‘FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge’, ‘__return_true’ );

if($response[$i]===”Session 1″){
// hook into function below to set surcharge details
add_filter( ‘FHEE__bc_ee_apply_transaction_surcharge__surcharge_details’, ‘bc_ee_session1_fee_surcharge_details’);

}elseif($response[$i]===”Session 2″){
// hook into function below to set surcharge details
add_filter( ‘FHEE__bc_ee_apply_transaction_surcharge__surcharge_details’, ‘bc_ee_session2_fee_surcharge_details’);

}elseif($response[$i]===”Session 3″){
// hook into function below to set surcharge details
add_filter( ‘FHEE__bc_ee_apply_transaction_surcharge__surcharge_details’, ‘bc_ee_session3_fee_surcharge_details’);

}elseif($response[$i]===”Session 4”){
// hook into function below to set surcharge details
add_filter( ‘FHEE__bc_ee_apply_transaction_surcharge__surcharge_details’, ‘bc_ee_session4_fee_surcharge_details’);

}else{
// hook into function below to set surcharge details
add_filter( ‘FHEE__bc_ee_apply_transaction_surcharge__surcharge_details’, ‘bc_ee_session5_fee_surcharge_details’);

}//end if

}//end for

}//end if

}//end foreach
}//end if
}//end function


inside

November 21, 2017 at 5:41 am

This is my final code (Same problem i get only latest selected value in the checkbox)

function bc_ee_determine_whether_to_apply_surcharge() {
// CHANGE $surcharge_QST_ID VALUE TO MATCH THE ID OF YOUR QUESTION
$surcharge_QST_ID = 69;
if ( isset( $_REQUEST[ ‘ee_reg_qstn’ ] ) ) {
foreach ( $_REQUEST[ ‘ee_reg_qstn’ ] as $registrations ) {
if ( ! empty( $registrations ) ) {
foreach ( $registrations as $QST_ID => $response ) {
if ( $QST_ID === $surcharge_QST_ID ) {
$result = count($response);

for ($i = 0; $i < $result; $i++) {

switch ( $response[$i] ) {

case ‘Session 1’ :
// apply the surcharge
add_filter( ‘FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge’, ‘__return_true’ );
// hook into function below to set surcharge details
add_filter( ‘FHEE__bc_ee_apply_transaction_surcharge__surcharge_details’, ‘bc_ee_session1_fee_surcharge_details’ );
break;
case ‘Session 2’ :
// apply the surcharge
add_filter( ‘FHEE__bc_ee_apply_transaction_surcharge__apply_surcharge’, ‘__return_true’ );
// hook into function below to set surcharge details
add_filter( ‘FHEE__bc_ee_apply_transaction_surcharge__surcharge_details’, ‘bc_ee_session2_fee_surcharge_details’ );
break;
}// end switch
}//end for
}
}
}
}
}
}

add_action( ‘AHEE__EE_System__core_loaded_and_ready’, ‘bc_ee_determine_whether_to_apply_surcharge’, 2 );
/**
* EDIT THIS TO HOLD THE DETAILS FOR ONE OF YOUR ANSWER OPTIONS
* @return array
*/
function bc_ee_print_at_home_fee_surcharge_details() {
write_log(‘This is bc_ee_print_at_home_fee_surcharge_details’);
return array(
‘name’ => ‘Session 1’,
‘code’ => ‘session11’,
‘description’ => ‘Session 1’,
‘unit_price’ => 100.00,
‘taxable’ => false,
);

}

function bc_ee_session1_fee_surcharge_details() {
write_log(‘This is bc_ee_session1_fee_surcharge_details’);
return array(
‘name’ => ‘Session 1’,
‘code’ => ‘session1’,
‘description’ => ‘Session 1’,
‘unit_price’ => 100.00,
‘taxable’ => false,
);

}

function bc_ee_session2_fee_surcharge_details() {
write_log(‘This is bc_ee_session2_fee_surcharge_details’);
return array(
‘name’ => ‘Session 2’,
‘code’ => ‘session2’,
‘description’ => ‘Session 2’,
‘unit_price’ => 100.00,
‘taxable’ => false,
);

}
function bc_ee_session3_fee_surcharge_details() {
return array(
‘name’ => ‘Session 3’,
‘code’ => ‘session3’,
‘description’ => ‘Session 3’,
‘unit_price’ => 100.00,
‘taxable’ => false,
);

}

function bc_ee_session4_fee_surcharge_details() {
return array(
‘name’ => ‘Session 4’,
‘code’ => ‘session4’,
‘description’ => ‘Session 4’,
‘unit_price’ => 100.00,
‘taxable’ => false,
);

}

function bc_ee_session5_fee_surcharge_details() {
return array(
‘name’ => ‘Session 5’,
‘code’ => ‘session5’,
‘description’ => ‘Session 5’,
‘unit_price’ => 100.00,
‘taxable’ => false,
);

}

/**
* DO NOT EDIT ANYTHING EXCEPT DEFAULT SURCHARGE DETAILS
*
* bc_ee_apply_transaction_surcharge
*
* @param \EE_Checkout $checkout
* @return \EE_Checkout
*/
function bc_ee_apply_transaction_surcharge( EE_Checkout $checkout ) {
// 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’ => ‘shipping fee’,
// unique code used to identify surcharge in the db, ie: ‘print-at-home-fee’
‘code’ => ‘ticket-shipping-fee’,
// ‘fee for printing tickets’
‘description’ => ‘postal fee for shipping tickets’,
// surcharge amount
‘unit_price’ => 0.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;
}

write_log(“@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@”);
write_log($surcharge_details);
write_log(“@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@”);
// i get only latest checkbox option

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’ );
// End of file bc_ee_apply_transaction_surcharge.php


inside

November 21, 2017 at 10:15 pm

Your early reply is highly appreciated


Tony

  • Support Staff

November 22, 2017 at 5:37 am

Hi there,

Can I ask why you linked to one example in your opening post, then use a completely different example in your follow ups? Those 2 functions work at different steps in the registration.

So with the first example you gave, its set up to only allow a single value from the questions to apply a ‘product’. The code needs some refactoring to allow you to select multiple items within a checkbox and then add a ‘product’ for each selected answer, take a look at this example:

https://gist.github.com/Pebblo/51c4284d2dbedb7c0e1daff0c562f1dc#file-example-multiple-php


inside

November 22, 2017 at 6:19 am

My apologies, I liked to the wrong gist, the right link is:
https://github.com/eventespresso/ee-code-snippet-library/blob/master/checkout/bc_ee_add_product_surcharge.php

Thank you Tony Warwick for the wonderful script. it’s working like a charm <3


inside

November 23, 2017 at 5:14 am

Hi Tony,

I have one question please, is it normal that when the ticket is free the surcharge is not applied and the payment step is ignored?


Tony

  • Support Staff

November 23, 2017 at 5:51 am

Yes that’s normal.

If your ticket is free the Single Page Checkout Module does not build out the payment options step, the snippet above adds a charge between the attendee info and payment options step so if you don’t a ticket with a price you don’t see the charge.


inside

November 23, 2017 at 6:06 am

Really thank you for your wonderful help and sorry for asking a lot of questions but there is any possibility to make the payment option of surcharge mandatory?


Tony

  • Support Staff

November 23, 2017 at 7:02 am

I don’t there is, I checked the code and it doesn’t appear to allow that.

When the checkout page is generated it checks if a payment is required for your registrations and if not it does not initialize the payment options steps (to reduce processing that is unneeded).

The snippet above adds a payment amount to the transaction after the above happens so the payment options steps isn’t there.

I’ll check with our developers to see if its possible to work around that but I don’t believe there is.


inside

November 23, 2017 at 7:22 am

Thanks a lot.I’m waiting for your feedback


Tony

  • Support Staff

November 24, 2017 at 1:36 pm

So right now it’s not possible to force EE to show the payment options for a free ticket, the function that checks if the transaction is free and removes the payment options steps is ran way before the surcharges are added to the registration.

However we can add a filter to the function that will allow it to be bypassed and I’ve created a ticket for for this which will be reviewed, test and released so it may be possible (all being well) to do wtih the next version of EE.


Tony

  • Support Staff

November 24, 2017 at 2:04 pm

Actually, a simple filter for this isn’t going to work so for now you’ll need to use a paid ticket.

I’ve created a ticket to investigate this further but as the developers are working on other areas of the plugin this is unlikely to be added ‘soon’.

The support post ‘Add surcharge if multi select checkbox question checkbox’ 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