Last active
February 20, 2021 01:16
-
-
Save yeriepiscesa/c235bb5de5012e91cf7c7453b3ac2e5b to your computer and use it in GitHub Desktop.
WooCommerce custom checkout fields
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
<?php | |
class SolusiPress_Custom_Woo_Checkout { | |
public function __construct() { | |
add_filter( 'woocommerce_default_address_fields', array( $this, 'woo_address_fields' ), 50, 1 ); | |
add_filter( 'woocommerce_checkout_fields', array( $this, 'woo_checkout_fields' ) ); | |
// first & last name merge | |
add_filter( 'default_checkout_billing_first_name', array( $this, 'first_name_default_value' ), 10, 2 ); | |
add_filter( 'default_checkout_shipping_first_name', array( $this, 'first_name_default_value' ), 10, 2 ); | |
add_filter( 'woocommerce_checkout_posted_data', array( $this, 'checkout_full_name_posted_data' ), 11 ); | |
} | |
public function woo_address_fields( $fields ) { | |
unset( $fields['company'] ); | |
unset( $fields['postcode'] ); | |
// merge first name & last name | |
$fields['first_name']['class'][0] = 'form-row-wide'; | |
$fields['first_name']['label'] = 'Full Name'; | |
unset( $fields['last_name'] ); | |
// swap field position | |
$fields['address_1']['priority'] = 60; | |
$fields['address_2']['priority'] = 50; | |
//unset($fields['address_2']); # uncomment to remove address_2 | |
return $fields; | |
} | |
public function woo_checkout_fields( $fields ) { | |
/* remove email or phone */ | |
unset( $fields['billing']['billing_phone'] ); | |
//unset( $fields['billing']['billing_email'] ); | |
return $fields; | |
} | |
public function first_name_default_value( $value, $input ) { | |
$checkout = WC_Checkout::instance(); | |
$ln = $checkout->get_value( str_replace( 'first', 'last', $input ) ); | |
return trim( $value . ' ' . $ln ); | |
return $value; | |
} | |
private function get_last_name( $full_name ) { | |
unset( $full_name[0] ); | |
return implode( ' ', $full_name ); | |
} | |
function checkout_full_name_posted_data( $data ) { | |
if( isset( $data['billing_first_name'] ) ) { | |
$full_name = explode( ' ', $data['billing_first_name'] ); | |
$data['billing_first_name'] = $full_name[0]; | |
$data['billing_last_name'] = $this->get_last_name( $full_name ); | |
} | |
if( isset( $data['shipping_first_name'] ) ) { | |
$full_name = explode( ' ', $data['shipping_first_name'] ); | |
$data['shipping_first_name'] = $full_name[0]; | |
$data['shipping_last_name'] = $this->get_last_name( $full_name ); | |
} | |
return $data; | |
} | |
} | |
new SolusiPress_Custom_Woo_Checkout(); |
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
<?php | |
public function woo_address_fields( $fields ) { | |
unset( $fields['company'] ); | |
unset( $fields['postcode'] ); | |
// merge first name & last name | |
$fields['first_name']['class'][0] = 'form-row-wide'; | |
$fields['first_name']['label'] = 'Full Name'; | |
unset( $fields['last_name'] ); | |
// swap field position | |
$fields['address_1']['priority'] = 60; | |
$fields['address_2']['priority'] = 50; | |
//unset($fields['address_2']); # uncomment to remove address_2 | |
return $fields; | |
} | |
public function woo_checkout_fields( $fields ) { | |
/* remove email or phone */ | |
unset( $fields['billing']['billing_phone'] ); | |
//unset( $fields['billing']['billing_email'] ); | |
return $fields; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment