eventespresso/ee-code-snippet-library

checkout/bc_add_cart_modifier.php new line item not added to total

finalcircuit opened this issue · 3 comments

I'd really love to use this snippet as it does almost exactly what I want. But although the line item for the surcharge is created (and shows up on invoices/receipts/transaction details), the value of the surcharge is not added to the total for the transaction. The one place the correct total is displayed is in the REG Details tab but I'm guessing that's adding everything up itself.

For testing, I'm using the code completely unmodified in functions.php of Twenty Sixteen. Wordpress is 5.1 and EE is 4.9.78.p.

I did notice that the comments on EEH_Line_Item::add_percentage_based_item (which is responsible for adding the line item in question) say "You should call EE_Registration_Processor::calculate_reg_final_prices_per_line_item() after using this, to keep the registration final prices in-sync with the transaction's total." That function doesn't actually exist (though there is a function of that name in EEH_Line_Item itself) and the snippet calls EE_Registration_Processor::update_registration_final_prices() which sounds like it should do the same kind of thing.

So basically, help?

Phil

hi @finalcircuit, thanks for reporting the issue. I'm reproducing it with the code snippet (I'm sorry that at first I didn't notice the transaction total wasn't getting updated, everything else looked ok...)
I had to tweak the code snippet to have it also update the transaction total. Here's the modified version:

/**
 * bc_add_cart_modifier
 *
 * @param \EE_SPCO_Reg_Step $payment_options_reg_step
 * @throws \EE_Error
 */
function bc_add_cart_modifier( EE_SPCO_Reg_Step $payment_options_reg_step ) {
    // CHANGE THESE TO YOUR LIKING
    $cart_modifier_name = 'my cart % surcharge';
    $cart_modifier_amount = 10.00;
    $cart_modifier_description = '10% surcharge for choosing invoice payment method';
    $cart_modifier_taxable = true; // or false if surcharge is not taxable
    $payment_methods_with_surcharges = array( 'invoice' );
    // get what the user selected for payment method
    $selected_method_of_payment = $payment_options_reg_step->checkout->selected_method_of_payment;
    if ( ! in_array( $selected_method_of_payment, $payment_methods_with_surcharges, true ) ) {
        // does not require surcharge
        return;
    }
    $cart = $payment_options_reg_step->checkout->cart;
    if ( ! $cart instanceof EE_Cart ) {
        // ERROR
        return;
    }
    $total_line_item = $cart->get_grand_total();
    if ( ! $total_line_item instanceof EE_Line_Item && ! $total_line_item->is_total() ) {
        // ERROR
        return;
    }
    $transaction = $payment_options_reg_step->checkout->transaction;
    if ( ! $transaction instanceof EE_Transaction) {
        // ERROR
        return;
    }
    //delete existing in case page is refreshed or something
    EEM_Line_Item::instance()->delete(
        array(
            array(
                'TXN_ID'   => $transaction->ID(),
                'LIN_name' => $cart_modifier_name,
            )
        )
    );
    // changed
    $amount_owing_before = $payment_options_reg_step->checkout->amount_owing;
    $surcharge_line_item_id = EEH_Line_Item::add_percentage_based_item(
        $total_line_item,
        $cart_modifier_name,
        $cart_modifier_amount,
        $cart_modifier_description,
        $cart_modifier_taxable
    );
    if ($surcharge_line_item_id) {
        // changed
        $total_line_item->recalculate_total_including_taxes();
        $payment_options_reg_step->checkout->amount_owing += ($total_line_item->total() - $amount_owing_before);
        $transaction->set_total($total_line_item->total());
        $success = $transaction->save();
        if ($success) {
            /** @type EE_Registration_Processor $registration_processor */
            $registration_processor = EE_Registry::instance()->load_class('Registration_Processor');
            $registration_processor->update_registration_final_prices($transaction);
        }
    }
}
add_action( 'AHEE__Single_Page_Checkout__before_payment_options__process_reg_step', 'bc_add_cart_modifier', 10, 1 );

Can you give that a whirl and let me know if it works properly for you too?

My first test worked perfectly so I think that's resolved it. Thanks for the quick fix.

Phil

Great!
And don't feel bad telling everyone which is your favourite WordPress events plugin 😉 https://wordpress.org/support/plugin/event-espresso-decaf/reviews/?filter=5