WC_API_Products::get_product_by_sku()publicWC 2.3.0

Deprecated from version 2.4.0. It is no longer supported and can be removed in future releases. It is recommended to replace this function with the same one.

Get product by SKU

Method of the class: WC_API_Products{}

No Hooks.

Return

Array|WP_Error.

Usage

$WC_API_Products = new WC_API_Products();
$WC_API_Products->get_product_by_sku( $sku, $fields );
$sku(int) (required)
the product SKU
$fields(string)
-
Default: null

Changelog

Since 2.3.0 Introduced.
Deprecated since 2.4.0

WC_API_Products::get_product_by_sku() code WC 8.7.0

public function get_product_by_sku( $sku, $fields = null ) {
	try {
		$id = wc_get_product_id_by_sku( $sku );

		if ( empty( $id ) ) {
			throw new WC_API_Exception( 'woocommerce_api_invalid_product_sku', __( 'Invalid product SKU', 'woocommerce' ), 404 );
		}

		return $this->get_product( $id, $fields );
	} catch ( WC_API_Exception $e ) {
		return new WP_Error( $e->getErrorCode(), $e->getMessage(), array( 'status' => $e->getCode() ) );
	}
}