Skip to content

Instantly share code, notes, and snippets.

@solepixel
Created February 17, 2014 05:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solepixel/9045318 to your computer and use it in GitHub Desktop.
Save solepixel/9045318 to your computer and use it in GitHub Desktop.
iThemes Exchange attempts to create a custom customer/registration field.
<?php do_action( 'it_exchange_content_registration_before_company_element' ); ?>
<div class="it-exchange-registration-company">
<?php it_exchange( 'dichiara_registration', 'company' ); ?>
</div>
<?php do_action( 'it_exchange_content_registration_after_company_element' ); ?>
<?php
# this would be included in functions.php
require_once( 'dichiara_registration.class.php' );
add_action( 'after_setup_theme', 'dichiara_setup', 12 );
function dichiara_setup(){
add_filter( 'it_exchange_get_content_registration_field_details', 'dichiara_itex_registration_fields' );
add_filter( 'it_exchange_get_content_checkout/elements/purchase-requirements/logged-in/elements/registration/_fields_elements', 'dichiara_itex_registration_fields' );
add_filter( 'it_exchange_get_content_profile_fields_elements', 'dichiara_itex_registration_fields' );
add_filter( 'it_exchange_get_purchase_requirments', 'dichiara_itex_purchase_requirements' );
}
function dichiara_itex_registration_fields( $fields ){
$new_fields = array();
foreach( $fields as $field ){
$new_fields[] = $field;
if( $field == 'last-name' ){
$new_fields[] = 'company';
} elseif( $field == 'email' ){
$new_fields[] = 'phone';
}
}
return $new_fields;
}
function dichiara_itex_purchase_requirements( $requirements ){
$requirements['phone'] = array(
'priority' => 2,
'requirement-met' => 'dichiara_itex_get_customer_phone',
'sw-template-part' => 'phone',
'checkout-template-part' => 'phone',
'notification' => __( 'Phone number is required to checkout.', 'dichiara' ),
'slug' => 'phone'
);
$requirements['company'] = array(
'priority' => 4,
'requirement-met' => 'dichiara_itex_get_customer_company',
'sw-template-part' => 'company',
'checkout-template-part' => 'company',
'notification' => __( 'Company name is required to checkout.', 'dichiara' ),
'slug' => 'company'
);
return $requirements;
}
function dichiara_itex_get_customer_phone( $customer_id = false ) {
$phone = it_exchange_get_customer_data( 'phone', $customer_id );
return apply_filters( 'it_exchange_get_customer_phone', $phone, $customer_id );
}
function dichiara_itex_get_customer_company( $customer_id = false ) {
$company = it_exchange_get_customer_data( 'company', $customer_id );
return apply_filters( 'it_exchange_get_customer_company', $company, $customer_id );
}
<?php
if( ! class_exists( 'IT_Theme_API_Registration' ) || class_exists( 'IT_Theme_API_Dichiara_registration' ) )
return;
class IT_Theme_API_Dichiara_registration extends IT_Theme_API_Registration {
/**
* API context
* @var string $_context
* @since 0.4.0
*/
private $_context = 'dichiara_registration';
/**
* Constructor
*
* @since 0.4.0
* @todo get working for admins looking at other users profiles
* @return void
*/
function __construct() {
$this->_tag_map['company'] = 'company';
$this->_tag_map['phone'] = 'phone';
}
/**
* Outputs the customer's phone data
*
* @since 0.4.0
* @return string
*/
function phone( $options=array() ) {
$defaults = array(
'format' => 'html',
'label' => __( 'Phone Number', 'it-l10n-ithemes-exchange' ),
);
$options = ITUtility::merge_defaults( $options, $defaults );
$field_id = 'phone';
$field_name = $field_id;
switch( $options['format'] ) {
case 'field-id':
$output = $field_id;
case 'field-name':
$output = $field_name;
case 'label':
$output = esc_attr( $options['label'] );
case 'html':
default:
$output = '<label for="' . $field_id . '">' . esc_attr( $options['label'] ) . '</label>';
$output .= '<input type="text" id="' . $field_id . '" name="' . $field_name . '" value="" />';
}
return $output;
}
/**
* Outputs the customer's company data
*
* @since 0.4.0
* @return string
*/
function company( $options=array() ) {
$defaults = array(
'format' => 'html',
'label' => __( 'Company Name', 'it-l10n-ithemes-exchange' ),
);
$options = ITUtility::merge_defaults( $options, $defaults );
$field_id = 'company';
$field_name = $field_id;
switch( $options['format'] ) {
case 'field-id':
$output = $field_id;
case 'field-name':
$output = $field_name;
case 'label':
$output = esc_attr( $options['label'] );
case 'html':
default:
$output = '<label for="' . $field_id . '">' . esc_attr( $options['label'] ) . '</label>';
$output .= '<input type="text" id="' . $field_id . '" name="' . $field_name . '" value="" />';
}
return $output;
}
}
<!-- company field -->
<label for="company">Company Name</label>
<input type="text" id="company" name="company" value="">
<!-- phone field -->
<label for="phone">Phone Number</label>
<input type="text" id="phone" name="phone" value="">
<?php do_action( 'it_exchange_content_registration_before_phone_element' ); ?>
<div class="it-exchange-registration-phone">
<?php it_exchange( 'dichiara_registration', 'phone' ); ?>
</div>
<?php do_action( 'it_exchange_content_registration_after_phone_element' ); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment