Support

Home Forums Event Espresso Premium EE4 Price Modifier via questions

EE4 Price Modifier via questions

Posted: November 26, 2016 at 2:17 pm

Viewing 2 reply threads


Michel Hurrell

November 26, 2016 at 2:17 pm

I have searched high and low. I am trying to find some more documentations. Since the price modifier of EE3 is gone and it looks like it might be coming back next year hopefully, I have had to try and use some custom code. Any help to point me in the right direction.

I created a plugin based off of this page:
https://github.com/eventespresso/ee-code-snippet-library/blob/master/checkout/bc_ee_apply_transaction_surcharge.php

This had lots of good bits of information.

<?php
/**
 * Plugin Name: Custom Functions Event Espresso 
 * Plugin URI: 
 * Description: This plugin adds surcharge to checkout.
 * Version: 1.0.0
 * Author URI: 
 * License: GPL2
 */
 
/**
 * PLEASE READ AND FOLLOW ALL INSTRUCTIONS IN CAPS
 *
 * IN ORDER FOR THIS TO WORK YOU NEED TO ADD A CUSTOM QUESTION
 * BY LOGGING INTO THE WORDPRESS 	ADMIN AND GOING TO :
 *        Event Espresso > Registration Form
 * AND THEN CLICKING ON "Add New Question"
 * FOR THIS EXAMPLE CODE I CREATED A QUESTION NAMED "Ticket Printing"
 * SET ITS TYPE TO "Dropdown" AND GAVE IT THE FOLLOWING TWO OPTIONS:
 * 		"you print tickets at home"
 * 		"we print and ship tickets"
 * (THE ANSWER VALUES ARE CASE SENSITIVE)
 * THEN SET THE QUESTION TO REQUIRED
 *
 * BECAUSE THIS QUESTION SHOULD ONLY BE ASKED ONCE PER TRANSACTION
 * I ALSO CREATED A QUESTION GROUP CALLED "Ticket Fees"
 * AND ADDED THE "Ticket Printing" QUESTION TO THAT GROUP
 *
 * THEN ON MY EVENT ( Event Espresso > Events > Edit Event ),
 * I CHECKED OFF THE  "Ticket Fees" QUESTION GROUP
 * IN THE "Questions for Primary Registrant" SIDEBAR METABOX
 *
 * THIS WAY, ONLY THE PRIMARY REGISTRANT WILL BE ASKED
 * TO SELECT A TICKET PRINTING OPTION, WHICH WILL THEN
 * CONTROL WHICH CHARGE IS ADDED TO THE TRANSACTION
 *
 * PLZ SEE ADDITIONAL INSTRUCTIONS IN FUNCTIONS BELOW
 *
 * bc_ee_determine_whether_to_apply_surcharge
 *
 * @return void
 */
function bc_ee_determine_whether_to_apply_surcharge() {
	// CHANGE $surcharge_QST_ID VALUE TO MATCH THE ID OF YOUR QUESTION
	$surcharge_QST_ID = 24;
	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 ) {
						switch ( $response ) {
							// CHANGE THESE TO EXACTLY MATCH THE ANSWER OPTIONS FOR YOUR QUESTION
							// THEN EDIT / ADD / DELETE THE FUNCTIONS BELOW
							// WHOSE NAMES MATCH THESE OPTIONS
							// YOU CAN ADD NEW OPTIONS, JUST MAKE SURE TO HAVE A CORRESPONDING
							// FUNCTION FOR SETTING THE SURCHARGE DETAILS
							case 'Lundi' :
								// 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_day_care_1_fee_surcharge_details' );
								break;
							case 'Mardi' :
								// 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_day_care_2_fee_surcharge_details' );
								break;
								case 'Mercedi' :
								// 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_day_care_3_fee_surcharge_details' );
								break;
								case 'Jeudi' :
								// 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_day_care_4_fee_surcharge_details' );
								break;
								case 'Vendredi' :
								// 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_day_care_5_fee_surcharge_details' );
								break;

						}
					}
				}
			}
		}
	}
}
add_action( 'AHEE__EE_System__core_loaded_and_ready', 'bc_ee_determine_whether_to_apply_surcharge', 1 );
/**
 * EDIT THIS TO HOLD THE DETAILS FOR ONE OF YOUR ANSWER OPTIONS
 * @return array
 */
function bc_ee_day_care_1_fee_surcharge_details() {
	return array(
		'name'        	=> 'Lundi day care',
		'code'        	=> '1_day_care',
		'description' 	=> 'day care',
		'unit_price'  	=> 7.00,
		'taxable'     	=> false,
	);
}
/**
 * EDIT THIS TO HOLD THE DETAILS FOR ONE OF YOUR ANSWER OPTIONS
 * @return array
 */
function bc_ee_day_care_2_fee_surcharge_details() {
	return array(
		'name'        	=> 'Mardi day care',
		'code'        	=> '2_day_care',
		'description' 	=> 'day care',
		'unit_price'  	=> 7.00,
		'taxable'     	=> false,
	);
}
/**
 * EDIT THIS TO HOLD THE DETAILS FOR ONE OF YOUR ANSWER OPTIONS
 * @return array
 */
function bc_ee_day_care_3_fee_surcharge_details() {
	return array(
		'name'        	=> 'Mercedi day care',
		'code'        	=> '3_day_care',
		'description' 	=> 'day care',
		'unit_price'  	=> 7.00,
		'taxable'     	=> false,
	);
}
/**
 * EDIT THIS TO HOLD THE DETAILS FOR ONE OF YOUR ANSWER OPTIONS
 * @return array
 */
function bc_ee_day_care_4_fee_surcharge_details() {
	return array(
		'name'        	=> 'Jeudi day care',
		'code'        	=> '4_day_care',
		'description' 	=> 'day care',
		'unit_price'  	=> 7.00,
		'taxable'     	=> false,
	);
}
/**
 * EDIT THIS TO HOLD THE DETAILS FOR ONE OF YOUR ANSWER OPTIONS
 * @return array
 */
function bc_ee_day_care_5_fee_surcharge_details() {
	return array(
		'name'        	=> 'Vendredi day care',
		'code'        	=> '5_day_care',
		'description' 	=> 'day care',
		'unit_price'  	=> 7.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'        => 'daycare',
			// unique code used to identify surcharge in the db, ie: 'print-at-home-fee'
			'code'        => 'ticket-day-care-fee',
			// 'fee for printing tickets'
			'description' => 'cost of day care',
			// surcharge amount
			'unit_price'  => 7.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' );
// End of file bc_ee_apply_transaction_surcharge.php

I can’t seem to get this to work, I have followed all of directions and reviewed it several times. Any ideas guys.

My question is does that have to be a dropdown question or can it be a checkbox question with multiple different answers.

My overall goal is to charge for a added service in the questions.

I do not want to make additional packages / bundles for this.

Thanks guys.

  • This topic was modified 8 years ago by Seth Shoultes. Reason: Removed the author name


Michel Hurrell

November 26, 2016 at 2:18 pm

Can someone remove the name in the code please that was put in there by accident, copy pasted too much.


Seth Shoultes

  • Support Staff

November 28, 2016 at 11:33 am

Hey Michel,

I removed that name for you.

Would you mind posting this as a new issue on our Github page so our developers will see it and offer feedback?

You can post it as a new issue here:

https://github.com/eventespresso/event-espresso-core/issues

Viewing 2 reply threads

The support post ‘EE4 Price Modifier via questions’ 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