-
-
Save wordpressvn/c3096e77bcb6789cd9e39bff39ce317a to your computer and use it in GitHub Desktop.
split street/housenumber woocommerce
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
/* | |
Description: unset WooCommerce billing & shipping address_1 field in checkout in favor for 2 new custom fields street and houseno | |
Author: Code Agency | |
Author URI: https://codeagency.be | |
*/ | |
function codeagency_remove_checkout_fields( $fields ) { | |
// Billing fields | |
// unset( $fields['billing']['billing_company'] ); | |
// unset( $fields['billing']['billing_email'] ); | |
// unset( $fields['billing']['billing_phone'] ); | |
// unset( $fields['billing']['billing_state'] ); | |
// unset( $fields['billing']['billing_first_name'] ); | |
// unset( $fields['billing']['billing_last_name'] ); | |
unset( $fields['billing']['billing_address_1'] ); | |
// unset( $fields['billing']['billing_address_2'] ); | |
// unset( $fields['billing']['billing_city'] ); | |
// unset( $fields['billing']['billing_postcode'] ); | |
// Shipping fields | |
// unset( $fields['shipping']['shipping_company'] ); | |
// unset( $fields['shipping']['shipping_phone'] ); | |
// unset( $fields['shipping']['shipping_state'] ); | |
// unset( $fields['shipping']['shipping_first_name'] ); | |
// unset( $fields['shipping']['shipping_last_name'] ); | |
unset( $fields['shipping']['shipping_address_1'] ); | |
// unset( $fields['shipping']['shipping_address_2'] ); | |
// unset( $fields['shipping']['shipping_city'] ); | |
// unset( $fields['shipping']['shipping_postcode'] ); | |
// Order fields | |
// unset( $fields['order']['order_comments'] ); | |
return $fields; | |
} | |
add_filter( 'woocommerce_checkout_fields', 'codeagency_remove_checkout_fields' ); | |
/* | |
Description: unset WooCommerce billing address_1 field in my account in favor for 2 new custom fields street and houseno | |
Author: Code Agency | |
Author URI: https://codeagency.be | |
*/ | |
function codeagency_override_billing_fields( $fields ) { | |
unset($fields['billing_address_1']); | |
return $fields; | |
} | |
add_filter( 'woocommerce_billing_fields' , 'codeagency_override_billing_fields' ); | |
/* | |
Description: unset WooCommerce shipping address_1 field in my account in favor for 2 new custom fields street and houseno | |
Author: Code Agency | |
Author URI: https://codeagency.be | |
*/ | |
function codeagency_override_shipping_fields( $fields ) { | |
unset($fields['shipping_address_1']); | |
return $fields; | |
} | |
add_filter( 'woocommerce_shipping_fields' , 'codeagency_override_shipping_fields' ); | |
/* | |
Description: WooCommerce checkout new billing custom fields street and houseno | |
Author: Code Agency | |
Author URI: https://codeagency.be | |
*/ | |
function codeagency_add_billing_fields( $fields ) { | |
$fields['billing_street'] = array( | |
'label' => __( 'Street' ), | |
'type' => 'text', | |
'class' => array( 'form-row-first' ), | |
'priority' => 55, | |
'required' => true, | |
); | |
$fields['billing_houseno'] = array( | |
'label' => __( 'House number' ), | |
'type' => 'text', | |
'class' => array( 'form-row-last' ), | |
'priority' => 56, | |
'required' => true, | |
); | |
return $fields; | |
} | |
add_filter( 'woocommerce_billing_fields', 'codeagency_add_billing_fields' ); | |
/* | |
Description: WooCommerce checkout new shipping custom fields street and houseno | |
Author: Code Agency | |
Author URI: https://codeagency.be | |
*/ | |
function codeagency_add_shipping_fields( $fields ) { | |
$fields['shipping_street'] = array( | |
'label' => __( 'Street' ), | |
'type' => 'text', | |
'class' => array( 'form-row-first' ), | |
'priority' => 55, | |
'required' => true, | |
); | |
$fields['shipping_houseno'] = array( | |
'label' => __( 'House number' ), | |
'type' => 'text', | |
'class' => array( 'form-row-last' ), | |
'priority' => 56, | |
'required' => true, | |
); | |
return $fields; | |
} | |
add_filter( 'woocommerce_shipping_fields', 'codeagency_add_shipping_fields' ); | |
/* | |
Description: WooCommerce checkout new billing custom fields street and houseno | |
Author: Code Agency | |
Author URI: https://codeagency.be | |
*/+/* | |
add_action( 'woocommerce_new_order', 'codeagency_merge_street_fields', 10, 1 ); | |
function codeagency_merge_street_fields( $order_id ) { | |
$streethouseno = get_post_meta( $order_id, 'billing_street', true ); | |
if( $streethouseno ){ | |
update_field( 'billing_address_1', $streethouseno, $order_id ); | |
} | |
} | |
*/ | |
/* | |
Description: WooCommerce update order meta data at checkout | |
Merge data from 2 new fields into native billing_address_1 field to preserve breaking conflicts for other operations and 3rd party plugins | |
Author: Code Agency | |
Author URI: https://codeagency.be | |
*/ | |
add_action( 'woocommerce_checkout_update_order_meta', 'codeagency_checkout_field_update_order_meta', 10, 2 ); | |
function codeagency_checkout_field_update_order_meta( $order_id ) { | |
$separator = ' '; // Separator for merged fields | |
// Billing fields | |
$billing_street = sanitize_text_field( $_POST['billing_street'] ); | |
$billing_houseno = sanitize_text_field( $_POST['billing_houseno'] ); | |
if( ! ( empty($billing_street) && empty($billing_houseno) ) ) | |
update_post_meta( $order_id, '_billing_address_1', $billing_street . $separator . $billing_houseno ); | |
$shipping_street = sanitize_text_field( $_POST['shipping_street'] ); | |
$shipping_houseno = sanitize_text_field( $_POST['shipping_houseno'] ); | |
if( ! ( empty($shipping_street) && empty($shipping_houseno) ) ) | |
update_post_meta( $order_id, '_shipping_address_1', $shipping_street . $separator . $shipping_houseno ); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment