PHP Class WC_API_Products

Inheritance: extends WC_API_Resource
Show file Open project: woocommerce/woocommerce Class Usage Examples

Protected Properties

Property Type Description
$base string the route base

Public Methods

Method Description
bulk ( array $data ) : array Bulk update or insert products Accepts an array with products in the formats supported by WC_API_Products->create_product() and WC_API_Products->edit_product()
create_product ( array $data ) : array Create a new product
create_product_attribute ( array $data ) : array Create a new product attribute
create_product_attribute_term ( integer $attribute_id, array $data ) : array Create a new product attribute term.
create_product_category ( array $data ) : array | WP_Error Create a new product category.
create_product_shipping_class ( array $data ) : array | WP_Error Create a new product shipping class.
create_product_tag ( array $data ) : array | WP_Error Create a new product tag.
delete_product ( integer $id, boolean $force = false ) : array Delete a product
delete_product_attribute ( integer $id ) : array Delete a product attribute
delete_product_attribute_term ( integer $attribute_id, integer $id ) : array Delete a product attribute term.
delete_product_category ( integer $id ) : array | WP_Error Delete a product category.
delete_product_shipping_class ( integer $id ) : array | WP_Error Delete a product shipping class.
delete_product_tag ( integer $id ) : array | WP_Error Delete a product tag.
edit_product ( integer $id, array $data ) : array Edit a product
edit_product_attribute ( integer $id, array $data ) : array Edit a product attribute
edit_product_attribute_term ( integer $attribute_id, integer $id, array $data ) : array Edit a product attribute term.
edit_product_category ( integer $id, array $data ) : array | WP_Error Edit a product category.
edit_product_shipping_class ( integer $id, array $data ) : array | WP_Error Edit a product shipping class.
edit_product_tag ( integer $id, array $data ) : array | WP_Error Edit a product tag.
get_product ( integer $id, string $fields = null ) : array Get the product for the given ID
get_product_attribute ( string $id, string | null $fields = null ) : array Get the product attribute for the given ID
get_product_attribute_term ( integer $attribute_id, string $id, string | null $fields = null ) : array Get the product attribute term for the given ID.
get_product_attribute_terms ( integer $attribute_id, string | null $fields = null ) : array Get a listing of product attribute terms.
get_product_attributes ( string | null $fields = null ) : array Get a listing of product attributes
get_product_by_sku ( integer $sku, string $fields = null ) : array Get product by SKU
get_product_categories ( string | null $fields = null ) : array Get a listing of product categories
get_product_category ( string $id, string | null $fields = null ) : array Get the product category for the given ID
get_product_orders ( integer $id, $fields = null, string $filter = [], string $status = null, $page = 1 ) : array Get the orders for a product
get_product_reviews ( integer $id, string $fields = null ) : array Get the reviews for a product
get_product_shipping_class ( string $id, string | null $fields = null ) : array | WP_Error Get the product shipping class for the given ID.
get_product_shipping_classes ( string | null $fields = null ) : array | WP_Error Get a listing of product shipping classes.
get_product_tag ( string $id, string | null $fields = null ) : array Get the product tag for the given ID.
get_product_tags ( string | null $fields = null ) : array Get a listing of product tags.
get_products ( string $fields = null, string $type = null, array $filter = [], integer $page = 1 ) : array Get all products
get_products_count ( string $type = null, array $filter = [] ) : array Get the total number of orders
register_routes ( array $routes ) : array Register the routes for this class
upload_product_category_image ( string $image_url ) : integer | WP_Error Upload product category image from URL.
upload_product_image ( string $image_url ) : integer | WP_Error Upload image from URL

Protected Methods

Method Description
clear_product ( $product_id ) Clear product
get_attribute_options ( integer $product_id, array $attribute ) : array Get attribute options.
save_default_attributes ( WC_Product $product, WP_REST_Request $request ) : WC_Product Save default attributes.
save_default_attributes ( WC_Product $product, array $request ) : WC_Product Save default attributes.
save_product_images ( WC_Product $product, array $images ) Save product images
save_product_images ( WC_Product $product, array $images ) : WC_Product Save product images.
save_product_meta ( WC_Product $product, array $data ) : WC_Product Save product meta
save_variations ( WC_Product $product, array $request ) : WC_Product Save variations
set_product_category_image_as_attachment ( integer $upload ) : integer Sets uploaded category image as attachment and returns the attachment ID.
set_product_image_as_attachment ( array $upload, integer $id ) : integer Sets product image as attachment and returns the attachment ID.
set_uploaded_image_as_attachment ( array $upload, integer $id ) : integer Set uploaded image as attachment.
upload_image_from_url ( string $image_url, string $upload_for = 'product_image' ) : integer | WP_Error Upload image from URL.
validate_attribute_data ( string $name, string $slug, string $type, string $order_by, boolean $new_data = true ) : boolean Validate attribute data.

