1. Home
  2. WC Vendors Pro
  3. How To
  4. How to change the store address country for all vendors
  1. Home
  2. WC Vendors Pro
  3. Code Snippets
  4. How to change the store address country for all vendors

How to change the store address country for all vendors

If you want to change the store address country for all vendors in the Marketplace, you can do so by adding the following code snippet to your theme’s functions.php file

/**
 * Set the vendor's store country to us.
 */
function wcv_set_vendor_country() {
	if ( function_exists( 'set_time_limit' ) ) {
		set_time_limit( 0 );
	}
	$vendor_ids = get_users(
		array(
			'role__in' => array(
				'vendor',
				'pending_vendor',
			),
			'number'   => -1,
			'fields'   => 'ids',
		)
	);

	if ( count( $vendor_ids ) === 0 ) {
		return;
	}

	foreach ( $vendor_ids as $vendor_id ) {
		update_user_meta( $vendor_id, '_wcv_store_country', 'US' );
	}
}

There are two ways to invoke/call the function just added above. The first one is by hooking it to WordPress’s action hooks, the second one is to define it as a WP_CLI command that can be executed any time.

If you prefer to hook it into WordPress’s action hooks then simply add the line below just under the last closing curly bracket in the code above.

add_action( 'wp', 'wcv_set_vendor_country' );

Note that the code will be called each time WordPress is loaded and that may cause unnecessary processing especially when all addresses have been updated. To avoid this you have to uncomment the last line and comment it back when you need to update new accounts.

Another method is to define the function as a WP_CLI command which can be done by adding the code below just under the last closing curly brace in the coded added above.

if ( defined( 'WP_CLI' ) ) {
	WP_CLI::add_command( 'vendors setcountry', 'wcv_set_vendor_country' );
}

Once you have this you can run it in the command line as follows:

wp vendors setcountry

Note: This method is for users who know how to connect to a WordPress site to run WP_CLI Commands.

It is important that you add this code snippet in a child theme to avoid losing this change when the theme is updated. If you are not using a child theme you should create a new file in ‘wp-content/mu-plugins/change-vendor-address.php‘ and add the code snippet in that file, do not forget to add the opening PHP tag if this is a new file or the tag does not exist.

actions

Updated on February 20, 2020

Was this article helpful?