List Square Locations

This standalone script connects to Square Locations API via cURL and returns existing locations on your account. This is helpful when the WooCommerce Square extension does not retrieve the locations, and web hosts claim that cURL is working as expected

<?php

$ch = curl_init();

curl_setopt( $ch, CURLOPT_URL, 'https://connect.squareup.com/v2/locations' );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer PRODUCTION_ACCESS_TOKEN', 'Accept: application/json', 'Square-Version: 2021-04-21' ) );
curl_setopt( $ch, CURLOPT_TIMEOUT, 30 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

if ( curl_exec( $ch ) === false ) {
	echo 'Curl error: ' . curl_error( $ch );
} else {
	$data = json_decode( curl_exec( $ch ) );
	echo '<pre>';
	print_r( $data );
	echo '</pre>';
}

curl_close( $ch );

USAGE:

This is not a WordPress plugin, so please upload this script to the root directory of the site and access it directly: https://example.com/square-list-locations.php

For Production environment:

  • Replace PRODUCTION_ACCESS_TOKEN with the ACCESS token from your app at https://developer.squareup.com/apps/
  • You need to access the site > Open App > Toggle to production creds > Find the token here
The square production access token

For Sandbox environment:

  • If you’re testing with Sandbox, you’ll want to replace https://connect.squareup.com/v2/locations with https://connect.squareupsandbox.com/v2/locations
  • And also replace PRODUCTION_ACCESS_TOKEN in the script, with the Sandbox Access token instead
  • You can use the sandbox access token from Square settings page: WooCommerce > Settings > Square > Settings
  • Or, you can also use the creds from https://developer.squareup.com/apps/ and grab the sandbox access token from there

RESULTS:

If things work fine, you’ll see a list of Square locations and their details. If not, you’ll see the cURL error.

All cURL errors start with Curl error:. If you don’t see that, it means the error you’re seeing is returned by Square.

Source/Credits: https://developer.squareup.com/docs/locations-api

Leave a Reply

Your email address will not be published.