Created
March 9, 2018 16:29
-
-
Save woogists/3e591e9615b4cee35330eaf6e7746278 to your computer and use it in GitHub Desktop.
[Checkout field editor] Add custom fields to WebHooks API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* WooCommerce - extends woocommerce checkout field editor */ | |
/* adds additional display areas (Admin edit user screen and WooCommerce WebHooks */ | |
/** | |
* add custom checkout fields to user profile on admin edit screen | |
* | |
* @param mixed $payload | |
*/ | |
function filter_woocommerce_admin_profile__custom_checkout_fields( $payload ) { | |
if ( class_exists( 'WC_Checkout_Field_Editor' ) ) { | |
$fieldgroups = array( 'billing'=>'billing', 'shipping'=>'shipping', 'additional'=>'additional' ); | |
foreach ($fieldgroups as $fieldgroup => $payload_group) { | |
$fakeorder = new WC_Order(); | |
foreach (wc_get_custom_checkout_fields($fakeorder , array( $fieldgroup ) ) as $field_name => $field_options ) { | |
//basic validation, is the field enabled | |
if ( !isset($field_options['enabled'] ) || ( $field_options['enabled'] != true ) ) | |
continue; | |
//is the field for profile | |
if ( (!isset($field_options['display_options']) ) || ( !is_array($field_options['display_options'])) || ( !in_array('profile', $field_options['display_options'])) ) | |
continue; | |
//passed all checks, add to payload | |
/*'fields' => array( | |
'billing_first_name' => array( | |
'label' => __( 'First name', 'woocommerce' ), | |
'description' => '' | |
),*/ | |
$payload_value = array( | |
'label' => $field_options['label'], | |
'description' => '' | |
); | |
if ($field_options['type'] == 'select') { | |
$payload_value['type'] = 'select'; | |
$payload_value['options'] = $field_options['options']; | |
} | |
$payload[$payload_group]['fields'][$field_name] = $payload_value; | |
} | |
} | |
return $payload; | |
} | |
}; | |
/** | |
* filter add for above function | |
*/ | |
add_filter( 'woocommerce_customer_meta_fields', 'filter_woocommerce_admin_profile__custom_checkout_fields', 10, 1 ); | |
/** | |
* filter action to add custom checkout fields to the webhook payload | |
* | |
* @param mixed $payload | |
* @param mixed $resource | |
* @param mixed $resource_id | |
* @param mixed $this_id | |
*/ | |
function filter_woocommerce_webhook_payload__custom_checkout_fields( $payload, $resource, $resource_id, $this_id ) { | |
if ( class_exists( 'WC_Checkout_Field_Editor' ) ) { | |
$fieldgroups = array( 'billing'=>'billing_address', 'shipping'=>'shipping_address', 'additional'=>'additional' ); | |
if ($resource != 'order' || empty($resource_id)) { | |
return $payload; | |
} | |
$order = wc_get_order( $resource_id ); | |
foreach ($fieldgroups as $fieldgroup => $payload_group) { | |
foreach (wc_get_custom_checkout_fields($order , array( $fieldgroup ) ) as $field_name => $field_options ) { | |
//basic validation, is the field enabled | |
if ( !isset($field_options['enabled'] ) || ( $field_options['enabled'] != true ) ) | |
continue; | |
//is the field for webhooks | |
if ( (!isset($field_options['display_options']) ) || ( !is_array($field_options['display_options'])) || ( !in_array('webhook', $field_options['display_options'])) ) | |
continue; | |
//passed all checks, add to payload | |
$payload_value = get_post_meta( $resource_id, $field_name, true ); | |
$payload['order'][$payload_group][$field_name] = $payload_value; | |
if ( is_array($payload['order']) ) { | |
$payload['order']['customer'][$payload_group][$field_name] = $payload_value; | |
} | |
} | |
} | |
return $payload; | |
} | |
}; | |
/** | |
* filter add for above function | |
*/ | |
add_filter( 'woocommerce_webhook_payload', 'filter_woocommerce_webhook_payload__custom_checkout_fields', 10, 4 ); | |
/** | |
* filter action to add "profile" and "webhook" to selectable display options in custom options editing screen | |
* | |
* @param mixed $options | |
*/ | |
function filter_woocommerce_custom_checkout_display_options__customer_checkout_fields_profile($options) { | |
$options['profile'] = __( 'Admin Profile', 'woocommerce-checkout-field-editor' ); | |
$options['webhook'] = __( 'Webhooks', 'woocommerce-checkout-field-editor' ); | |
return $options; | |
} | |
/** | |
* filter add for above function | |
*/ | |
add_filter ('woocommerce_custom_checkout_display_options', 'filter_woocommerce_custom_checkout_display_options__customer_checkout_fields_profile', 10, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
WHERE SHOUL I ADD THIS CODE? IN HERE I WILL HAVE TO MODFY TO CONNECT TO AS CUSTOM POST TYPE?