Return the Starting Price by Bedroom Count by Asset

This document describes how to retrieve the lowest price (starting price) by bedroom count from Spaces using shortcode.

Shortcode Method

Spaces provides the option to return a simple list of elements, containing each distinct bedroom count from the available units, and the lowest starting price from the available units for each individual bedroom count. To utilize this method, add a shortcode of the following format to any page: [spaces_room_price asset_id="XXXXX"]

Replace the value of the asset ID parameter ("XXXXX" above) with the WordPress post ID of the Spaces Asset post in the CMS. This is the same ID that would be used for the standard Spaces shortcode, and can be found by viewing the Spaces Assets index in the CMS, looking at the shortcode column for the ID. Alternatively, one can find the WP ID in the address of the Spaces Asset page itself as the value of the "post" parameter; for example, if the Spaces Asset were at the following address, the appropriate value for the asset ID would be 12345:

https://example.dev.wearestud.io/wp-admin/post.php?post=12345&action=edit

If supplied with a valid asset ID, the shortcode will generate an output similar to the following by default:

1 Bedrooms starting at:
$1,890
2 Bedrooms starting at:
$2,830
Studios starting at:
$1,545

๐Ÿ“˜

Please note that the output of the shortcode is based on the units currently available for the asset. If there are no available units for a particular bedroom count, the bedroom count will not be included in the returned array. If there are no available units at all, the array will be empty.

Individual Bedrooms

By default, the shortcode will return a list of elements containing each distinct bedroom count, and the lowest starting price for each one. To display only a single bedroom count and price at a time, a room count parameter can be added to the shortcode to return only the specified bedroom count:

[spaces_room_price asset_id="XXXXX" room_count="YYYYY"]

Replace the "YYYYY" with the value of the bedroom count that you would like to display (0 for studios, then 1, 2, 3, etc.). For example, [spaces_room_price asset_id="47" room_count="0"] would only output a single element with the Studio bedroom count and starting price.

Note: As stated above, selecting a room count with no available units will result in an empty return value.

Overriding the output

Spaces provides an API to override Twig templates. To manage the HTML output of the shortcode, developers can add a twig file to this location: /wp-content/[your-theme-here]/views/spaces-room-price.twig

The default contents of the template are as follows:


{% if rooms %}
  <div class="spaces-room-price-list">
    {% for room in rooms %}
    <div clas="spaces-room-price">
      <div class="spaces-room-price-count">{{room.bedroom_count_label}}</div>
      <div class="spaces-room-price-formatted">{{room.min_price_display}}</div>
    </div>
    {% endfor %}
  </div>
{% endif %}

Use double braces to tokenize output like this: {{ }}

Example data points available to the twig:

array(3) {
  [0]=>
  array(5) {
    ["bedroom_count"]=>
    int(2)
    ["min_price"]=>
    int(2165)
    ["min_price_display"]=>
    string(6) "$2,165"
    ["bedroom_count_label"]=>
    string(23) "2 Bedrooms starting at:"
    ["min_price_display_string"]=>
    string(30) "2 Bedrooms starting at: $2,165"
  }
  [1]=>
  array(5) {
    ["bedroom_count"]=>
    int(1)
    ["min_price"]=>
    int(1695)
    ["min_price_display"]=>
    string(6) "$1,695"
    ["bedroom_count_label"]=>
    string(23) "1 Bedrooms starting at:"
    ["min_price_display_string"]=>
    string(30) "1 Bedrooms starting at: $1,695"
  }
  [2]=>
  array(5) {
    ["bedroom_count"]=>
    int(0)
    ["min_price"]=>
    int(1574)
    ["min_price_display"]=>
    string(6) "$1,574"
    ["bedroom_count_label"]=>
    string(20) "Studios starting at:"
    ["min_price_display_string"]=>
    string(27) "Studios starting at: $1,574"
  }
}

Utility Method

For full control over the output without overriding the twig template, SPACES also provides access to a utility method to return the data via PHP WordPress templates.

/**
 * Returns the minimum price for a bedroom count by asset
 *
 * @param integer|string $spaces_asset_id         The WP ID of the SPACES asset post in the WordPress CMS
 * @param integer|string|null $room_count = null  Optional room count to return price data for a single room count
 * @return array|false                            A unique array of bedroom counts with minimum price, returns false if no SPACES asset found in CMS.
 */
public static function spaces_get_min_price_for_room_count_by_asset ( int|string $spaces_asset_id, int|string|null $room_count = null ): array|false

Usage:

$rooms = \SPACES\Utility::spaces_get_min_price_for_room_count_by_asset( spaces_asset_id: 5 ); // Return room counts array for SPACES Asset ID 5

$single_room = \SPACES\Utility::spaces_get_min_price_for_room_count_by_asset( spaces_asset_id: 5, room_count: 2 ); // Return room counts array for SPACES Asset ID 5, for 2 bedrooms only.