We have an ecommerce website where we can't allow the users to edit their shipping address, but would like them to able to store multiple addresses from a drop down. I used the following code to make the shipping address read only and not editable, but when they select a different address from the drop down the readonly fields do not update. I tried the disabled attribute which worked visually, but we need the data to POST when placing an order. Is there any work around for this?
jQuery(document).ready(function ($) {
$("#shipping_last_name").attr("readonly", true);
});
/** * Adds the address book select to the checkout page. * * @since 1.0.0 * * @param array $fields An array of WooCommerce Shipping Address fields. * @return array */ public function shipping_address_select_field( $fields ) { if ( is_user_logged_in() ) { $address_book = $this->get_address_book();
$address_selector['address_book'] = array(
'type' => 'select',
'class' => array( 'form-row-wide', 'address_book' ),
'label' => __( 'Address Book', 'woo-address-book' ),
'order' => -1,
'priority' => -1,
);
if ( ! empty( $address_book ) && false !== $address_book ) {
foreach ( $address_book as $name => $address ) {
if ( ! empty( $address[ $name . '_address_1' ] ) ) {
$address_selector['address_book']['options'][ $name ] = $this->address_select_label( $address, $name );
}
}
$address_selector['address_book']['options']['add_new'] = __( 'Add New Address', 'woo-address-book' );
$fields['shipping'] = $address_selector + $fields['shipping'];
$fields['shipping']['custom_attributes'] = array('readonly'=>'readonly');
}
}
return $fields;
}
/**
* Adds the address book select to the checkout page.
*
* @since 1.0.0
*
* @param array $address An array of WooCommerce Shipping Address data.
* @param string $name Name of the address field to use.
* @return string
*/
public function address_select_label( $address, $name ) {
$label = '';
$address_nickname = get_user_meta( get_current_user_id(), $name . '_address_nickname', true );
if ( $address_nickname ) {
$label .= $address_nickname . ': ';
}
if ( ! empty( $address[ $name . '_first_name' ] ) ) {
$label .= $address[ $name . '_first_name' ];
}
if ( ! empty( $address[ $name . '_last_name' ] ) ) {
if ( ! empty( $label ) ) {
$label .= ' ';
}
$label .= $address[ $name . '_last_name' ];
}
if ( ! empty( $address[ $name . '_address_1' ] ) ) {
if ( ! empty( $label ) ) {
$label .= ', ';
}
$label .= $address[ $name . '_address_1' ];
}
if ( ! empty( $address[ $name . '_city' ] ) ) {
if ( ! empty( $label ) ) {
$label .= ', ';
}
$label .= $address[ $name . '_city' ];
}
if ( ! empty( $address[ $name . '_state' ] ) ) {
if ( ! empty( $label ) ) {
$label .= ', ';
}
$label .= $address[ $name . '_state' ];
}
return apply_filters( 'wc_address_book_address_select_label', $label, $address, $name );
}