WC_Product{}└─ WC_Abstract_Legacy_Product
Works with data of an individual WooCommerce product: name, prices, stock quantities, images, attributes, and other properties. Allows creating, retrieving, modifying, and deleting products.
This is the base products class. Specific types are represented by its descendants:
| Class | Product type |
|---|---|
| WC_Product_Simple{} | Simple product. |
| WC_Product_Variable{} | Variable product. |
| WC_Product_Variation{} | A single variation. |
| WC_Product_Grouped{} | Grouped product. |
| WC_Product_External{} | External/partner product. |
Despite the word Abstract in the source code description, the class is not declared abstract. Creating new WC_Product() is technically possible, but for a new product you should choose the class corresponding to its type.
Data is read using get_*() methods, modified using set_*() methods, and written to the database using save(). The class performs storage operations via Data Store. Therefore, the code must not update service meta-fields like _price or _stock on its own.
Main methods:
| Method | Purpose |
|---|---|
| get_id() | Product ID. For a new unsaved object — 0. |
| get_type() | Product type: simple, variable, variation and others. |
| get_name() | Name. |
| get_sku() | SKU. |
| get_description() | Full description. |
| get_short_description() | Short description. |
| get_permalink() | Product link. |
| get_price() | Current price without HTML and currency symbol. |
| get_regular_price() | Regular price. |
| get_sale_price() | Sale price. |
| get_price_html() | HTML of the price with formatting, currency, and taking into account tax display settings. |
| get_image() | HTML for the main image. |
| get_image_id() | ID of the main image. |
| get_gallery_image_ids() | Gallery image IDs. |
| get_category_ids() | Category IDs. |
| get_tag_ids() | Tag IDs. |
| get_stock_quantity() | Stock quantity (inventory). |
| get_stock_status() | Stock status. |
| get_attributes() | All attributes. |
| get_attribute() | Value of one attribute. |
| get_meta() | Reading a user meta-field. |
| update_meta_data() | Updating a user meta-field. |
| get_data() | Object data array. |
| get_changes() | Changed properties awaiting saving. |
| set_props() | Setting multiple properties via an array. |
| save() | Creating or updating a product. Result — product ID. |
| delete() | Deleting. If true — permanently. |
For checks, the following are available:
| Method | Purpose |
|---|---|
| is_type() | Checks the product type. |
| is_on_sale() | Returns whether or not the product is on sale. |
| is_in_stock() | Returns whether or not the product can be purchased. This returns true for 'instock' and 'onbackorder' stock statuses. |
| is_purchasable() | Returns false if the product cannot be bought. |
| is_visible() | Returns whether or not the product is visible in the catalog. |
| is_virtual() | Checks if a product is virtual (has no shipping). |
| is_downloadable() | Checks if a product is downloadable. |
is_purchasable() does not replace checking stock quantities. And is_in_stock() allows the onbackorder status, so its result does not necessarily mean a positive quantity in the warehouse.
Many property getter methods accept $context:
view- value with WooCommerce filters applied. Used by default.edit- value without filters for retrieving a property. Suitable for editing and subsequent saving.
The edit context takes into account the changes already made to the object. It does not re-read the value from the database.
Use wc_get_product() when you need to get an existing product. The function determines its type and creates an object of the appropriate class.
Use wc_get_products() when you need to get a list of products by conditions.
The set_*() methods change data only in the object. To write changes to the database, call save().
Some property setter methods throw an WC_Data_Exception when given invalid data. The set_props() method collects such errors in WP_Error. In this case, successfully processed properties remain changed: there is no automatic rollback.
Hooks from the class
Returns
null.
- WC_Product - object when created via
new WC_Product(). The constructor itself does not return a separate value.
Creating an object without an ID does not add the product to the database. The record is created when save() is called.
Usage
$wc_product = new WC_Product( $product ); // use class methods
- $product(number|WC_Product|object)
Product for initializing the object. Constructor parameter.
- Positive ID - load product data from the database.
WC_Productobject - take its ID and load data from the database. Unsaved changes of the passed object are not copied.- Object with field
ID, for example WP_Post - load the product by this ID. 0- create an object for a new product.
Direct constructor call does not select a class based on the type of the existing product.
With a positive ID for a missing product or a record of an inappropriate type, the default data store throws
Exception. The functionwc_get_product()catches the exception and in such case returnsfalse.Default:
0
Methods
- public __construct( $product = 0 )
- public get_type()
- public get_name( $context = 'view' )
- public get_slug( $context = 'view' )
- public get_date_created( $context = 'view' )
- public get_date_modified( $context = 'view' )
- public get_status( $context = 'view' )
- public get_featured( $context = 'view' )
- public get_catalog_visibility( $context = 'view' )
- public get_description( $context = 'view' )
- public get_short_description( $context = 'view' )
- public get_sku( $context = 'view' )
- public get_global_unique_id( $context = 'view' )
- public get_price( $context = 'view' )
- public get_regular_price( $context = 'view' )
- public get_sale_price( $context = 'view' )
- public get_date_on_sale_from( $context = 'view' )
- public get_date_on_sale_to( $context = 'view' )
- public get_total_sales( $context = 'view' )
- public get_tax_status( $context = 'view' )
- public get_tax_class( $context = 'view' )
- public get_manage_stock( $context = 'view' )
- public get_stock_quantity( $context = 'view' )
- public get_stock_status( $context = 'view' )
- public get_backorders( $context = 'view' )
- public get_low_stock_amount( $context = 'view' )
- public get_sold_individually( $context = 'view' )
- public get_weight( $context = 'view' )
- public get_length( $context = 'view' )
- public get_width( $context = 'view' )
- public get_height( $context = 'view' )
- public get_dimensions( $formatted = true )
- public get_upsell_ids( $context = 'view' )
- public get_cross_sell_ids( $context = 'view' )
- public get_parent_id( $context = 'view' )
- public get_reviews_allowed( $context = 'view' )
- public get_purchase_note( $context = 'view' )
- public get_attributes( $context = 'view' )
- public get_default_attributes( $context = 'view' )
- public get_menu_order( $context = 'view' )
- public get_post_password( $context = 'view' )
- public get_category_ids( $context = 'view' )
- public get_tag_ids( $context = 'view' )
- public get_brand_ids( $context = 'view' )
- public get_virtual( $context = 'view' )
- public get_gallery_image_ids( $context = 'view' )
- public get_shipping_class_id( $context = 'view' )
- public get_downloads( $context = 'view' )
- public get_download_expiry( $context = 'view' )
- public get_downloadable( $context = 'view' )
- public get_download_limit( $context = 'view' )
- public get_image_id( $context = 'view' )
- public get_rating_counts( $context = 'view' )
- public get_average_rating( $context = 'view' )
- public get_review_count( $context = 'view' )
- public set_name( $name )
- public set_slug( $slug )
- public set_date_created( $date = null )
- public set_date_modified( $date = null )
- public set_status( $status )
- public set_featured( $featured )
- public set_catalog_visibility( $visibility )
- public set_description( $description )
- public set_short_description( $short_description )
- public set_sku( $sku )
- public set_global_unique_id( $global_unique_id )
- public set_price( $price )
- public set_regular_price( $price )
- public set_sale_price( $price )
- public set_date_on_sale_from( $date = null )
- public set_date_on_sale_to( $date = null )
- public set_total_sales( $total )
- public set_tax_status( $status )
- public set_tax_class( $class )
- public set_manage_stock( $manage_stock )
- public set_stock_quantity( $quantity )
- public set_stock_status( $status = ProductStockStatus::IN_STOCK )
- public set_backorders( $backorders )
- public set_low_stock_amount( $amount )
- public set_sold_individually( $sold_individually )
- public set_weight( $weight )
- public set_length( $length )
- public set_width( $width )
- public set_height( $height )
- public set_upsell_ids( $upsell_ids )
- public set_cross_sell_ids( $cross_sell_ids )
- public set_parent_id( $parent_id )
- public set_reviews_allowed( $reviews_allowed )
- public set_purchase_note( $purchase_note )
- public set_attributes( $raw_attributes )
- public set_default_attributes( $default_attributes )
- public set_menu_order( $menu_order )
- public set_post_password( $post_password )
- public set_category_ids( $term_ids )
- public set_tag_ids( $term_ids )
- public set_brand_ids( $term_ids )
- public set_virtual( $virtual )
- public set_shipping_class_id( $id )
- public set_downloadable( $downloadable )
- public set_downloads( $downloads_array )
- public set_download_limit( $download_limit )
- public set_download_expiry( $download_expiry )
- public set_gallery_image_ids( $image_ids )
- public set_image_id( $image_id = '' )
- public set_rating_counts( $counts )
- public set_average_rating( $average )
- public set_review_count( $count )
- public validate_props()
- public save()
- public delete( $force_delete = false )
- public supports( $feature )
- public exists()
- public is_type( $type )
- public is_downloadable()
- public is_virtual()
- public is_featured()
- public is_sold_individually()
- public is_viewable()
- public is_publicly_viewable()
- public is_visible()
- public is_purchasable()
- public is_on_sale( $context = 'view' )
- public has_dimensions()
- public has_weight()
- public is_in_stock()
- public needs_shipping()
- public is_taxable()
- public is_shipping_taxable()
- public managing_stock()
- public backorders_allowed()
- public backorders_require_notification()
- public is_on_backorder( $qty_in_cart = 0 )
- public has_enough_stock( $quantity )
- public has_attributes()
- public has_child()
- public child_has_dimensions()
- public child_has_weight()
- public has_file( $download_id = '' )
- public has_options()
- public get_title()
- public get_permalink()
- public get_children()
- public get_stock_managed_by_id()
- public get_price_html( $deprecated = '' )
- public get_cogs_value_html()
- public get_formatted_name()
- public get_min_purchase_quantity()
- public get_max_purchase_quantity()
- public get_purchase_quantity_step()
- public add_to_cart_url()
- public single_add_to_cart_text()
- public add_to_cart_aria_describedby()
- public add_to_cart_text()
- public add_to_cart_description()
- public get_image( $size = 'woocommerce_thumbnail', $attr = array(), $placeholder = true )
- public get_shipping_class()
- public get_attribute( $attribute )
- public get_rating_count( $value = null )
- public get_file( $download_id = '' )
- public get_file_download_path( $download_id )
- public get_price_suffix( $price = '', $qty = 1 )
- public get_availability()
- public set_cogs_value( ?float $value )
- public get_cogs_value()
- public get_cogs_effective_value()
- public get_cogs_total_value()
- protected get_valid_tax_classes()
- private build_downloads_map( array $downloads )
- protected before_data_store_save_or_update()
- protected after_data_store_save_or_update( $state )
- protected maybe_defer_product_sync()
- protected is_visible_core()
- protected get_availability_text()
- protected get_availability_class()
- protected adjust_cogs_value_before_set( ?float $value )
- protected get_cogs_effective_value_core()
- protected get_cogs_total_value_core()
Examples
#1 Getting and outputting product data
The code is intended for a template that runs after WooCommerce is initialized.
$product = wc_get_product( 123 );
if ( ! $product ) {
return;
}
printf(
'<a href="%s">%s</a>',
esc_url( $product->get_permalink() ),
esc_html( $product->get_name() )
);
echo wp_kses_post( $product->get_image() );
echo wp_kses_post( $product->get_price_html() );
get_price() gets the price as a string, for example '1500'. If the price is not set, the result is ''. A zero price of '0' is allowed, so checking with empty() is not suitable here.
Use wc_get_price_to_display() when you need a numeric price that takes into account tax display settings in the store.
#2 Getting a value for display and editing
$product = wc_get_product( 123 );
if ( ! $product ) {
return;
}
$display_price = $product->get_price();
$edit_price = $product->get_price( 'edit' );
The first call allows the price to be modified by filters. The second gets the current property value without these filters.
#3 Changing the price of a simple product
Let's set the regular price and remove the sale price along with its schedule:
$product = wc_get_product( 123 );
if ( ! $product || ! $product->is_type( 'simple' ) ) {
return;
}
$product->set_regular_price( '1500' );
$product->set_sale_price( '' );
$product->set_date_on_sale_from( null );
$product->set_date_on_sale_to( null );
$product->save();
When saving, the default data store will recalculate the current price considering the changes.
For a variable product, prices are set on individual variations. The parent product price is synchronized with them.
#4 Creating a simple product
Let's create a draft with a price and stock handling:
$product = new WC_Product_Simple();
$result = $product->set_props( [
'name' => 'Футболка',
'status' => 'draft',
'regular_price' => '1500',
'manage_stock' => true,
'stock_quantity' => 10,
] );
if ( is_wp_error( $result ) ) {
return;
}
$product_id = $product->save();
The array keys correspond to property names without the set_ prefix. For example, regular_price is processed by the set_regular_price() method.
set_props() returns true or WP_Error. Unknown properties without a suitable setter method are skipped.
When saving a product with stock management enabled, the stock status is determined by quantity, the out-of-stock threshold, and backorder settings. If stock management is disabled, the quantity is reset to null.
#5 Saving a user meta-field
$product = wc_get_product( 123 );
if ( ! $product ) {
return;
}
$product->update_meta_data( '_supplier_code', 'ABC-123' );
$product->save();
$supplier_code = $product->get_meta( '_supplier_code' );
To delete a user meta-field, use delete_meta_data(), after which you also need to save changes:
$product->delete_meta_data( '_supplier_code' ); $product->save();
Modify service product properties using special methods: for example, SKU via set_sku(), price via set_regular_price().
Notes
| Package: WooCommerce\Abstracts |