Support

Home Forums Event Espresso Premium Handling global variable for implementing surcharge custom function

Handling global variable for implementing surcharge custom function

Posted: September 22, 2019 at 8:57 pm

Viewing 4 reply threads


Patrick

September 22, 2019 at 8:57 pm

Good evening,

I have built several surcharge fonctions based on this example with success (4 functions up now !).

I also built a variable donation surcharge based on the above example and it worked well for one of my event wihtout any flaws. Nevertheless, trying to duplicate this fonction on another event registration question group lead to erratic results, i.e. outputting zeros or ones instead of field input value. I suspect that I do not handle global variable ($response_don_2) correctly but before spending more time debugging, I would like to have your recommendations or guidance. Here is the custom code :


$response_don_2 = 0;

function rc_ee_determine_whether_to_apply_surcharge_don_2() {
// CHANGE $surcharge_QST_ID VALUE TO MATCH THE ID OF YOUR QUESTION
global $response_don_2;
$surcharge_QST_ID = 39;
if ( isset( $_REQUEST[ 'ee_reg_qstn' ] ) ) {
foreach ( $_REQUEST[ 'ee_reg_qstn' ] as $registrations ) {
if ( ! empty( $registrations ) ) {
foreach ( $registrations as $QST_ID => $response_don_2 ) {
if ( $QST_ID === $surcharge_QST_ID ) {
if ( ( $response_don_2 > 0 ) && ( $response_don_2 < 1000 ) ) {
add_filter( 'FHEE__rc_ee_apply_transaction_surcharge__apply_surcharge_don_2', '__return_true' );
// hook into function below to set surcharge details
add_filter( 'FHEE__rc_ee_apply_transaction_surcharge__surcharge_details_don_2', 'rc_ee_don_amra_2' );
}
}
}
}
}
}
}

add_action( 'AHEE__EE_System__core_loaded_and_ready', 'rc_ee_determine_whether_to_apply_surcharge_don_2', 1 );

function rc_ee_don_amra_2() {
global $response_don_2;
return array(
'name' => 'Don (AMRA)',
'code' => 'don-amra-convention',
'description' => 'Pour le bénéfice du Domaine rosicrucien de Lachute',
'unit_price' => $response_don_2,
'taxable' => false,
);
}

function rc_ee_apply_transaction_surcharge_don_2( EE_Checkout $checkout ) {
$surcharge_details = apply_filters(
'FHEE__rc_ee_apply_transaction_surcharge__surcharge_details_don_2',
array(
'name' => 'Don (AMRA)',
'code' => 'don-amra-convention',
'description' => 'Pour le bénéfice du Domaine rosicrucien de Lachute',
'unit_price' => 0,
'taxable' => false,
)
);
// STOP EDITING
// apply the surcharge ?
if ( ! apply_filters( 'FHEE__rc_ee_apply_transaction_surcharge__apply_surcharge_don_2', 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', 'rc_ee_apply_transaction_surcharge_don_2' );

Thanks in advance for you help.

Cheers.

Patrick


Patrick

September 22, 2019 at 8:59 pm

Sorry for the above presentation, I have used the and tags but it seems that I have forgotten something !!!


Josh

  • Support Staff

September 23, 2019 at 12:37 pm

Hi,

The global variable may indeed be the problem. Can you avoid using a global variable there and follow the example you linked to instead?


Patrick

September 23, 2019 at 9:38 pm

Hi Josh,

After digging a little further, the global variable is not responsible (at least by itself) for the strange behavior of the variable donation surcharge function.

After creating a new question and assigning it to a group of questions, I cannot mess around the question’s order inside the group of questions. If I move a question related to a surcharge, the surcharge function will stop working. If I resume the rank of the related question inside the group of questions, everything get back to normal and the function perform well again !

I can sure leave things as they are as it seems to work if I don’t modify the question’s groups but it would be a good thing to understand why it is so.

Hope you’ll have some guidance for me.

Regards.

Patrick


Josh

  • Support Staff

September 24, 2019 at 7:57 am

Hi Patrick,

Do you know how to add debug code so you can debug issues with your code?

What you can do is add this code to a functions file:

if (!function_exists('write_log')) {
    function write_log ( $log )  {
        if ( true === WP_DEBUG ) {
            if ( is_array( $log ) || is_object( $log ) ) {
                error_log( print_r( $log, true ) );
            } else {
                error_log( $log );
            }
        }
    }
}

Then you make the following modification to your wp-config.php file where it sets WP_DEBUG:

define( 'WP_DEBUG', true );
if ( WP_DEBUG ) {
        define( 'WP_DEBUG_LOG', true );
        define( 'WP_DEBUG_DISPLAY', false );
}

Then, you step through your custom code and check values with the above function. e.g.

write_log($variable_name);

Then test, and tail your site’s wp-content/debug.log file to check what gets logged.

Viewing 4 reply threads

The support post ‘Handling global variable for implementing surcharge custom function’ 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