• Resolved broder

    (@broder)


    Hi,

    Is it possible to change the html output on the checkout? I would like to convert the <select> to regular divs to look like to tiles.

    Thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Matt Harrison

    (@matt-h-1)

    Hi,

    To do something like that it would require removing the filter that creates the select input calling this function: https://github.com/hallme/woo-address-book/blob/dec8ed350a109cd20ec9153ac49208def74d655d/includes/class-wc-address-book.php#L613-L652

    Using this:
    remove_filter( 'woocommerce_checkout_fields', array( WC_Address_Book::get_instance(), 'checkout_address_select_field' ), 9999, 1 );

    And then you would need to write your own custom function to do what you want instead of the checkout_address_select_field function.

    Thread Starter broder

    (@broder)

    Hi,

    Sorry for my late reply. I’ve been trying to test

    remove_filter( 'woocommerce_checkout_fields', array( WC_Address_Book::get_instance(), 'checkout_address_select_field' ), 9999, 1 );

    and

    add_filter( 'woocommerce_checkout_fields', array( WC_Address_Book::get_instance(), 'checkout_address_custom_function' ), 9999, 1 );

    With the same github function in my functions.php to see if that would work, but it gives an error. Should I edit the template file directly in order to do this?

    Thanks

    • This reply was modified 2 years, 6 months ago by broder.
    Plugin Author Matt Harrison

    (@matt-h-1)

    The remove filter is fine, but to target a custom function in your functions.php you would run the add_filter like:

    
    add_filter( 'woocommerce_checkout_fields', 'checkout_address_custom_function', 9999, 1 );
    

    since it is not part of the WC_Address_Book class.

    Thread Starter broder

    (@broder)

    Really sorry and appreciate the help but this also returns error, I now have this:

    add_filter( 'woocommerce_checkout_fields', 'checkout_address_custom_function', 9999, 1 );
    
    function checkout_address_custom_function( $fields ) {
    
    // Github function 
    
    return $fields;
    
    }
    • This reply was modified 2 years, 6 months ago by broder.
    • This reply was modified 2 years, 6 months ago by broder.
    Plugin Author Matt Harrison

    (@matt-h-1)

    What is the actual error? My guess would be because the function uses a lot of $this so you would have to change those. This should work.

    
    add_filter( 'woocommerce_checkout_fields', 'checkout_address_custom_function', 9999, 1 );
    
    function checkout_address_custom_function( $fields ) {
    	if ( is_user_logged_in() ) {
    		$wc_address_book_plugin = WC_Address_Book::get_instance();
    		foreach ( $fields as $type => $address_fields ) {
    			if ( ( 'billing' === $type && $wc_address_book_plugin->get_wcab_option( 'billing_enable' ) === true ) || ( 'shipping' === $type && $wc->get_wcab_option( 'shipping_enable' ) === true ) ) {
    				$address_book = $wc_address_book_plugin->get_address_book( null, $type );
    
    				$address_selector                            = array();
    				$address_selector[ $type . '_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 ) {
    					$default_to_new_address = $wc_address_book_plugin->get_wcab_option( $type . '_default_to_new_address', 'no' );
    
    					foreach ( $address_book as $name => $address ) {
    						if ( ! empty( $address[ $name . '_address_1' ] ) ) {
    							$address_selector[ $type . '_address_book' ]['options'][ $name ] = $wc_address_book_plugin->address_select_label( $address, $name );
    						}
    					}
    
    					$address_selector[ $type . '_address_book' ]['options']['add_new'] = __( 'Add New Address', 'woo-address-book' );
    
    					if ( true === $default_to_new_address ) {
    						$address_selector[ $type . '_address_book' ]['default'] = 'add_new';
    					} else {
    						$address_selector[ $type . '_address_book' ]['default'] = $type;
    					}
    
    					$fields[ $type ] = $address_selector + $fields[ $type ];
    				}
    			}
    		}
    	}
    
    	return $fields;
    }
    
    
    Thread Starter broder

    (@broder)

    Hi Matt,

    Unfortunately this also gives error (php fatal error on checkout).

    I think it’s not calling the right variable e.g.

    $wc_address_book_plugin = WC_Address_Book::get_instance();

    But not sure

    Plugin Author Matt Harrison

    (@matt-h-1)

    I just tested it out. That part was fine, just a typo in one of the variables when I was using it.

    Here is the updated version of everything.

    
    remove_filter( 'woocommerce_checkout_fields', array( WC_Address_Book::get_instance(), 'checkout_address_select_field' ), 9999, 1 );
    add_filter( 'woocommerce_checkout_fields', 'checkout_address_custom_function', 9999, 1 );
    
    function checkout_address_custom_function( $fields ) {
    	if ( is_user_logged_in() ) {
    		$wc_address_book_plugin = WC_Address_Book::get_instance();
    		foreach ( $fields as $type => $address_fields ) {
    			if ( ( 'billing' === $type && $wc_address_book_plugin->get_wcab_option( 'billing_enable' ) === true ) || ( 'shipping' === $type && $wc_address_book_plugin->get_wcab_option( 'shipping_enable' ) === true ) ) {
    				$address_book = $wc_address_book_plugin->get_address_book( null, $type );
    
    				$address_selector                            = array();
    				$address_selector[ $type . '_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 ) {
    					$default_to_new_address = $wc_address_book_plugin->get_wcab_option( $type . '_default_to_new_address', 'no' );
    
    					foreach ( $address_book as $name => $address ) {
    						if ( ! empty( $address[ $name . '_address_1' ] ) ) {
    							$address_selector[ $type . '_address_book' ]['options'][ $name ] = $wc_address_book_plugin->address_select_label( $address, $name );
    						}
    					}
    
    					$address_selector[ $type . '_address_book' ]['options']['add_new'] = __( 'Add New Address', 'woo-address-book' );
    
    					if ( true === $default_to_new_address ) {
    						$address_selector[ $type . '_address_book' ]['default'] = 'add_new';
    					} else {
    						$address_selector[ $type . '_address_book' ]['default'] = $type;
    					}
    
    					$fields[ $type ] = $address_selector + $fields[ $type ];
    				}
    			}
    		}
    	}
    
    	return $fields;
    }
    
    Thread Starter broder

    (@broder)

    Indeed works now, thank you for your effort and help.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Change the select element on checkout’ is closed to new replies.