Private Methods

Method Description
get_attribute_taxonomy_by_slug ( string $slug ) : string | null Get attribute taxonomy by slug.
get_attributes ( WC_Product | WC_Product_Variation $product ) : array Get the attributes for a product or product variation
get_downloads ( WC_Product | WC_Product_Variation $product ) : array Get the downloads for a product or product variation
get_grouped_products_data ( WC_Product $product ) : array Get grouped products data
get_images ( WC_Product | WC_Product_Variation $product ) : array Get the images for a product or product variation
get_product_data ( WC_Product $product ) : WC_Product Get standard product data that applies to every product type
get_product_data ( WC_Product $product ) : array Get standard product data that applies to every product type
get_product_menu_order ( WC_Product $product ) : integer Get product menu order.
get_variation_data ( WC_Product $product ) : array Get an individual variation's data
query_products ( array $args ) : WP_Query Helper method to get product post objects
save_downloadable_files ( WC_Product $product, array $downloads, integer $deprecated ) : WC_Product Save downloadable files
save_product_shipping_data ( WC_Product $product, array $data ) : WC_Product Save product shipping data

Method Details

bulk() public method

Bulk update or insert products Accepts an array with products in the formats supported by WC_API_Products->create_product() and WC_API_Products->edit_product()
Since: 2.4.0
public bulk ( array $data ) : array
$data array
return array

clear_product() protected method

Clear product
protected clear_product ( $product_id )

create_product() public method

Create a new product
Since: 2.2
public create_product ( array $data ) : array
$data array posted data
return array

create_product_attribute() public method

Create a new product attribute
Since: 2.4.0
public create_product_attribute ( array $data ) : array
$data array posted data
return array

create_product_attribute_term() public method

Create a new product attribute term.
Since: 2.5.0
public create_product_attribute_term ( integer $attribute_id, array $data ) : array
$attribute_id integer Attribute ID.
$data array Posted data.
return array

create_product_category() public method

Create a new product category.
Since: 2.5.0
public create_product_category ( array $data ) : array | WP_Error
$data array Posted data
return array | WP_Error Product category if succeed, otherwise WP_Error will be returned

create_product_shipping_class() public method

Create a new product shipping class.
Since: 2.5.0
public create_product_shipping_class ( array $data ) : array | WP_Error
$data array Posted data
return array | WP_Error Product shipping class if succeed, otherwise WP_Error will be returned

create_product_tag() public method

Create a new product tag.
Since: 2.5.0
public create_product_tag ( array $data ) : array | WP_Error
$data array Posted data
return array | WP_Error Product tag if succeed, otherwise WP_Error will be returned

delete_product() public method

Delete a product
public delete_product ( integer $id, boolean $force = false ) : array
$id integer the product ID
$force boolean true to permanently delete order, false to move to trash
return array

delete_product_attribute() public method

Delete a product attribute
Since: 2.4.0
public delete_product_attribute ( integer $id ) : array
$id integer the product attribute ID
return array

delete_product_attribute_term() public method

Delete a product attribute term.
Since: 2.5.0
public delete_product_attribute_term ( integer $attribute_id, integer $id ) : array
$attribute_id integer Attribute ID.
$id integer the product attribute ID.
return array

delete_product_category() public method

Delete a product category.
Since: 2.5.0
public delete_product_category ( integer $id ) : array | WP_Error
$id integer Product category term ID
return array | WP_Error Success message if succeed, otherwise WP_Error will be returned

delete_product_shipping_class() public method

Delete a product shipping class.
Since: 2.5.0
public delete_product_shipping_class ( integer $id ) : array | WP_Error
$id integer Product shipping class term ID
return array | WP_Error Success message if succeed, otherwise WP_Error will be returned

