The Castlegate IT WP Product Catalogue plugin provides a simple, searchable product catalogue for WordPress, using Advanced Custom Fields. It supports taxonomies, featured products, discounts, image galleries, product variations, and searching by price range.
The post type and taxonomy names are set with constants. These can be overridden to avoid naming conflicts by defining the constants earlier, e.g. in wp-config.php.
CGIT_PRODUCT_POST_TYPEis the post type name, defaultproduct.CGIT_PRODUCT_CATEGORYis the category taxonomy name, defaultproduct_category.CGIT_PRODUCT_TAGis the tag taxonomy name, defaultproduct_tag.
The currency symbol displayed in the WP admin interface is also set using the CGIT_PRODUCT_CURRENCY constant and can be overridden. The default value is £ Note that the symbol is only used in the admin panel; it is not stored in the database.
The number of products per page (in archives or searches) can be customized with CGIT_PRODUCT_PER_PAGE. By default, this constant is not defined and the number of products per page will be the same as the number of posts per page.
Products exist as entries in the product post type. They support the same range of fields as regular posts or pages, including comments. They also include various custom fields, defined using ACF:
- Price
- Includes VAT?
- Featured product?
- Discount (with options for an amount or a percentage)
- Image gallery
- Catalogue code
- Number in stock
- Product variations, including name, description, and images
- Related products
Two taxonomies have also been defined for the product post type: categories and tags. These should behave like their equivalents for the default post type.
The main product listing archive-product.php will list featured products first, then all products in alphabetical order.
The admin panel lets you enter the price, the discount type (none, an amount, or a percentage), and a numerical discount value. This original price is saved as price_original. When you update the product, the plugin will calculate the discounted price with any discounts applied and save it as price.
This field cannot be edited directly in WordPress, but will be updated every time the product is saved. When searching by price, it is this calculated price field that is used, not the original price.
The $catalogue->metaQuery() method converts WP_Query-like arguments related to products into a meta query that is actually compatible with WP_Query. This function is used internally to filter searches that are specifically restricted to the product post type, allowing the following query parameters:
min_priceminimum price (number)max_priceminimum price (number)inc_vatprice includes VAT (boolean)featuredfeatured product (boolean)discountdiscounted product (boolean)cat_codecatalogue code (string)stockin stock (boolean)
For example, a product search query string might look like ?post_type=product&max_price=20&featured=1. These searches can be combined with the default WordPress search query string, e.g. ?s=example.
As with WP_Query and get_posts(), you can use the orderby and order (ASC and DESC) options to set the order of the posts. You can also sort by price, using ?orderby=price. See the WordPress documentation for the default options.
Therefore, you could use something like the following to allow quick sorting of products on a search or archive page:
<?php
$asc = add_query_arg('order', 'asc');
$desc = add_query_arg('order', 'desc');
?>
<p>
<a href="<?= add_query_arg('orderby', 'title', $asc) ?>">Sort by name (ascending)</a>
<a href="<?= add_query_arg('orderby', 'title', $desc) ?>">Sort by name (descending)</a>
<a href="<?= add_query_arg('orderby', 'price', $asc) ?>">Sort by price (ascending)</a>
<a href="<?= add_query_arg('orderby', 'price', $desc) ?>">Sort by price (descending)</a>
</p>Searching by custom taxonomy is supported natively by WordPress, using the taxonomy slug as the query parameter. Assuming that CGIT_PRODUCT_CATEGORY is product_category, you can could use ?product_category=foo to search for a single category or ?product_category[]=foo&product_category[]=bar to search for multiple categories.
According to the WordPress template hierarchy, product searches will use the search.php template. This plugin adds the option of using a search-product.php template to customize the format of product searches.
The $catalogue->render('search') method returns the compiled output of the file views/search.php within the plugin directory. Future versions of the plugin may include more views that can be rendered with this method. Their default output can be considered an example form; you can create your own forms or use the cgit_product_search_form filter to customize this form to suit your site.
cgit_product_catalogue() returns the single instance of the Cgit\Products\Catalogue object. This is mostly used internally to manage the product post type and associated queries. However, you may interact with it in templates to access the $catalogue::formatCurrency($number, $after = false, $sep = '') method, which formats numbers with two decimal places and the currency symbol set in CGIT_PRODUCT_CURRENCY. If $after is true, the symbol is placed after the number; $sep is always put between the number and the symbol.
cgit_product($post_id) returns a Cgit\Products\Product object, which is based on the default WP_Post object, but has additional properties for the various product details. If $post_id is not specified, the function uses the current post ID. This function is provided for convenience, so you don't have to write lots of get_field() calls.
cgit_products($args) works like get_posts, but allows more arguments (see Searches and queries above) and returns an array of Cgit\Products\Product objects instead of WP_Post objects. This could be used to return a list of featured products:
$featured = cgit_products(array(
'featured' => true
));Various filters are available to edit the product post type and fields.
cgit_product_post_typefilters the options passed to theregister_post_type()function that defines the product post type.cgit_product_fieldsfilters the main product field options passed to ACF.cgit_product_variant_fieldsfilters the product variant field options passed to ACF.cgit_product_related_fieldsfilters the related product field options passed to ACF.cgit_product_categoryfilters the options passed to theregister_taxonomy()function that defined the product category taxonomy.cgit_product_tagfilters the options passed to theregister_taxonomy()function that defined the product tag taxonomy.cgit_product_search_formfilters the HTML of the default search form. You could use this to edit or replace the default search form.cgit_product_meta_queryis used in thecgit_product_meta_query()function that converts query parameters to WordPress meta queries. You can use this to extend the range of searchable fields.cgit_product_render_searchcan be used to edit or replace the search form returned by$catalogue->render('search').
Requires Advanced Custom Fields. The plugin will fail to activate without this dependency.