Skip to content

Instantly share code, notes, and snippets.

@artikus11
Last active January 23, 2023 22:20
Show Gist options
  • Save artikus11/35f7b3fb387fe15caf30ab44c4d9ecfe to your computer and use it in GitHub Desktop.
Save artikus11/35f7b3fb387fe15caf30ab44c4d9ecfe to your computer and use it in GitHub Desktop.
Woocommerce. Изменение HTML полей на странице оформления заказа (чекаут)
add_filter( 'woocommerce_form_field', 'art_change_woocommerce_form_field', 10, 4 );
/**
* Изменение хтмл полей
*
* @param $field
* @param $key
* @param $args
* @param $value
*
* @testedwith WooCommerce 7.0
* @return mixed|string
* @author Artem Abramovich
*
*/
function art_change_woocommerce_form_field( $field, $key, $args, $value ) {
if ( 'country' === $args['type']
|| 'state' === $args['type']
|| 'hidden' === $args['type']
|| 'checkbox' === $args['type']
|| 'select' === $args['type']
) {
return $field;
}
if ( is_string( $args['class'] ) ) {
$args['class'] = [ $args['class'] ];
}
$required = '';
if ( $args['required'] ) {
$args['class'][] = 'validate-required';
$required = '&nbsp;<abbr class="required" title="' . esc_attr__( 'required', 'woocommerce' ) . '">*</abbr>';
}
if ( is_string( $args['label_class'] ) ) {
$args['label_class'] = [ $args['label_class'] ];
}
if ( is_null( $value ) ) {
$value = $args['default'];
}
// Custom attribute handling.
$custom_attributes = [];
$args['custom_attributes'] = array_filter( (array) $args['custom_attributes'], 'strlen' );
if ( $args['maxlength'] ) {
$args['custom_attributes']['maxlength'] = absint( $args['maxlength'] );
}
if ( ! empty( $args['autocomplete'] ) ) {
$args['custom_attributes']['autocomplete'] = $args['autocomplete'];
}
if ( true === $args['autofocus'] ) {
$args['custom_attributes']['autofocus'] = 'autofocus';
}
if ( $args['description'] ) {
$args['custom_attributes']['aria-describedby'] = $args['id'] . '-description';
}
if ( ! empty( $args['custom_attributes'] ) && is_array( $args['custom_attributes'] ) ) {
foreach ( $args['custom_attributes'] as $attribute => $attribute_value ) {
$custom_attributes[] = esc_attr( $attribute ) . '="' . esc_attr( $attribute_value ) . '"';
}
}
if ( ! empty( $args['validate'] ) ) {
foreach ( $args['validate'] as $validate ) {
$args['class'][] = 'validate-' . $validate;
}
}
$label_id = $args['id'];
$sort = $args['priority'] ? : '';
$field_container = '<p class="form-row %1$s" id="%2$s" data-priority="' . esc_attr( $sort ) . '">%3$s</p>';
switch ( $args['type'] ) {
case 'textarea':
$field = sprintf(
'<textarea name="%s" class="input-text %s" id="%s" placeholder="%s" %s%s%s>%s</textarea>',
esc_attr( $key ),
esc_attr( implode( ' ', $args['input_class'] ) ),
esc_attr( $args['id'] ),
esc_attr( $args['placeholder'] ),
empty( $args['custom_attributes']['rows'] ) ? ' rows="2"' : '',
empty( $args['custom_attributes']['cols'] ) ? ' cols="5"' : '',
implode( ' ', $custom_attributes ),
esc_textarea( $value )
);
break;
case 'text':
case 'password':
case 'datetime':
case 'datetime-local':
case 'date':
case 'month':
case 'time':
case 'week':
case 'number':
case 'email':
case 'url':
case 'tel':
$field = sprintf(
'<input type="%s" class="input-text %s" name="%s" id="%s" placeholder="%s" value="%s" %s />',
esc_attr( $args['type'] ),
esc_attr( implode( ' ', $args['input_class'] ) ),
esc_attr( $key ),
esc_attr( $args['id'] ),
esc_attr( $args['placeholder'] ),
esc_attr( $value ),
implode( ' ', $custom_attributes )
);
break;
case 'radio':
$label_id .= '_' . current( array_keys( $args['options'] ) );
$radio = '';
if ( ! empty( $args['options'] ) ) {
foreach ( $args['options'] as $option_key => $option_text ) {
$radio_input = sprintf(
'<input type="radio" class="input-radio %s" value="%s" name="%s" %s id="%s_%s"%s />',
esc_attr( implode( ' ', $args['input_class'] ) ),
esc_attr( $option_key ),
esc_attr( $key ),
implode( ' ', $custom_attributes ),
esc_attr( $args['id'] ), esc_attr( $option_key ),
checked( $value, $option_key, false )
);
$radio_label = sprintf(
'<label for="%s_%s" class="radio %s">%s</label>',
esc_attr( $args['id'] ),
esc_attr( $option_key ),
implode( ' ', $args['label_class'] ),
esc_html( $option_text )
);
$radio .= $radio_input . $radio_label;
}
$field = $radio;
}
break;
}
if ( ! empty( $field ) ) {
$field_html = '';
$field_description = '';
if ( $args['description'] ) {
$field_description = sprintf(
'<span class="tooltips__item"><span class="tooltips description" id="%s-description" aria-hidden="true">%s</span>',
esc_attr( $args['id'] ),
wp_kses_post( $args['description'] )
);
}
if ( $args['label'] ) {
$field_html .= sprintf(
'<label for="%s" class="integration_form_field_head %s">%s%s%s</label>',
esc_attr( $label_id ),
esc_attr( implode( ' ', $args['label_class'] ) ),
wp_kses_post( $args['label'] ),
$required,
wp_kses_post( $field_description )
);
}
$field_html .= sprintf( '<span class="woocommerce-input-wrapper">%s</span>', $field );
$container_class = esc_attr( implode( ' ', $args['class'] ) );
$container_id = esc_attr( $args['id'] ) . '_field';
$field = sprintf( $field_container, $container_class, $container_id, $field_html );
}
return $field;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment