Getting Product Variants Programmatically in WooCommerce

In WooCommerce, we use product variants generally set in the product editor and then display them in the product page, which is an easy operation for anyone who is familiar with WooCommerce operations. But for us developers, how to get product variants in a way that is not common. In this article, I will introduce how to get product variants programmatically for further processing.

Let's assume that, in the following example, the$product an WC_Product A product object, or more specifically, a WC_Product_Variable variableWe can use the WooCommerce hooks or the wc_get_product() function to get it as an argument.

But if you're not sure $product Whether the object is a product variant can also be determined using the if( $product->is_type( 'variable' ) ) Conditions to judge.

Method 1. get_available_variations()

$variations = $product->get_available_variations();

if( $variations ) {
foreach( $variations as $variation ) {
// a WC_Product_Variation object

echo $variation->get_id(); // the ID of the particular variation
echo $variation->get_name(); }
}
}

This approach looks like exactly what we need, but there's a small problem -- the It only applies to visible inventory variables, theThat is, variants with the “Enabled“ checkbox checked will only be available through this function.

We can use the get_available_variations()method gets only the enabled inventory variants.

How to get all variables with get_available_variations()

As I mentioned before.get_available_variations()Only variants that are in stock and not hidden are returned. Is there a way around this? The answer is yes.

If we set up our WooCommerce settings in theUncheck this checkbox, you can allow this method to return the out-of-stock variant::

This checkbox is available in the WooCommerce > Settings > offerings > Inventory found.

Second, we can also use filter hooks woocommerce_hide_invisible_variationsinstruct sb. to do sth. get_available_variations()method can also return hidden (not enabled) changes:

add_filter( 'woocommerce_hide_invisible_variations', 'wprs_all_variations', 25, 3 );

function wprs_all_variations( $hide_invisible, $variation_id, $variation ) {
// We can also add some variant-specific conditions here
$hide_invisible = false;
return $hide_invisible;
}

Or a simplified version

add_filter( 'woocommerce_hide_invisible_variations', '__return_false' );

Method 2. get_children()

What we discussed above get_available_variations()The way to do this is to just add the get_children() methodology based work.

However, if you need to get both hidden and visible, in-stock and out-of-stock variants of a WooCommerce product.get_children()method might work better for you. You just need to remember one thing - it returns an array of variant IDs, not objects.

$variation_ids = $product->get_children();

if( $variation_ids ) {
foreach( $variation_ids as $variation_id ) {

$variation = wc_get_product( $variation_id );
if ( ! $variation || ! $variation->exists() ) {
continue; }
}

echo $variation->get_id();
echo $variation->get_name(); }
}
}

Method 3. Getting Product Variants in the WooCommerce REST API

In addition to the above two ways, we can also use the WooCommerce REST API to get the product variants, but this method is a bit roundabout, obviously can be obtained directly from the database through PHP, this method does have an additional HTTP request, the performance will also have a slight impact, if there is not a special need, it is not recommended to use this method.

$url = '';
$username = '';
$pwd = ''; // application password, not WordPress login password

// Send a request to get the product variant in WooCommerce
$response = wp_remote_get(
add_query_arg(
array(
'per_page' => 100, // defaults to 10
// 'status' => 'publish', // get only enabled variants
// 'stock_status' => 'instock', // get only variants that are in stock
),
"{$url}/wp-json/wc/v3/products/{$product_id}/variations"
),
array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( "{$username}:{$pwd}" )
)
)
).

// You can check the server return code and content here before proceeding
$variations = json_decode( wp_remote_retrieve_body( $response ), true );
if( $variations ) {
foreach( $variations as $variation ) {
echo $variation[ 'id' ]; echo $variation[ 'id' ]; }
echo $variation[ 'sku' ]; }
}
}

Let me explain the code:

  • As you can see, I'm using the WordPress HTTP API functions - the wp_remote_get()
  • To authenticate, you need to provide a username and application password.
  • please note per_page parameter, since its default value is 10, which means that if you have many variants of your product, you won't be able to get all of them in one REST API request.

If there are any questions or ideas about this article, feel free to extract them in the comments section and we'll discuss and study them together.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *