Skip to content

Instantly share code, notes, and snippets.

@gausam
Created October 16, 2020 11:47
Show Gist options
  • Save gausam/e9fb69ef8b9c1b8864c12b2c37870b4f to your computer and use it in GitHub Desktop.
Save gausam/e9fb69ef8b9c1b8864c12b2c37870b4f to your computer and use it in GitHub Desktop.
Add phone and shipping method fields to the checkout page, and change order of countries in Country field, with UK as the default.
<?php
/**
* This recipe adds custom phone and shipping method fields to the checkout page.
* It will also change the order of countries in the Country field and set the UK
* as the default.
*
* It is intended for use with the Register Helper and Shipping Address Add Ons:
* https://www.paidmembershipspro.com/add-ons/pmpro-register-helper-add-checkout-and-profile-fields/
* https://www.paidmembershipspro.com/add-ons/shipping-address-membership-checkout/
*
* 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_pmprorh_init() {
global $pmpro_countries, $pmpro_default_country;
//don’t break if Register Helper is not loaded
if ( ! function_exists( "pmprorh_add_registration_field" ) ) {
return false;
}
// Set the UK as the default country
$pmpro_default_country = 'GB';
$priority_countries = array(
'GB',
'US',
'NL',
'FR',
'DE',
'ES'
);
$countries_priority = array(
'GB' => $pmpro_countries['GB'],
'US' => $pmpro_countries['US'],
'NL' => $pmpro_countries['NL'],
'FR' => $pmpro_countries['FR'],
'DE' => $pmpro_countries['DE'],
'ES' => $pmpro_countries['ES'],
);
$countries_rest = array();
foreach( $pmpro_countries as $key => $value ) {
if ( ! in_array( $key, $priority_countries ) ) {
$countries_rest[$key] = $value;
}
}
$pmpro_countries = array_merge($countries_priority, $countries_rest);
$phoneField = new PMProRH_Field(
'phone',
'text',
array(
'label' => 'Phone',
'required' => true, // make field required, set value to false to make it optional
'profile' => true, // show in user profile,
)
);
pmprorh_add_registration_field(
'after_email', // location on checkout page
$phoneField // PMProRH_Field object
);
$shippingMethodField = new PMProRH_Field(
'shipping_method',
'select',
array(
'label' => 'Shipping Method',
'required' => true, // make field required, set value to false to make it optional
'profile' => false, // show in user profile,
'options' => array(
'launch_offer_within_uk' => '*Launch Offer Shipping Cost – £0',
'launch_offer_international' => '*International Launch Offer Shipping Cost – £9.99',
'standard_shipping_uk' => 'Standard Shipping within UK (3-5 days) – £4.80',
'express_shipping_uk' => 'Express Shipping within UK (1-2 days) – £8.54',
'international_shipping' => 'International Shipping (7 – 14days) – £21.92',
)
)
);
pmprorh_add_registration_field(
'before_submit_button', // location on checkout page
$shippingMethodField // PMProRH_Field object
);
}
add_action( "init", "my_pmprorh_init" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment