Using Calculated WooCommerce fields in Views query filters

We have added some ‘calculated’ fields for you to use in your normal Views Query filters. Whenever you create a Views Query for your WooCommerce Products, you can use these filters to refine your View results.

You can easily create complex Views using filters such as “Products on sale”, “Products between 10€ and 50€”, “Products in Stock”. And of course, combine these to produce rich output for your users.

If you plan to use these field, you’ll need to choose how and when they will be calculated. Check out our section on Manual and batch update for WooCommerce calculated fields

View Query filter - WooCommerce fields
View Query filter – WooCommerce fields

Select the field you want for comparison in your Views Query Filter and set your filter options:

Query filter - calculated fields
Views query filter – calculated fields

In the example above we are using the views_woo_in_stock. This will return 1 (true) if the product has stock and 0 (false) if the product out of stock.

views_woo_on_sale is similar and will also return values of 1 (true) and 0 (false) if the product is on sale. For Variable products, this will be true if any of the Product Variants are on sale.

views_woo_price. This will return the product’s normal price or, if it is currently on sale, the sale price. For Variable products, the lowest price for the Products variants will be returned.

Using Calculated WooCommerce fields in Views Conditional shortcodes

You can use the [wpv-if][/wpvif] conditional shortcode in your views together with the WooCommerce Views calculated fields to check for products on sale or product in stock and modify your HTML accordingly.

If you plan to use these field, you’ll need to choose how and when they will be calculated. Check out our section on Manual and batch update for WooCommerce calculated fields

So for example, you can add an image or text for the product if it is on sale. woo_product_on_sale = returns a value of 1 (true) if the product is on sale. Otherwise, a 0 (false) will be returned.

You can use the following in your View or Content template.

[wpv-if onsale="views_woo_on_sale" evaluate="$onsale = '1'"]On sale![/wpv-if]

or

[wpv-if evaluate="[types field="views_woo_on_sale" id=""][/types] = '1'"]On sale![/wpv-if]

Similarly for checking stock, the structure is nearly identical.

[wpv-if instock="views_product_in_stock" evaluate="$onsale = '1'"]Got lots![/wpv-if]

or

[wpv-if evaluate="[types field="views_product_in_stock" id=""][/types] = '1'"]Got lots![/wpv-if]

Find out more about conditional html output in Views.

Manual and batch update for WooCommerce calculated fields

WooCommerce stock and price are calculated fields. For example, we can specify a price and a sales price for our product. Not only that, we can also create a schedule for our sales price. This means we can set a from/to date range for our sales price to be active. If you plan to use these fields in your Views filter, you’ll need to run a batch routine to Calculate product attributes for Views filters.

When do you need to update calculated fields

Since WooCommerce Views version 2.7.2, fields are automatically calculated during WooCommerce Views plugin activation. This means you do not need to manually calculate them.

The calculated fields will be updated automatically when stock changes or you edit prices – so you normally won’t need to intervene.

If you plan to use Scheduled dates for your Product Sales Prices, the fields won’t get updated when the schedule starts and finishes, so you’ll need to either run manually or schedule the cron task to update quietly in the background for you.

If you disable the WooCommerce Views plugin and stock or price changes take place, the custom fields can get out of sync and you’ll need to manually update them when you re-active the plugin.

To deal with this, WooCommerce Views offers you three alternatives to update the values of these Calculated fields. The plugin does this by batch processing all your products and makes sure the Calculated fields values are set correctly.

Static Product Fields for Parametric Searches
Static Product Fields for Custom Searches

There are three options as available in the WooCommerce Views admin screen:

  1. Manual
  2. Using WordPress Cron
  3. Using System Cron

Manual method is the easiest thing to do. It’s selected by default and will show the Calculate product attributes for Views filters button (only appears when manual is selected). By clicking on this button and confirming the action, the plugin will automatically update the Calculated field values. Since this is manual, you will need to click button to trigger the update.

Using WordPress Cron offers an automatic alternative in updating the WooCommerce Views custom field values. Simply select this option and choose the interval. Then click “Save all settings“. WordPress cron will automatically update the fields once the interval is reached.

Bear in mind this is not the same as the real Linux cron task. When there is an HTTP request made (usually when there is a visitor in your site), WP Cron will check if there is cron schedule to be executed (based on the interval you set). If there is, it will run the WooCommerce Views batch processing function for the fields.

Using a system cron allows you to run the batch processing script automatically determined by the plugin in a real cron job way.

You can easily create a custom product search with Toolset. This works best if you are customizing a WooCommerce site with a theme integrated with Toolset Layouts. This is because the entire page is overridden and customizable.

However, if you customize your WooCommerce listing pages (e.g. the “Shop” page) by using only the Views plugin’s WordPress Archives, you will notice that it still displays the default WooCommerce dropdown for sorting. As you might have guessed, this is still coming from the WooCommerce archive template, as shown in the following image.

Default WooCommerce sorting controls
Default WooCommerce sorting controls

If you want to replace the default WooCommerce sorting controls with your own, follow the tutorial about building WooCommerce Product Search with Toolset and no PHP.

You also need to disable the default WooCommerce sorting filter in the settings. To do this, go to the Toolset -> WooCommerce Views page and scroll down to the Frontend Sorting section. There, uncheck the Use default WooCommerce sorting for product archives option and save.

Please note that this setting is hidden if you are using Layouts. It is because the Layouts plugin overrides the entire page and the default WooCommerce sorting is not used at all. In this case, you do not need to change any settings.

WooCommerce Views setting for using the WooCommerce default sorting controls
WooCommerce Views setting for using the WooCommerce default sorting controls

On the front-end, you should only see your custom Toolset search filter and not the default WooCommerce sorting filter, as shown in the following image.

Custom product search controls created with Toolset
Custom product search controls created with Toolset

 

Since WooCommerce version 3.0, the  _featured and _visibility custom fields are no longer used. Instead, a new “product visibility” taxonomy is used.

This change was done to improve performance according and you can read more about it in the official release notes

If you are using Views with WooCommerce 3.0, this means you will not be able to query these fields anymore and display featured WooCommerce products or exclude hidden products. The solution is to make sure you upgrade to WooCommerce Views version 2.7.2 since it adds querying support for the new “product visibility” taxonomy.

WooCommerce Views version 2.7.2 and newer, allows you to query these terms just like any other taxonomy. To do this, use the following steps:

  • In the Query filter section, click the Add a filter button.
  • In the Filter by dropdown select Product Visibility under the Taxonomies section.
  • Click the Add query filter button.

That’s it!

For example, if you wanted to display only featured products, you would use the setting as displayed in the following image.

Filtering by featured products
Filtering by featured products

Aside from querying featured products, you can also query the following:

  • Products set as “hidden” on the backend (exclude-from-search or exclude-from-catalog).
  • WooCommerce “out of stock” products (Outofstock).
  • Rated products.