delete_product_tag() public method

Delete a product tag.
Since: 2.5.0
public delete_product_tag ( integer $id ) : array | WP_Error
$id integer Product tag term ID
return array | WP_Error Success message if succeed, otherwise WP_Error will be returned

edit_product() public method

Edit a product
public edit_product ( integer $id, array $data ) : array
$id integer the product ID
$data array
return array

edit_product_attribute() public method

Edit a product attribute
Since: 2.4.0
public edit_product_attribute ( integer $id, array $data ) : array
$id integer the attribute ID
$data array
return array

edit_product_attribute_term() public method

Edit a product attribute term.
Since: 2.5.0
public edit_product_attribute_term ( integer $attribute_id, integer $id, array $data ) : array
$attribute_id integer Attribute ID.
$id integer the attribute ID.
$data array
return array

edit_product_category() public method

Edit a product category.
Since: 2.5.0
public edit_product_category ( integer $id, array $data ) : array | WP_Error
$id integer Product category term ID
$data array Posted data
return array | WP_Error Product category if succeed, otherwise WP_Error will be returned

edit_product_shipping_class() public method

Edit a product shipping class.
Since: 2.5.0
public edit_product_shipping_class ( integer $id, array $data ) : array | WP_Error
$id integer Product shipping class term ID
$data array Posted data
return array | WP_Error Product shipping class if succeed, otherwise WP_Error will be returned

edit_product_tag() public method

Edit a product tag.
Since: 2.5.0
public edit_product_tag ( integer $id, array $data ) : array | WP_Error
$id integer Product tag term ID
$data array Posted data
return array | WP_Error Product tag if succeed, otherwise WP_Error will be returned

get_attribute_options() protected method

Get attribute options.
protected get_attribute_options ( integer $product_id, array $attribute ) : array
$product_id integer
$attribute array
return array

get_product() public method

Get the product for the given ID
Since: 2.1
public get_product ( integer $id, string $fields = null ) : array
$id integer the product ID
$fields string
return array

get_product_attribute() public method

Get the product attribute for the given ID
Since: 2.4.0
public get_product_attribute ( string $id, string | null $fields = null ) : array
$id string product attribute term ID
$fields string | null fields to limit response to
return array

get_product_attribute_term() public method

Get the product attribute term for the given ID.
Since: 2.5.0
public get_product_attribute_term ( integer $attribute_id, string $id, string | null $fields = null ) : array
$attribute_id integer Attribute ID.
$id string Product attribute term ID.
$fields string | null Fields to limit response to.
return array

get_product_attribute_terms() public method

Get a listing of product attribute terms.
Since: 2.5.0
public get_product_attribute_terms ( integer $attribute_id, string | null $fields = null ) : array
$attribute_id integer Attribute ID.
$fields string | null Fields to limit response to.
return array

get_product_attributes() public method

Get a listing of product attributes
Since: 2.4.0
public get_product_attributes ( string | null $fields = null ) : array
$fields string | null fields to limit response to
return array

get_product_by_sku() public method

Get product by SKU
Deprecation: 2.4.0
Since: 2.3.0
public get_product_by_sku ( integer $sku, string $fields = null ) : array
$sku integer the product SKU
$fields string
return array

get_product_categories() public method

Get a listing of product categories
Since: 2.2
public get_product_categories ( string | null $fields = null ) : array
$fields string | null fields to limit response to
return array

get_product_category() public method

Get the product category for the given ID
Since: 2.2
public get_product_category ( string $id, string | null $fields = null ) : array
$id string product category term ID
$fields string | null fields to limit response to
return array

get_product_orders() public method

Get the orders for a product
Since: 2.4.0
public get_product_orders ( integer $id, $fields = null, string $filter = [], string $status = null, $page = 1 ) : array
$id integer the product ID to get orders for
$filter string filters to include in response
$status string the order status to retrieve
$page $page page to retrieve
return array

get_product_reviews() public method

Get the reviews for a product
Since: 2.1
public get_product_reviews ( integer $id, string $fields = null ) : array
$id integer the product ID to get reviews for
$fields string fields to include in response
return array

get_product_shipping_class() public method

Get the product shipping class for the given ID.
Since: 2.5.0
public get_product_shipping_class ( string $id, string | null $fields = null ) : array | WP_Error
$id string Product shipping class term ID
$fields string | null Fields to limit response to
return array | WP_Error Product shipping class if succeed, otherwise WP_Error will be returned

