WC_Product{} │ WC 1.0└─ 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.

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_Product object - 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 function wc_get_product() catches the exception and in such case returns false.

Default: 0

Methods

  1. public __construct( $product = 0 )
  2. public get_type()
  3. public get_name( $context = 'view' )
  4. public get_slug( $context = 'view' )
  5. public get_date_created( $context = 'view' )
  6. public get_date_modified( $context = 'view' )
  7. public get_status( $context = 'view' )
  8. public get_featured( $context = 'view' )
  9. public get_catalog_visibility( $context = 'view' )
  10. public get_description( $context = 'view' )
  11. public get_short_description( $context = 'view' )
  12. public get_sku( $context = 'view' )
  13. public get_global_unique_id( $context = 'view' )
  14. public get_price( $context = 'view' )
  15. public get_regular_price( $context = 'view' )
  16. public get_sale_price( $context = 'view' )
  17. public get_date_on_sale_from( $context = 'view' )
  18. public get_date_on_sale_to( $context = 'view' )
  19. public get_total_sales( $context = 'view' )
  20. public get_tax_status( $context = 'view' )
  21. public get_tax_class( $context = 'view' )
  22. public get_manage_stock( $context = 'view' )
  23. public get_stock_quantity( $context = 'view' )
  24. public get_stock_status( $context = 'view' )
  25. public get_backorders( $context = 'view' )
  26. public get_low_stock_amount( $context = 'view' )
  27. public get_sold_individually( $context = 'view' )
  28. public get_weight( $context = 'view' )
  29. public get_length( $context = 'view' )
  30. public get_width( $context = 'view' )
  31. public get_height( $context = 'view' )
  32. public get_dimensions( $formatted = true )
  33. public get_upsell_ids( $context = 'view' )
  34. public get_cross_sell_ids( $context = 'view' )
  35. public get_parent_id( $context = 'view' )
  36. public get_reviews_allowed( $context = 'view' )
  37. public get_purchase_note( $context = 'view' )
  38. public get_attributes( $context = 'view' )
  39. public get_default_attributes( $context = 'view' )
  40. public get_menu_order( $context = 'view' )
  41. public get_post_password( $context = 'view' )
  42. public get_category_ids( $context = 'view' )
  43. public get_tag_ids( $context = 'view' )
  44. public get_brand_ids( $context = 'view' )
  45. public get_virtual( $context = 'view' )
  46. public get_gallery_image_ids( $context = 'view' )
  47. public get_shipping_class_id( $context = 'view' )
  48. public get_downloads( $context = 'view' )
  49. public get_download_expiry( $context = 'view' )
  50. public get_downloadable( $context = 'view' )
  51. public get_download_limit( $context = 'view' )
  52. public get_image_id( $context = 'view' )
  53. public get_rating_counts( $context = 'view' )
  54. public get_average_rating( $context = 'view' )
  55. public get_review_count( $context = 'view' )
  56. public set_name( $name )
  57. public set_slug( $slug )
  58. public set_date_created( $date = null )
  59. public set_date_modified( $date = null )
  60. public set_status( $status )
  61. public set_featured( $featured )
  62. public set_catalog_visibility( $visibility )
  63. public set_description( $description )
  64. public set_short_description( $short_description )
  65. public set_sku( $sku )
  66. public set_global_unique_id( $global_unique_id )
  67. public set_price( $price )
  68. public set_regular_price( $price )
  69. public set_sale_price( $price )
  70. public set_date_on_sale_from( $date = null )
  71. public set_date_on_sale_to( $date = null )
  72. public set_total_sales( $total )
  73. public set_tax_status( $status )
  74. public set_tax_class( $class )
  75. public set_manage_stock( $manage_stock )
  76. public set_stock_quantity( $quantity )
  77. public set_stock_status( $status = ProductStockStatus::IN_STOCK )
  78. public set_backorders( $backorders )
  79. public set_low_stock_amount( $amount )
  80. public set_sold_individually( $sold_individually )
  81. public set_weight( $weight )
  82. public set_length( $length )
  83. public set_width( $width )
  84. public set_height( $height )
  85. public set_upsell_ids( $upsell_ids )
  86. public set_cross_sell_ids( $cross_sell_ids )
  87. public set_parent_id( $parent_id )
  88. public set_reviews_allowed( $reviews_allowed )
  89. public set_purchase_note( $purchase_note )
  90. public set_attributes( $raw_attributes )
  91. public set_default_attributes( $default_attributes )
  92. public set_menu_order( $menu_order )
  93. public set_post_password( $post_password )
  94. public set_category_ids( $term_ids )
  95. public set_tag_ids( $term_ids )
  96. public set_brand_ids( $term_ids )
  97. public set_virtual( $virtual )
  98. public set_shipping_class_id( $id )
  99. public set_downloadable( $downloadable )
  100. public set_downloads( $downloads_array )
  101. public set_download_limit( $download_limit )
  102. public set_download_expiry( $download_expiry )
  103. public set_gallery_image_ids( $image_ids )
  104. public set_image_id( $image_id = '' )
  105. public set_rating_counts( $counts )
  106. public set_average_rating( $average )
  107. public set_review_count( $count )
  108. public validate_props()
  109. public save()
  110. public delete( $force_delete = false )
  111. public supports( $feature )
  112. public exists()
  113. public is_type( $type )
  114. public is_downloadable()
  115. public is_virtual()
  116. public is_featured()
  117. public is_sold_individually()
  118. public is_viewable()
  119. public is_publicly_viewable()
  120. public is_visible()
  121. public is_purchasable()
  122. public is_on_sale( $context = 'view' )
  123. public has_dimensions()
  124. public has_weight()
  125. public is_in_stock()
  126. public needs_shipping()
  127. public is_taxable()
  128. public is_shipping_taxable()
  129. public managing_stock()
  130. public backorders_allowed()
  131. public backorders_require_notification()
  132. public is_on_backorder( $qty_in_cart = 0 )
  133. public has_enough_stock( $quantity )
  134. public has_attributes()
  135. public has_child()
  136. public child_has_dimensions()
  137. public child_has_weight()
  138. public has_file( $download_id = '' )
  139. public has_options()
  140. public get_title()
  141. public get_permalink()
  142. public get_children()
  143. public get_stock_managed_by_id()
  144. public get_price_html( $deprecated = '' )
  145. public get_cogs_value_html()
  146. public get_formatted_name()
  147. public get_min_purchase_quantity()
  148. public get_max_purchase_quantity()
  149. public get_purchase_quantity_step()
  150. public add_to_cart_url()
  151. public single_add_to_cart_text()
  152. public add_to_cart_aria_describedby()
  153. public add_to_cart_text()
  154. public add_to_cart_description()
  155. public get_image( $size = 'woocommerce_thumbnail', $attr = array(), $placeholder = true )
  156. public get_shipping_class()
  157. public get_attribute( $attribute )
  158. public get_rating_count( $value = null )
  159. public get_file( $download_id = '' )
  160. public get_file_download_path( $download_id )
  161. public get_price_suffix( $price = '', $qty = 1 )
  162. public get_availability()
  163. public set_cogs_value( ?float $value )
  164. public get_cogs_value()
  165. public get_cogs_effective_value()
  166. public get_cogs_total_value()
  167. protected get_valid_tax_classes()
  168. private build_downloads_map( array $downloads )
  169. protected before_data_store_save_or_update()
  170. protected after_data_store_save_or_update( $state )
  171. protected maybe_defer_product_sync()
  172. protected is_visible_core()
  173. protected get_availability_text()
  174. protected get_availability_class()
  175. protected adjust_cogs_value_before_set( ?float $value )
  176. protected get_cogs_effective_value_core()
  177. 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

WC_Product{} code WC 11.2.1

The code is too large. See it here: woocommerce/includes/abstracts/abstract-wc-product.php