get_product_type cache

This commit is contained in:
Mike Jolley 2019-01-30 14:13:34 +00:00
parent cb1c579262
commit 57ccde6643
1 changed files with 20 additions and 8 deletions

View File

@ -1503,15 +1503,27 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
* @return bool|string * @return bool|string
*/ */
public function get_product_type( $product_id ) { public function get_product_type( $product_id ) {
$cache_key = WC_Cache_Helper::get_cache_prefix( 'product_' . $product->get_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 ); $post_type = get_post_type( $product_id );
if ( 'product_variation' === $post_type ) { if ( 'product_variation' === $post_type ) {
return 'variation'; $product_type = 'variation';
} elseif ( 'product' === $post_type ) { } elseif ( 'product' === $post_type ) {
$terms = get_the_terms( $product_id, 'product_type' ); $terms = get_the_terms( $product_id, 'product_type' );
return ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple'; $product_type = ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple';
} else { } else {
return false; $product_type = false;
} }
wp_cache_set( $cache_key, $product_type, 'products' );
return $product_type;
} }
/** /**