get_product_shipping_classes() public method

Get a listing of product shipping classes.
Since: 2.5.0
public get_product_shipping_classes ( string | null $fields = null ) : array | WP_Error
$fields string | null Fields to limit response to
return array | WP_Error List of product shipping classes if succeed, otherwise WP_Error will be returned

get_product_tag() public method

Get the product tag for the given ID.
Since: 2.5.0
public get_product_tag ( string $id, string | null $fields = null ) : array
$id string Product tag term ID
$fields string | null Fields to limit response to
return array Product tag

get_product_tags() public method

Get a listing of product tags.
Since: 2.5.0
public get_product_tags ( string | null $fields = null ) : array
$fields string | null Fields to limit response to
return array Product tags

get_products() public method

Get all products
Since: 2.1
public get_products ( string $fields = null, string $type = null, array $filter = [], integer $page = 1 ) : array
$fields string
$type string
$filter array
$page integer
return array

get_products_count() public method

Get the total number of orders
Since: 2.1
public get_products_count ( string $type = null, array $filter = [] ) : array
$type string
$filter array
return array

register_routes() public method

GET /products GET /products/count GET /products/ GET /products//reviews
Since: 2.1
public register_routes ( array $routes ) : array
$routes array
return array

save_default_attributes() protected method

Save default attributes.
Since: 2.7.0
protected save_default_attributes ( WC_Product $product, WP_REST_Request $request ) : WC_Product
$product WC_Product
$request WP_REST_Request
return WC_Product

save_default_attributes() protected method

Save default attributes.
Since: 2.7.0
protected save_default_attributes ( WC_Product $product, array $request ) : WC_Product
$product WC_Product
$request array
return WC_Product

save_product_images() protected method

Save product images
Since: 2.2
protected save_product_images ( WC_Product $product, array $images )
$product WC_Product
$images array

save_product_images() protected method

Save product images.
Since: 2.2
protected save_product_images ( WC_Product $product, array $images ) : WC_Product
$product WC_Product
$images array
return WC_Product

save_product_meta() protected method

Save product meta
Since: 2.2
protected save_product_meta ( WC_Product $product, array $data ) : WC_Product
$product WC_Product
$data array
return WC_Product

save_variations() protected method

Save variations
Since: 2.2
protected save_variations ( WC_Product $product, array $request ) : WC_Product
$product WC_Product
$request array
return WC_Product

set_product_category_image_as_attachment() protected method

Sets uploaded category image as attachment and returns the attachment ID.
Since: 2.5.0
protected set_product_category_image_as_attachment ( integer $upload ) : integer
$upload integer Upload information from wp_upload_bits
return integer Attachment ID

set_product_image_as_attachment() protected method

Sets product image as attachment and returns the attachment ID.
Since: 2.2
protected set_product_image_as_attachment ( array $upload, integer $id ) : integer
$upload array
$id integer
return integer

set_uploaded_image_as_attachment() protected method

Set uploaded image as attachment.
Since: 2.5.0
protected set_uploaded_image_as_attachment ( array $upload, integer $id ) : integer
$upload array Upload information from wp_upload_bits
$id integer Post ID. Default to 0.
return integer Attachment ID

upload_image_from_url() protected method

Upload image from URL.
Since: 2.5.0
protected upload_image_from_url ( string $image_url, string $upload_for = 'product_image' ) : integer | WP_Error
$image_url string
$upload_for string
return integer | WP_Error Attachment id

upload_product_category_image() public method

Upload product category image from URL.
Since: 2.5.0
public upload_product_category_image ( string $image_url ) : integer | WP_Error
$image_url string
return integer | WP_Error attachment id

upload_product_image() public method

Upload image from URL
Since: 2.2
public upload_product_image ( string $image_url ) : integer | WP_Error
$image_url string
return integer | WP_Error attachment id

validate_attribute_data() protected method

Validate attribute data.
Since: 2.4.0
protected validate_attribute_data ( string $name, string $slug, string $type, string $order_by, boolean $new_data = true ) : boolean
$name string
$slug string
$type string
$order_by string
$new_data boolean
return boolean

Property Details

$base protected property

the route base
protected string $base
return string