In case you want to have a Find in Store button to redirect to the store locator page from your WooCommerce product page, this can be achieved with just a couple of lines of code that you need to add to your functions.php of the active theme, here is the code below.
/**
* [find_in_store_button_hook Hook to add a Find in Store button]
* @return [type] [description]
*/
function find_in_store_button_hook() {
echo '<a class="btn btn-primary" href="/store-locator-url">'.__('Find in Store', 'asl_locator').'</a>';
}
add_action( 'woocommerce_after_add_to_cart_quantity', 'find_in_store_button_hook', 30 );
In the above code, we have used after-cart quantity action, if you want to place it somewhere else, you can replace that event, more events are available on the WooCommerce documentation.
Try Multi-Store Addons for WooCommerce #
The above code will just add a button, if you want your stores to appear on your product page and your users to be able to select the from the page, with the store details visible in the order and shipping with it, you can our Multi-Store Addons for WooCommerce which has a lot more features related to Stores and managing the inventory by stores.
How to Load Associated Stores on WooCommerce Product Pages with Store Locator? #
If you want the Store Locator to display only the locations assigned to a WooCommerce product via the Multi-Store Addons for WooCommerce, you need to add the Store Locator shortcode [ASL_STORELOCATOR]
to the WooCommerce product page and insert the following code into your functions.php
file.
This code will filter and display only the stores that are related to the product being viewed. This feature is supported starting from version 1.6.1 of Multi-Store Addons for WooCommerce.
Code to Add to functions.php
#
/**
* Filter out unrelated stores from the store locator list on the WooCommerce product page.
*/
function aslwc_product_stores_only() {
// Only proceed if it's an AJAX request
if (defined('DOING_AJAX') && DOING_AJAX) {
// Get the referring URL from where the AJAX request originated
$referrer = wp_get_referer();
// Parse the URL to get the post ID
if ($referrer) {
$referrer_post_id = url_to_postid($referrer); // Retrieve post ID from URL
if ($referrer_post_id) {
// Get the post object for the referred post ID
$post = get_post($referrer_post_id);
// Check if the post type is 'product'
if ($post && $post->post_type === 'product') {
$product_id = $post->ID; // We have the product ID
// Apply the filter for 'asl_filter_stores_query'
add_filter('asl_filter_stores_query', function($query) use ($product_id) {
// Get store IDs related to the product
$store_ids = \ASLWC\Helper::get_store_ids_by_product($product_id);
// When Store IDs are found
if (!empty($store_ids)) {
$store_ids = array_map('intval', $store_ids);
$store_ids = implode(',', $store_ids);
// If IDs are there, modify the query based on the product ID
if ($store_ids) {
$query .= " AND s.`id` IN (" . $store_ids . ")";
}
}
return $query;
});
}
}
}
}
}
add_action('wp_loaded', 'aslwc_product_stores_only');
Explanation: #
- Purpose: This code checks if the current AJAX request originates from a WooCommerce product page and filters the Store Locator query to show only stores related to the viewed product.
- Version Support: The feature has been supported since version 1.6.1 of the “Multi-Store Addons for WooCommerce.“
By following these steps, your Store Locator will dynamically filter and display only relevant store locations on WooCommerce product pages, enhancing the user experience by showing accurate product availability per location.