Displaying Inventory by Location

To display inventory based on location, simply use the location or location_id attributes in the shortcode.  Like so:

[[wpinventory location=”1″]] – or 2 or 3 or 4… whatever the location ID is.

This will also work:

[[wpinventory location_id=”1″]]

That will display all inventory at location ID #1.  From there, if you want to display only the item quantity that is at that particular location (listing page or detail page) then you can use some handy developer friendly hooks to accomplish that.  Continue reading if you are interested.

Displaying the location quantity and not the total item quantity

Display Page:

You will need two hooks to get the display/detail page wired up.  The first hook is necessary to attach the ‘location_id’ argument to the link.  The second hook is to receive that argument and get the quantity for the item at that location.  Note:  You must have the ‘inventory_quantity’ field in the listing display settings for this to work.

add_filter( 'wpim_inventory_permalink', 'my_wpim_inventory_permalink' );
function my_wpim_inventory_permalink( $permalink ) {
   // Check $arg if location_id is set then append location_id to permalink
   $loop = wpinventory_get_wpim();
   if ( $loop->is_single() ) {
      return $permalink;
   }

   $location_id = $loop->get_query_arg( 'location_id' );
   if ( ! $location_id ) {
      return $permalink;
   }

   $permalink = add_query_arg( 'location_id', $location_id, $permalink );

   return $permalink;
}

add_filter( 'wpim_get_field_inventory_quantity', 'my_wpim_get_field_quantity' );
function my_wpim_get_field_quantity( $value ) {
   $location_id = WPIMCore::request( 'location_id' );

   if ( empty( $location_id ) ) {
      return $value;
   }

   $loop = wpinventory_get_wpim();
   if ( ! $loop->is_single() ) {
      return $value;
   }
   // Query for the inventory_id and query the location quantity
   $inventory_id = $loop->single_id();

   // global db and prefix and get values raw way
   $locations         = WPIMLOCDB::getInstance();
   $location_to_table = $locations->inventory_to_location_table;
   $locations_table   = $locations->location_table;

   global $wpdb;

   $query  = "SELECT quantity, name FROM " . $location_to_table . " LEFT JOIN " . $locations_table . " ON " . $location_to_table . ".location_id=" . $locations_table . ".location_id WHERE inventory_id = %d AND " . $location_to_table . ".location_id = %d";
   $query  = $wpdb->prepare( $query, $inventory_id, $location_id );
   $result = $wpdb->get_row( $query );

   if ( $result ) {
      return $result->name . " Quantity: " . $result->quantity;
   }

   return $value;
}

Listing page only

Again, just like the details page above, you must have the ‘inventory_quantity’ field in the listing display.  Then, simply drop this code into your theme/child theme’s functions.php file:

add_filter( 'wpim_get_field_inventory_quantity', 'my_wpim_get_quantity' );
function my_wpim_get_quantity( $value ) {
   $loop = wpinventory_get_wpim();
   if ( $loop->is_single() ) {
      return $value;
   }

   $location_id = ( $loop->get_query_arg( 'location_id' ) ) ? $loop->get_query_arg( 'location_id' ) : $loop->get_query_arg( 'location' );

   if ( empty( $location_id ) ) {
      return $value;
   }

   $inventory_id = wpinventory_get_the_ID();

   if ( empty( $inventory_id ) ) {
      return $value;
   }
   global $wpdb;
   $locations         = WPIMLOCDB::getInstance();
   $location_to_table = $locations->inventory_to_location_table;
   $locations_table   = $locations->location_table;
   $query             = "SELECT quantity, `name`
      FROM ${location_to_table} AS li
         LEFT JOIN ${locations_table} AS l
            ON li.location_id = l.location_id
      WHERE inventory_id = %d
         AND li.location_id = %d";
   $query             = $wpdb->prepare( $query, $inventory_id, $location_id );
   $result            = $wpdb->get_row( $query );
   if ( empty( $result->name ) ) {
      return $value;
   }
   return $result->quantity;
}