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 ) { if ( ! $product_id ) {
return false; return false;
} }
$product_type = $this->get_product_type( $product_id ); $product_type = $this->get_product_type( $product_id );
$classname = $this->get_classname_from_product_type( $product_type ); $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 ); $override = apply_filters( 'woocommerce_product_type_query', false, $product_id );
if ( ! $override ) { if ( ! $override ) {
$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'; return 'variation';
} elseif ( 'product' === $post_type ) { } 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. // Hide out of stock products.
if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) { 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( $tax_query[] = array(
'taxonomy' => 'product_visibility', 'taxonomy' => 'product_visibility',
'field' => 'name', 'field' => 'term_taxonomy_id',
'terms' => $product_visibility_not_in, 'terms' => $product_visibility_not_in,
'operator' => '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 * @since 2.7.0
*/ */
public function get_featured_product_ids() { public function get_featured_product_ids() {
$product_visibility_term_ids = wc_get_product_visibility_term_ids();
return get_posts( array( return get_posts( array(
'post_type' => array( 'product', 'product_variation' ), 'post_type' => array( 'product', 'product_variation' ),
'posts_per_page' => -1, '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', 'relation' => 'AND',
array( array(
'taxonomy' => 'product_visibility', 'taxonomy' => 'product_visibility',
'field' => 'name', 'field' => 'term_taxonomy_id',
'terms' => array( 'featured' ), 'terms' => array( $product_visibility_term_ids['featured'] ),
), ),
array( array(
'taxonomy' => 'product_visibility', 'taxonomy' => 'product_visibility',
'field' => 'name', 'field' => 'term_taxonomy_id',
'terms' => array( 'exclude-from-catalog' ), 'terms' => array( $product_visibility_term_ids['exclude-from-catalog'] ),
'operator' => 'NOT IN', '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.post_type = 'product'";
$query['where'] .= " AND p.ID NOT IN ( {$exclude_ids} )"; $query['where'] .= " AND p.ID NOT IN ( {$exclude_ids} )";
if ( $exclude_catalog_term = get_term_by( 'name', 'exclude-from-catalog', 'product_visibility' ) ) { $product_visibility_term_ids = wc_get_product_visibility_term_ids();
$query['where'] .= " AND t.term_id !=" . absint( $exclude_catalog_term->term_id );
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' ) ) ) { if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && $product_visibility_term_ids['outofstock'] ) {
$query['where'] .= " AND t.term_id !=" . absint( $outofstock_term->term_id ); $query['where'] .= " AND t.term_id !=" . $product_visibility_term_ids['outofstock'];
} }
if ( $cats_array || $tags_array ) { 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' AND post_type = 'product'
"; ";
if ( $exclude_catalog_term = get_term_by( 'name', 'exclude-from-catalog', 'product_visibility' ) ) { $product_visibility_term_ids = wc_get_product_visibility_term_ids();
$count_query .= "AND term_id !=" . absint( $exclude_catalog_term->term_id );
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' ) ) ) { if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && $product_visibility_term_ids['outofstock'] ) {
$count_query .= "AND term_id !=" . absint( $outofstock_term->term_id ); $count_query .= "AND term_taxonomy_id !=" . $product_visibility_term_ids['outofstock'];
} }
// Pre-process term taxonomy ids // 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 ); 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

@ -89,10 +89,11 @@ class WC_Widget_Products extends WC_Widget {
* @return WP_Query * @return WP_Query
*/ */
public function get_products( $args, $instance ) { public function get_products( $args, $instance ) {
$number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std']; $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std'];
$show = ! empty( $instance['show'] ) ? sanitize_title( $instance['show'] ) : $this->settings['show']['std']; $show = ! empty( $instance['show'] ) ? sanitize_title( $instance['show'] ) : $this->settings['show']['std'];
$orderby = ! empty( $instance['orderby'] ) ? sanitize_title( $instance['orderby'] ) : $this->settings['orderby']['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']; $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( $query_args = array(
'posts_per_page' => $number, 'posts_per_page' => $number,
@ -109,8 +110,8 @@ class WC_Widget_Products extends WC_Widget {
if ( empty( $instance['show_hidden'] ) ) { if ( empty( $instance['show_hidden'] ) ) {
$query_args['tax_query'][] = array( $query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility', 'taxonomy' => 'product_visibility',
'field' => 'name', 'field' => 'term_taxonomy_id',
'terms' => is_search() ? 'exclude-from-search' : 'exclude-from-catalog', 'terms' => is_search() ? $product_visibility_term_ids['exclude-from-search'] : $product_visibility_term_ids['exclude-from-catalog'],
'operator' => 'NOT IN', 'operator' => 'NOT IN',
); );
$query_args['post_parent'] = 0; $query_args['post_parent'] = 0;
@ -129,8 +130,8 @@ class WC_Widget_Products extends WC_Widget {
$query_args['tax_query'] = array( $query_args['tax_query'] = array(
array( array(
'taxonomy' => 'product_visibility', 'taxonomy' => 'product_visibility',
'field' => 'name', 'field' => 'term_taxonomy_id',
'terms' => 'outofstock', 'terms' => $product_visibility_term_ids['outofstock'],
'operator' => 'NOT IN', 'operator' => 'NOT IN',
), ),
); );
@ -140,8 +141,8 @@ class WC_Widget_Products extends WC_Widget {
case 'featured' : case 'featured' :
$query_args['tax_query'][] = array( $query_args['tax_query'][] = array(
'taxonomy' => 'product_visibility', 'taxonomy' => 'product_visibility',
'field' => 'name', 'field' => 'term_taxonomy_id',
'terms' => 'featured', 'terms' => $product_visibility_term_ids['featured'],
); );
break; break;
case 'onsale' : case 'onsale' :