• Resolved vap93

    (@vap93)


    I’d like to display the billing address on a custom page on my site. This page contains a list of users with some of their registered information, but I would also like to display address information these users…

    To do this, I created a custom template with this code:

    
    <?php
    
    /* Template Name: List Users */
    
    get_header(); ?>
    
    <div id="primary">
    	<main id="main" class="site-main" role="main">
    		<div id="pageTitle">
    			<h2>Onde Comprar</h2>
    		</div>
    		<div id="pageText">
    			
    				A Editora Conclave distribui e tem parcerias com vários lojistas pelo Brasil, você pode localizar a loja mais próxima a você ou se preferir poderá comprar na nossa loja virtual, <a href="https://conclaveweb.com.br/loja/">clicando aqui</a>.
    			
    
    		</div>
    		<div id="searchContainer"></div>
    		<ul id="userListContainer">
    			<?php
    			// WP_User_Query arguments
    			$args = array(
    				'meta_key' => 'first_name',
    				'role' => 'shopkeeper',
    				//'search'         => 'first_name',
    				'search_columns' => array('first_name', 'city', 'state'),
    				'order' => 'ASC',
    				'orderby' => 'meta_value',
    				'fields' => 'all_with_meta',
    			);
    
    			// The User Query
    			$users = new WP_User_Query($args);
    
    			// User Loop
    			if (!empty($users->results)) {
    				foreach ($users->results as $user) {
    					echo
    					//get_author_posts_url( $user->ID ) . the_author_meta( 'user_url', $user->ID ) .
    					'<li>' .
    						'<a href="' . esc_url(get_author_posts_url($user->ID)) . '">' .
    							'<div id="userImage">' . get_avatar($user->ID) . '</div>' .
    							'<div id="userInfo">' . esc_attr($user->first_name) . ' ' . esc_attr($user->last_name) . '<br>' .
    								'CEP: ' . esc_attr($user->post_code) . ' — ' . esc_attr($user->city) . ', ' . esc_attr($user->state) . '<br>' .
    						'</a>' .
    							'<a href="' . esc_attr($user->user_url) . '">' . esc_attr($user->user_url) . '</a>' .
    							'</div>' .
    					'</li>';
    				}
    			} else {
    				echo 'Nenhum usuário encontrado.';
    			}
    			?>
    		</ul>
    
    	</main><!-- #main -->
    </div><!-- #primary -->
    
    <?php
    //do_action( 'ekommart_sidebar' );
    get_footer();
    ?>
    

    I tryed display the address, but don`t work. I believe that dont work because this informations does not come by “WP_User_Query”, but do not know how bring them.

    Any help will be welcome! Thank’s!!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Mirko P.

    (@rainfallnixfig)

    Hi there,

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook Community group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers.

    Thread Starter vap93

    (@vap93)

    Hi! Thanks for your reply!

    I received the solution on another forum. So here’s the functional code

    <?php
    			// WP_User_Query arguments
    			$args = array(
    				'meta_key' => 'first_name',
    				'role' => 'shopkeeper',
    				//'search'         => 'first_name',
    				'search_columns' => array('first_name', 'city', 'state'),
    				'order' => 'ASC',
    				'orderby' => 'meta_value',
    				'fields' => 'all_with_meta',
    			);
    
    			// The User Query
    			$users = new WP_User_Query($args);
    
    			// User Loop
    			if (!empty($users->results)) {
    				foreach ($users->results as $user) {
    					// Get an instance of the WC_Customer Object from the user ID
    					$customer = new WC_Customer( $user->ID );
    
    					$username     = $customer->get_username(); // Get username
    					$user_email   = $customer->get_email(); // Get account email
    					$first_name   = $customer->get_first_name();
    					$last_name    = $customer->get_last_name();
    					$display_name = $customer->get_display_name();
    
    					// Customer billing information details (from account)
    					$billing_first_name = $customer->get_billing_first_name();
    					$billing_last_name  = $customer->get_billing_last_name();
    					$billing_company    = $customer->get_billing_company();
    					$billing_address_1  = $customer->get_billing_address_1();
    					$billing_address_2  = $customer->get_billing_address_2();
    					$billing_city       = $customer->get_billing_city();
    					$billing_state      = $customer->get_billing_state();
    					$billing_postcode   = $customer->get_billing_postcode();
    					$billing_country    = $customer->get_billing_country();
    
    					// Customer shipping information details (from account)
    					$shipping_first_name = $customer->get_shipping_first_name();
    					$shipping_last_name  = $customer->get_shipping_last_name();
    					$shipping_company    = $customer->get_shipping_company();
    					$shipping_address_1  = $customer->get_shipping_address_1();
    					$shipping_address_2  = $customer->get_shipping_address_2();
    					$shipping_city       = $customer->get_shipping_city();
    					$shipping_state      = $customer->get_shipping_state();
    					$shipping_postcode   = $customer->get_shipping_postcode();
    					$shipping_country    = $customer->get_shipping_country();
    
    					echo
    					'<li>' .
    						'<a href="' . esc_url(get_author_posts_url($user->ID)) . '">' .
    							'<div id="userImage">' . get_avatar($user->ID) . '</div>' .
    							'<div id="userInfo">' . esc_attr($user->first_name) . ' ' . esc_attr($user->last_name) . '<br>' .
    								'CEP: ' . esc_attr($customer->billing_postcode) . ' — ' . esc_attr($customer->billing_city) . ', ' . esc_attr($customer->billing_state) . '<br>' .
    						'</a>' .
    							'<a href="' . esc_attr($user->user_url) . '">' . esc_attr($user->user_url) . '</a>' .
    							'</div>' .
    					'</li>';
    				}
    			} else {
    				echo 'Nenhum usuário encontrado.';
    			}
    			?>
    • This reply was modified 2 years, 7 months ago by vap93.
    • This reply was modified 2 years, 7 months ago by vap93.
    Mirko P.

    (@rainfallnixfig)

    Great! Good to know that you were able to achieve your goal and thank you for sharing the code so that other users of the WooCommerce Community can benefit from it.

    If you have other questions open a new topic.

    Cheers.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display woocommerce billing address on a custom page’ is closed to new replies.