Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xlplugins/234d605ffc77c49d4473175601ece5ed to your computer and use it in GitHub Desktop.
Save xlplugins/234d605ffc77c49d4473175601ece5ed to your computer and use it in GitHub Desktop.
Checkout: City, state, postcode label translate based on checkout page IDs
class WFACP_Change_City_State_Postcode_fields {
protected $labels = [];
protected $id = [9883]; // can multiple with comma saperated xxx,yyy
public function __construct() {
$this->labels = [
'postcode' => 'Code Postal',
'state' => 'Région',
'city' => 'Ville',
];
add_filter( 'woocommerce_get_country_locale', [ $this, 'unset_fields' ] );
add_filter( 'woocommerce_get_country_locale_base', [ $this, 'update_address_fields' ] );
}
public function update_address_fields( $fields ) {
if(!in_array(get_the_ID(),$this->id)){
return $fields;
}
if ( ! is_array( $fields ) || count( $fields ) === 0 ) {
return $fields;
}
foreach ( $fields as $key => $value ) {
if ( array_key_exists( $key, $this->labels ) ) {
$fields[ $key ]['label'] = $this->labels[ $key ];
}
}
return $fields;
}
public function unset_fields( $locals ) {
if(!in_array(get_the_ID(),$this->id)){
return $locals;
}
if ( ! is_array( $locals ) || count( $locals ) === 0 ) {
return $locals;
}
foreach ( $locals as $key => $value ) {
if ( isset( $locals[ $key ]['postcode']['label'] ) ) {
$locals[ $key ]['postcode']['label'] = $this->labels['postcode'];
}
if ( isset( $locals[ $key ]['state']['label'] ) ) {
$locals[ $key ]['state']['label'] = $this->labels['state'];
}
if ( isset( $locals[ $key ]['city']['label'] ) ) {
$locals[ $key ]['city']['label'] = $this->labels['city'];
}
}
return $locals;
}
}
new WFACP_Change_City_State_Postcode_fields();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment