WP SITES

3086 Coded Tutorials & 289 Plugins

How To Get WooCommerce Variations Data

WooCommerce variations are stored as individual posts of type product_variation, and their IDs are globally unique across all post types, just like any other post in WordPress.

So if you have a variation ID like 5758, it’s guaranteed to point to one specific variation site-wide, regardless of which parent product it belongs to.

Here’s how you can find a WooCommerce variation ID in the wp_posts table, and get all data associated with it that is stored in your database.

In your PHP admin screen, run a SQL Query or SQL Command.

SELECT ID, post_title, post_parent
FROM wp_posts
WHERE post_type = 'product_variation';

And you’ll get something like this :

IDpost_titlepost_parent
5758Tooltip – Small, Black5757
5759Tooltip – Small, White5757
5760Tooltip – Large, Black5757
5761Tooltip – Large, White5757

Now you can click on any ID in the first column to get all the data for that specific variation. Here’s the screenshots showing you exactly what data is stored in the DB, how it’s stored and how to access it.

Now we can use the variation id in PHP code to get all the data associated with it :

$variation = wc_get_product( '5758' );

General Info

$variation->get_id() | Variation ID
$variation->get_parent_id() | Parent product (variable product) ID
$variation->get_name() | Name (e.g., “T-Shirt – Large / Red”)
$variation->get_slug() | URL slug
$variation->get_permalink() | Full product + variation URL
$variation->get_type() | Always returns 'variation'

Attributes

$attributes = $variation->get_attributes();

Pricing & Stock

$variation->get_price() | Current price
$variation->get_regular_price() | Regular price
$variation->get_sale_price() | Sale price
$variation->is_on_sale() | Is on sale (true/false)
$variation->get_stock_quantity() | Stock quantity
$variation->get_manage_stock() | Is stock managed (true/false)
$variation->is_in_stock() | Is it in stock?
$variation->get_stock_status() | instock, outofstock, onbackorder
$variation->backorders_allowed() | Allow backorders (true/false)
$variation->get_backorders() | no, notify, or yes

Images

$variation->get_image_id() | Attachment ID for featured image
wp_get_attachment_url( $variation->get_image_id() ) | Get the image URL
$variation->get_image() | HTML image tag (with size & attr)

Shipping Info

$variation->get_weight() | Weight
$variation->get_length() | Length
$variation->get_width() | Width
$variation->get_height() | Height
$variation->get_shipping_class() | Shipping class slug
$variation->get_shipping_class_id() | Shipping class term ID

SKU, Tax, Purchase Rules

$variation->get_sku() | SKU for the variation
$variation->get_tax_class() | Tax class slug
$variation->get_tax_status() | taxable, none, etc.
$variation->get_virtual() | Is virtual product?
$variation->get_downloadable() | Is downloadable product?
$variation->get_downloads() | Array of download files (if any)
$variation->get_download_expiry() | Download expiry (in days)
$variation->get_download_limit() | Download limit (int)

Availability / Scheduling

$variation->get_date_created() | Date created (WC_DateTime)
$variation->get_date_modified() | Last modified (WC_DateTime)
$variation->get_date_on_sale_from() | Scheduled sale start
$variation->get_date_on_sale_to() | Scheduled sale end

Custom Fields

get_post_meta( $variation_id, '_your_meta_key', true );

Related Solutions

Leave a Reply

New Plugins