Using a global variable to create a filter

3 Replies ·

  1. Hi Justin,

    I have been bouncing my head around for a while on a custom function. It add a variable donation field (integer) to an existing Event Espresso registration form. I did use the same function on another site without any problem but, for some reason, this one is behaving erratically, sometimes outputting 0 and other time 1.

    I suspect that it is the way I handle the global variable (please don’t blame me for using a global variable, I cannot find another way to work around as I don’t know how to create my custom filter wihtout using it; the add_filter() does not seem to like having an array as an argument.)

    Your help and orientation would be greatly appreciated.

    Cheers.

    Patrick

    $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  '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' );
    
  2. Patrick Sergerie

    I again Justin !

    Here is an attempt to get rid of the global variable but still not working :

    function rc_ee_determine_whether_to_apply_surcharge_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  'Don (AMRA)',
                                        'code'          => 'don-amra-convention',
                                        'description'   => 'Pour le bénéfice du Domaine rosicrucien de Lachute',
                                        'unit_price'    => $response_don_2,
                                        'taxable'       => false,
                                    )
                                );
                            }
                            else {
    
                                add_filter( 'FHEE__rc_ee_apply_transaction_surcharge__apply_surcharge_don_2', '__return_false' );
    
                            }   
                        }
                    }
                }
            }
        }
    }
    
    add_action( 'AHEE__EE_System__core_loaded_and_ready', 'rc_ee_determine_whether_to_apply_surcharge_don_2', 1 );
    
  3. Justin Tadlock

    Sorry for the late reply.

    The problem here is that I don’t understand what the code is supposed to be doing. There’s a lot of code there with really not much context.

    My advice would be to break everything down to its simplest parts. Forget all the conditional checks, loops, and everything else. Just write the code that would make your filter work first. Post that here.