WooCommerce wc_get_product(): Read Product Data, Pricing, Stock, and Variations with WC_Product

wc_get_product(): read and change WooCommerce product data

A WooCommerce product is a post, but its data (prices, stock, SKU, dimensions, gallery) is managed by WooCommerce’s product classes. Since WooCommerce 3.0 the right way to read and change it is the product object returned by wc_get_product(), with getters like get_price() and setters plus save(). Reading post meta directly skips WooCommerce’s caching and logic, and writing it directly leaves related values out of sync.

Problem: A plugin needs the price, sale status, stock and categories of products from a custom query, shows prices in its own template, and must update prices and stock.

Solution: Get the object with wc_get_product( $id ), read data with its getter methods, show prices with get_price_html() or wc_price(), and change data with setters followed by save().

Reading

<?php
$product = wc_get_product( 123 ); // WC_Product (or a subclass) or false

if ( ! $product ) {
    return;
}

$product->get_name();
$product->get_sku();
$product->get_type();               // simple, variable, grouped, external
$product->get_regular_price();      // "49.99" (a string, without currency)
$product->get_sale_price();         // "" when not on sale
$product->get_price();              // the current price
$product->is_on_sale();
$product->is_in_stock();
$product->get_stock_status();       // instock, outofstock, onbackorder
$product->get_stock_quantity();     // int, or null when stock is not managed
$product->get_image_id();
$product->get_gallery_image_ids();
$product->get_category_ids();

Inside the WooCommerce loop and on the product page, the global $product is already set; wc_get_product() is for everywhere else.

Showing prices

<?php
// The same price markup as in the shop: sale prices with <del>/<ins>, ranges for variable products,
// taxes as configured in "Display prices in the shop"
echo $product->get_price_html();

// Your own format
if ( $product->is_on_sale() && $product->get_regular_price() ) {
    $saving = (float) $product->get_regular_price() - (float) $product->get_sale_price();
    echo '<span class="saving">' . sprintf( esc_html__( 'You save %s', 'textdomain' ), wc_price( $saving ) ) . '</span>';
}

// A price including tax, regardless of the display setting
echo wc_price( wc_get_price_including_tax( $product ) );

wc_price() formats a number with the store’s currency, decimals and separators. Raw getters return the price as entered in the admin, which may be with or without tax depending on the store settings.

Variable products

<?php
if ( $product->is_type( 'variable' ) ) {
    $min = $product->get_variation_price( 'min', true ); // true = as displayed (taxes)
    $max = $product->get_variation_price( 'max', true );

    foreach ( $product->get_children() as $variation_id ) {
        $variation = wc_get_product( $variation_id );
        // $variation->get_attributes() = array( 'pa_color' => 'red', ... )
    }
}

Changing data

<?php
$product->set_regular_price( '39.99' );
$product->set_sale_price( '29.99' );
$product->set_manage_stock( true );
$product->set_stock_quantity( 50 );
$product->save();

save() writes everything, updates the active price (_price), the lookup tables WooCommerce uses for filtering and sorting, and clears the caches. update_post_meta( $id, '_price', ... ) does none of that, and sorting by price or the price filter widget then show wrong results. For stock changes caused by sales, wc_update_product_stock( $product, $qty, 'decrease' ) handles concurrency better than setting a number.

For many products, wc_get_products( array( 'category' => array( 'shirts' ), 'limit' => 20 ) ) returns product objects directly and accepts WooCommerce-specific arguments like stock_status and on_sale that WP_Query does not know.

NOTE: Every wc_get_product() call loads the product with its meta. In a loop of a few dozen products that is fine; for thousands, as in an export, process them in batches with wc_get_products() and paginate, so memory stays under control.