Shop - Searching and listing products


Introduction

When you enable the shop plugin’s error method it calls the shop.lib.php / error(). This method is responsible for parsing requests and finding the proper pages based on friendly urls.

Keep in mind that before the error method is triggered, OFW will look for WordPress content (if WP plugin is enabled) and any applicable controller methods (for example any methods in default.ctl.php). Only if nothing is found will we continue on to the __error() method which triggers shop.lib.php / error().

While processing the error method, the shop library will go in this order:

  1. Look for category It will try to find a Category by its friendly url. You can set the {{#url_category#}} language variable as well (in shop.en_US.lang.ini), see below for details.
  2. Look for a specific product It will try to find a specific Product by its friendly url. You can set the {{#url_product#}} language variable as well (in shop.en_US.lang.ini), see below for details.
  3. Search for products It will try to find a list of Product objects using free-text search. You can set the {{#url_search#}} language variable as well (in shop.en_US.lang.ini), see below for details.
  4. Display 404 Actually, the error() method will just return false, but typically your default.ctl.php’s __error() method will follow up with a 404 page at this point.

Category page

The category page lists all products within a specific Category. Requests are handled by shop/category.ctl.php.

Configuration options

You can set up {{#url_category#}} (shop.en_US.lang.ini) in which case the category page will only work for urls that match. So, if you set url_category = category then only http://example.com/category/my-friendly-url/ will display the category page.

Query options

You can filter and sort the results via the URL query string. Here’s a list of options:

  • view – displays a ‘list’ or ‘grid’ view where it either loads shop/category_grid.html or shop/category.html. the default is ‘list’ but this default can be modified with the default_list_view configuration option.
  • property – this can be a single or multiple ProductProperty id(s),  which will filter the list down to the products which have that property/properties. Use property[] for the input’s name or in the url if you want to filter to multiple properties.
  • featured – if set to any value it will display only featured products.
  • sort – value can be ‘name’, ‘price’ or ‘time’ and determines how the products are sorted.
  • sortdir – value can be ‘ASC’ or ‘DESC’ and determines the sort order.
  • perpage – the number of items displayed per page. Defaults to 10 but this can be changed with the default_items_per_page configuration option.
  • minprice – the minimum product price
  • maxprice – the maximum product price

Templates and variables

Depending on the view option, category lists have two templates: shop/category_grid.html or shop/category.html. Copy them to your local /app/view/ folder to customize it.

In your template, you have access to these variables:

  • {{category}} – the currently selected Category
  • {{products}} – a list of Product objects found in the current Category
  • {{productproperties}} – a list of ProductProperty objects found in the current Category
  • {{current_property}} – the currently selected ProductProperty, if any
  • {{current_properties}} – the currently selected ProductProperties, if multiple properties were defined.

Product page

The product page displays a specific product. Requests are handled by shop/product.ctl.php.

Configuration options

You can set up {{#url_product#}} (shop.en_US.lang.ini) in which case the product page will only work for urls that match. So, if you set url_product = products then only http://example.com/products/my-friendly-url/ will display the product page.

Templates and variables

In your template /shop/product.html, you have access to these variables:

  • {{product}} – the currently selected Product object
  • {{admin_preview}} – this will be true when the admin is previewing the product page; implementing this is optional

Search page

The search results page lists all products that result from a search query. Requests are handled by shop/product.ctl.php / search().

Configuration options

You can set up {{#url_search#}} (shop.en_US.lang.ini) in which case the search page will only work for urls that match. So, if you set url_category = find then only http://example.com/find/?query=my%20search%20query will display the search results page.

Query options

You can filter and sort the results via the URL GET query string. Here’s a list of options:

  • query – the search query as sent by the user. if you want to display it on the page you can use {{zaj.get.query|escape}} to show what the user searched for.
  • view – displays a ‘list’ or ‘grid’ view where it either loads shop/search_grid.html or shop/search.html. the default is ‘list’ but this default can be modified with the default_list_view configuration option.
  • property – this can be a single or multiple ProductProperty id(s),  which will filter the list down to the products which have that property/properties. Use property[] for the input’s name or in the url if you want to filter to multiple properties.
  • featured – if set to any value it will display only featured products.
  • sort – value can be ‘name’, ‘price’ or ‘time’ and determines how the products are sorted.
  • sortdir – value can be ‘ASC’ or ‘DESC’ and determines the sort order.
  • perpage – the number of items displayed per page. Defaults to 10 but this can be changed with the default_items_per_page configuration option.
  • minprice – the minimum product price
  • maxprice – the maximum product price

Templates and variables

Depending on the view option, category lists have two templates: shop/search_grid.html or shop/search.html. Copy them to your local /app/view/ folder to customize them.

In your template, you have access to these variables:

  • {{products}} – a list of Product objects that match the current search query
  • {{productproperties}} – a list of ProductProperty objects found in any of the Product objects that match the current search query (this is different from the category page!)
  • {{current_property}} – the currently selected ProductProperty, if any
  • {{current_properties}} – the currently selected ProductProperties, if multiple properties were defined.

Get all available products

Use Product::fetch_by_shop() to get a list of products for the current shop. If you want items for a specific shop, you can pass that as a parameter Product::fetch_by_shop($shop).

Get last visited products

Visited products are saved into database with the actual order id (since each visitor gets their own order id).

The list of last visited products is available through the Product::fetch_last_visited(). You can optionally pass an $order object if you do not want to fetch last visited for the current user (current order id).

The list of last visits is available in ProductVisit::fetch_by_order(). You can optionally pass an $order object if you do not want to fetch last visited for the current user (current order id).

Autocomplete searchbox

Use the code snippet below to create a search box with autocomplete.

<form id="searchbar">
	<input type="text" name="search_value" id="search_value" class="header-searchbar__input" />
    <div id="search_autocomplete" class="search-autocomplete"></div>
    <script>
        var search_request;
        zaj.ready(function(){
            var $sa = $('#search_autocomplete');
            $sa.fadeOut(0);
            search_request = zaj.search.initialize($('#search_value'), {
                url: 'shop/ajax/search/',
                receiver: $sa,
                pushstate_url: null,
                callback: function(r){
                    $sa.fadeIn(500);
                    $sa.html(r);
                }
            });     
        });
    </script>
    <div class="clearfix"></div>
</form>
Outlast Web & Mobile Development (c) 2019 | Privacy Policy |