Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Last active April 5, 2019 09:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewlimaza/49d5c60c5209a47b0fc7b431b814f7b0 to your computer and use it in GitHub Desktop.
Save andrewlimaza/49d5c60c5209a47b0fc7b431b814f7b0 to your computer and use it in GitHub Desktop.
Pay By Check select a payment type from Register Helper Add On.
<?php
/**
* Creates a custom field for checkout called "Payment Type" which will allow you to specify which payment type was selected for an order.
* Saves the selection inside the Order > Payment Type.
* Add this code to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmprorh_init() {
//don't break if Register Helper is not loaded
if(!function_exists( 'pmprorh_add_registration_field' )) {
return false;
}
//define the fields
$fields = array();
$fields[] = new PMProRH_Field(
'payment_type', // input name, will also be used as meta key
'select', // type of field
array(
'label' => 'Payment Type',
'options' => array(
'' => '',
'cash' => 'Cash',
'check' => 'Check',
'other' => 'Other',
)
)
);
$fields[] = new PMProRH_Field(
'other_payment_type', // input name, will also be used as meta key
'text', // type of field
array(
'label' => 'Please describe the payment method used',
'depends' => array(
array(
'id' => 'payment_type',
'value' => 'other'
)
)
)
);
//add the fields into a new checkout_boxes are of the checkout page
foreach($fields as $field)
pmprorh_add_registration_field(
'checkout_boxes', // location on checkout page
$field // PMProRH_Field object
);
//that's it. see the PMPro Register Helper readme for more information and examples.
}
add_action( 'init', 'my_pmprorh_init' );
function add_payment_type_to_order( $order ) {
// Bail if payment gateway isn't check.
if ( 'check' != $order->gateway ) {
return;
}
if ( isset( $_REQUEST['other_payment_type'] ) && ! empty( $_REQUEST['other_payment_type'] ) ) {
$order->payment_type = esc_attr( $_REQUEST['other_payment_type'] );
} elseif ( isset( $_REQUEST['payment_type']) && 'other' !== $_REQUEST['payment_type'] ) {
$order->payment_type = esc_attr( $_REQUEST['payment_type'] );
}
$order->saveOrder();
}
add_action( 'pmpro_added_order', 'add_payment_type_to_order' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment