Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dwanjuki/efe00c3cde2c6bced480cce0124e9c8a to your computer and use it in GitHub Desktop.
Save dwanjuki/efe00c3cde2c6bced480cce0124e9c8a to your computer and use it in GitHub Desktop.
Adding additional items at checkout using User Fields
<?php
/**
* This recipe will add additional fees and pricing
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_add_user_fields() {
// Don't break if PMPro is out of date or not loaded.
if ( ! function_exists( 'pmpro_add_user_field' ) ) {
return false;
}
// Store our field settings in an array.
$fields = array();
$fields[] = new PMPro_Field(
'addon_options', // input field name, used as meta key
'checkbox_grouped', // field type
array(
'label' => 'Choose your Add Ons',
'options' => array( // display the different options, no need for a "blank" option
'add_on_a' => 'I would like to receive Item 1: US $20',
'add_on_b' => 'I would like to receive Item 2: US $40',
),
'profile' => true,
'memberslistcsv' => true,
'addmember' => true,
'required' => false,
)
);
// Add a field group to put our fields into.
pmpro_add_field_group( 'Add Ons' );
//add the fields into a new checkout_boxes area of the checkout page
foreach($fields as $field) {
pmpro_add_user_field(
'Add Ons', // location on checkout page
$field // PMPro_Field object
);
}
}
add_action( 'init', 'my_pmpro_add_user_fields' );
function my_pmpro_checkout_level($level) {
$addon_checkboxes = !empty( $_REQUEST['addon_options'] ) ? $_REQUEST['addon_options'] : '';
if ( ! is_array( $addon_checkboxes ) ) {
return $level;
}
foreach( $addon_checkboxes as $options ) {
switch( $options ) {
case "add_on_a":
$level->initial_payment = $level->initial_payment + 20; //to update the initial payment.
//$level->billing_amount = $level->billing_amount + 49; //to update recurring payments too
break;
case "add_on_b":
$level->initial_payment = $level->initial_payment + 40; //to update the initial payment.
//$level->billing_amount = $level->billing_amount + 99; //to update recurring payments too
break;
}
}
return $level;
}
add_filter( 'pmpro_checkout_level', 'my_pmpro_checkout_level' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment