Helper to get term ids

This commit is contained in:
Mike Jolley 2016-12-08 17:01:39 +00:00
parent 90ff6705be
commit 767a1e500e
5 changed files with 59 additions and 26 deletions

View File

@ -29,7 +29,6 @@ class WC_Product_Factory {
if ( ! $product_id ) {
return false;
}
$product_type = $this->get_product_type( $product_id );
$classname = $this->get_classname_from_product_type( $product_type );
@ -64,6 +63,7 @@ class WC_Product_Factory {
$override = apply_filters( 'woocommerce_product_type_query', false, $product_id );
if ( ! $override ) {
$post_type = get_post_type( $product_id );
if ( 'product_variation' === $post_type ) {
return 'variation';
} elseif ( 'product' === $post_type ) {

View File

@ -615,16 +615,17 @@ class WC_Query {
}
}
$product_visibility_not_in = array( is_search() && $main_query ? 'exclude-from-search' : 'exclude-from-catalog' );
$product_visibiity_terms = wc_get_product_visibility_term_ids();
$product_visibility_not_in = array( is_search() && $main_query ? $product_visibiity_terms['exclude-from-search'] : $product_visibiity_terms['exclude-from-catalog'] );
// Hide out of stock products.
if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
$product_visibility_not_in[] = 'outofstock';
$product_visibility_not_in[] = $product_visibiity_terms['outofstock'];
}
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'field' => 'term_taxonomy_id',
'terms' => $product_visibility_not_in,
'operator' => 'NOT IN',
);

View File

@ -656,6 +656,8 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
* @since 2.7.0
*/
public function get_featured_product_ids() {
$product_visibility_term_ids = wc_get_product_visibility_term_ids();
return get_posts( array(
'post_type' => array( 'product', 'product_variation' ),
'posts_per_page' => -1,
@ -664,13 +666,13 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
'relation' => 'AND',
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => array( 'featured' ),
'field' => 'term_taxonomy_id',
'terms' => array( $product_visibility_term_ids['featured'] ),
),
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => array( 'exclude-from-catalog' ),
'field' => 'term_taxonomy_id',
'terms' => array( $product_visibility_term_ids['exclude-from-catalog'] ),
'operator' => 'NOT IN',
),
),
@ -870,12 +872,14 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
$query['where'] .= " AND p.post_type = 'product'";
$query['where'] .= " AND p.ID NOT IN ( {$exclude_ids} )";
if ( $exclude_catalog_term = get_term_by( 'name', 'exclude-from-catalog', 'product_visibility' ) ) {
$query['where'] .= " AND t.term_id !=" . absint( $exclude_catalog_term->term_id );
$product_visibility_term_ids = wc_get_product_visibility_term_ids();
if ( $product_visibility_term_ids['exclude-from-catalog'] ) {
$query['where'] .= " AND t.term_id !=" . $product_visibility_term_ids['exclude-from-catalog'];
}
if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && ( $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' ) ) ) {
$query['where'] .= " AND t.term_id !=" . absint( $outofstock_term->term_id );
if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && $product_visibility_term_ids['outofstock'] ) {
$query['where'] .= " AND t.term_id !=" . $product_visibility_term_ids['outofstock'];
}
if ( $cats_array || $tags_array ) {

View File

@ -559,12 +559,14 @@ function _wc_term_recount( $terms, $taxonomy, $callback = true, $terms_are_term_
AND post_type = 'product'
";
if ( $exclude_catalog_term = get_term_by( 'name', 'exclude-from-catalog', 'product_visibility' ) ) {
$count_query .= "AND term_id !=" . absint( $exclude_catalog_term->term_id );
$product_visibility_term_ids = wc_get_product_visibility_term_ids();
if ( $product_visibility_term_ids['exclude-from-catalog'] ) {
$count_query .= "AND term_taxonomy_id !=" . $product_visibility_term_ids['exclude-from-catalog'];
}
if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && ( $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' ) ) ) {
$count_query .= "AND term_id !=" . absint( $outofstock_term->term_id );
if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && $product_visibility_term_ids['outofstock'] ) {
$count_query .= "AND term_taxonomy_id !=" . $product_visibility_term_ids['outofstock'];
}
// Pre-process term taxonomy ids
@ -733,3 +735,28 @@ function wc_clear_term_product_ids( $object_id, $terms, $tt_ids, $taxonomy, $app
}
}
add_action( 'set_object_terms', 'wc_clear_term_product_ids', 10, 6 );
/**
* Get full list of product visibilty term ids.
*
* @since 2.7.0
* @return int[]
*/
function wc_get_product_visibility_term_ids() {
return array_map( 'absint', wp_parse_args(
wp_list_pluck(
get_terms( array(
'taxonomy' => 'product_visibility',
'hide_empty' => false,
) ),
'term_taxonomy_id',
'name'
),
array(
'exclude-from-catalog' => 0,
'exclude-from-search' => 0,
'featured' => 0,
'outofstock' => 0,
)
) );
}

View File

@ -93,6 +93,7 @@ class WC_Widget_Products extends WC_Widget {
$show = ! empty( $instance['show'] ) ? sanitize_title( $instance['show'] ) : $this->settings['show']['std'];
$orderby = ! empty( $instance['orderby'] ) ? sanitize_title( $instance['orderby'] ) : $this->settings['orderby']['std'];
$order = ! empty( $instance['order'] ) ? sanitize_title( $instance['order'] ) : $this->settings['order']['std'];
$product_visibility_term_ids = wc_get_product_visibility_term_ids();
$query_args = array(
'posts_per_page' => $number,
@ -109,8 +110,8 @@ class WC_Widget_Products extends WC_Widget {
if ( empty( $instance['show_hidden'] ) ) {
$query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => is_search() ? 'exclude-from-search' : 'exclude-from-catalog',
'field' => 'term_taxonomy_id',
'terms' => is_search() ? $product_visibility_term_ids['exclude-from-search'] : $product_visibility_term_ids['exclude-from-catalog'],
'operator' => 'NOT IN',
);
$query_args['post_parent'] = 0;
@ -129,8 +130,8 @@ class WC_Widget_Products extends WC_Widget {
$query_args['tax_query'] = array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'outofstock',
'field' => 'term_taxonomy_id',
'terms' => $product_visibility_term_ids['outofstock'],
'operator' => 'NOT IN',
),
);
@ -140,8 +141,8 @@ class WC_Widget_Products extends WC_Widget {
case 'featured' :
$query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'field' => 'term_taxonomy_id',
'terms' => $product_visibility_term_ids['featured'],
);
break;
case 'onsale' :