This guide explains how to extend the functionality of the third-party plugin WooCommerce Print Address Labels to include store-specific information from our plugin Multi-Store Addons for WooCommerce. By adding this custom code to your theme’s functions.php
file, you can dynamically display additional address information related to specific stores within your address labels.
Feature Overview #
When using both WooCommerce Print Address Labels and Multi-Store Addons for WooCommerce, you may want to include store details (such as store name, address, phone, etc.) in the printed address labels. This can be achieved by using dynamic placeholders like [asl_title]
, [asl_city]
, [asl_phone]
, which automatically pull information from the Multi-Store Addons for WooCommerce plugin and insert it into the printed label.
How It Works #
- The placeholders in the address label template (e.g.,
[asl_title]
,[asl_city]
) are dynamically replaced with the corresponding data from the store assigned to the order. - If the store associated with the order contains a particular piece of information (like the store’s title or phone number), the placeholders are replaced with the actual data.
- This process is fully dynamic, allowing you to add custom store information to any WooCommerce address label.
Usage Instructions #
To add store-specific information to your address labels, follow these steps:
Add the following code to your theme’s functions.php
file:
add_filter('wpo_wclabel_address_format', 'custom_wpo_wclabel_address_format', 10, 2);
/**
* Customize the address format for WooCommerce address labels.
*
* @param string $custom_address_format The original address format.
* @param object $order The WooCommerce order object.
* @return string The modified address format.
*/
function custom_wpo_wclabel_address_format($custom_address_format, $order) {
// Get the store ID from the order meta (assigned by Multi-Store Addons for WooCommerce)
$asl_wc_store_id = $order->get_meta('asl-wc_store_id');
// When the store ID exists
if ($asl_wc_store_id) {
// Check if the Agile Store Locator helper method exists
if (method_exists("\AgileStoreLocator\Helper", 'get_store')) {
// Get the store object based on the store ID
$a_store = \AgileStoreLocator\Helper::get_store($asl_wc_store_id);
// If the store exists
if ($a_store) {
// Match all placeholders that start with [asl_ and end with ]
preg_match_all('/\[asl_(.*?)\]/', $custom_address_format, $matches);
// Loop through the matches and replace the placeholders with store data
foreach ($matches[0] as $key => $placeholder) {
// Extract the property name (e.g., 'title', 'city', etc.)
$property = $matches[1][$key];
// Check if the property exists in the store object
if (isset($a_store->$property)) {
// Replace the placeholder with the corresponding store property value
$custom_address_format = str_replace($placeholder, $a_store->$property, $custom_address_format);
} else {
// Optionally, remove the placeholder if the property doesn't exist
$custom_address_format = str_replace($placeholder, '', $custom_address_format);
}
}
}
}
}
// Return the modified address format with replaced placeholders
return $custom_address_format;
}
Customize the Address Format in Your Print Labels:
Now that the code is in place, you can use placeholders like the following in the address format of your WooCommerce Print Address Labels plugin:
- [asl_title]: Displays the title of the store.
- [asl_street]: Displays the street address of the store.
- [asl_city]: Displays the city of the store.
- [asl_state]: Displays the state of the store.
- [asl_phone]: Displays the store’s phone number.
- [asl_email]: Displays the store’s email address.
Result: When printing the address labels, the placeholders such as [asl_title]
and [asl_city]
will automatically be replaced with the actual store details related to the order.
Example: #
If the store information for a particular order is as follows:
- Store Name: “Downtown Store”
- Street: “123 Main St.”
- City: “New York”
- State: “NY”
- Phone: “(555) 123-4567”
- Email: “store@example.com“
Then the address label might look like this:
Order #12345
John Doe
Downtown Store
123 Main St.
New York, NY
(555) 123-4567
store@example.com