WC_Product_Data_Store_CPT::get_product_type()publicWC 3.0.0

Get the product type based on product ID.

Method of the class: WC_Product_Data_Store_CPT{}

No Hooks.

Return

true|false|String.

Usage

$WC_Product_Data_Store_CPT = new WC_Product_Data_Store_CPT();
$WC_Product_Data_Store_CPT->get_product_type( $product_id );
$product_id(int) (required)
Product ID.

Changelog

Since 3.0.0 Introduced.

WC_Product_Data_Store_CPT::get_product_type() code WC 8.6.1

public function get_product_type( $product_id ) {
	$cache_key    = WC_Cache_Helper::get_cache_prefix( 'product_' . $product_id ) . '_type_' . $product_id;
	$product_type = wp_cache_get( $cache_key, 'products' );

	if ( $product_type ) {
		return $product_type;
	}

	$post_type = get_post_type( $product_id );

	if ( 'product_variation' === $post_type ) {
		$product_type = 'variation';
	} elseif ( 'product' === $post_type ) {
		$terms        = get_the_terms( $product_id, 'product_type' );
		$product_type = ! empty( $terms ) && ! is_wp_error( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple';
	} else {
		$product_type = false;
	}

	wp_cache_set( $cache_key, $product_type, 'products' );

	return $product_type;
}