Download the PHP package codelight/woocommerce-product-variation-stock-cache without Composer

On this page you can find all versions of the php package codelight/woocommerce-product-variation-stock-cache. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package woocommerce-product-variation-stock-cache

WooCommerce Product Variation Stock Cache

This library adds a way to filter variable products by attributes and stock status.

WooCommerce currently doesn't have a way of querying products by attribute and displaying it only if a variation with the given attribute is in stock. For details, see the github issue. This functionality is scheduled to be released in v4.0, which is scheduled to be released somewhere in 2019, but no certain information is available yet.

Until then, you can use this library as a relatively simple workaround that won't affect performance much.

How it works

The library keeps track of each variation's stock status in the parent product's post meta.

Let's say you have an attribute "Size" with the following values: "S", "M", "L". The library stores the stock statuses in the parent post meta like this:

This allows you to use simple meta queries to fetch only those variable parent products that have a variation with a specific attribute in stock.

Installation

composer require codelight/woocommerce-product-variation-stock-cache

The library is initialized automatically and will start keeping track of stock status changes. To generate cache for existing products, see below.

Usage

In your WP_Query for retrieving the products, use the following meta query:

For example:

Keep in mind that the attribute_value is the slug of the attribute, not the human-readable value.

Multi-dimensional variation attributes

The library also works with multi-dimensional variation attributes. Let's say that in addition to the "Size" attribute, you also have "Color": "red" and "blue". In this case, a power set with the following cache keys will be created:

Having a power set allows you to filter by any single attribute or combination of attributes, in this case:

The following example will filter by both size and color:

Note that this library is not a good solution if you have a lot of attributes. The number of postmeta fields increases exponentially with the attributes and that's going to hurt your performance. However, it should still work properly, even with very long cache keys - if a cache key exceeds the allowed 255 chars, an md5 hash will be used instead.

Generating the initial cache

If you need to prime your cache for existing products: 1) Log in as (super) admin 2) Append the following query string to any URL: codelight_prime_cache=1 3) You won't see anything happening, but the cache will be primed.

Note that this will attempt to take care of all products in one request, so if you have a lot of products, this might timeout and crash.
Batch priming and a proper CLI command are in the todo list, help is appreciated.

Customization

Ignore an attribute

By default, all product attributes will be used for generating the cache. If you wish to use only specific attributes:

Change the postmeta key prefix

The default prefix is _codelight_stock. To change that:

Disable automatically initializing the library

The library runs automatically and hooks are automatically set up on 'after_setup_theme'. If you wish to change that:

Contributing

Any help is appreciated!

About

This library is proudly brought to you by Codelight, a web development agency based in Estonia, EU.


All versions of woocommerce-product-variation-stock-cache with dependencies

PHP Build Version
Package Version
Requires php Version >=7.0.0
composer/installers Version ~1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package codelight/woocommerce-product-variation-stock-cache contains the following files

Loading the files please wait ....