I'm using get_terms to show a list of terms. It works fine so far.
But I want to hide every term with out of stock products.
If I use 'hide_empty' => true it wouldn't work because the products are there and published.
Is there a way to add the _stock meta field to the get_terms function?
Unfortunately I have to use get_terms.
Here's my code (it's a lot bigger but that's the part):
$terms = get_terms( array(
'taxonomy' => 'product_tax',
'orderby' => 'name',
'hide_empty' => true,
) );
5 months ago but maybe it will help someone : I'm facing the same issue and all I found is to make a foreach to unset the empties values.
foreach ($terms as $key => $term) {
$args = array(
'post_type' => 'product',
'paged' => 1,
'posts_per_page' => 1,
'order' => 'DESC',
'post_status' => 'publish',
'orderby' => 'publish_date',
'meta_query' => array( array(
'key' => '_stock_status',
'value' => 'instock',
) ),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $term->term_id
)
)
);
$loop = new WP_Query( $args );
if($loop->post_count < 1) {
unset($terms[$key]);
}
}
Related
I made a category selection section that can be used to specify the woocommerce product category and then display the products of that category.
$args = array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => intval($_POST['category']),
'operator' => 'IN'
)
));
$id_posts = get_posts($args);
But I want to display all products when a category is not selected.How do I value 'terms' to achieve my goal?
you can define a function that checks all your categories in order and then gives you the ID of the categories.
Now you can put these ideas in terms.
function get_all_ID_category()
{
$args = array(
'taxonomy' => "product_cat",
'number' => 0,
'orderby' => 'name',
'order' => 'ASC',
);
$product_categories = get_terms($args);
$product_cat = [];
foreach ($product_categories as $pc) {
$int_pc = $pc->term_id;
$product_cat[] = $int_pc;
}
return $product_cat;
}
function get_all_posts_category()
{
$args = array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'fields' => 'ids',
'terms' => get_all_ID_category(),
'operator' => 'IN'
)
)
);
return get_posts($args);
}
I'm trying to get all product sku's in a current category. I have this code so far which is getting me only the IDs. I have also tried to use meta_query but I get some errors. I have did some research over the portal and other sites but I did not get it at all. What do I need to do is to display all curent Woocommerce product category products parent sku's in all_ids array.
Code so far
$all_ids = get_posts( array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'tenisky',
'operator' => 'IN',
)
),
) );
Output which gets me the data into my HTML data layers.
<?php echo "'" . implode ( "', '", $all_ids ) . "'"; ?>
Thank you in advance!
I think you should be able to solve this using get_post_meta:
$all_ids = get_posts( array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'tenisky',
'operator' => 'IN',
)
),
) );
$idsku = [];
for ($index = 0; $index < count($all_ids); $index++) {
$idsku[$index] = get_post_meta($all_ids[$index], '_sku', true);
}
Can you help me with this? I have a product list and what I want is to hide those products marked as hidden on the visibility detail of the product. Here's the code I have:
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
// 'terms' => 'white-wines'
'terms' => $product_categories[$wc_arr_key[$wc_cat_id]]->term_id,
'visibility' => 'visible' //NOT WORKING...
)
),
'post_type' => 'product',
'orderby' => 'menu_order',
'order' => 'ASC',
);
$products = new WP_Query( $args );
if(isset($_GET['staging']) && $_GET['staging'] == "true") {
echo "<pre>" . print_r($products) . "</pre>";
}
I want to display all the products that are marked as visible.
Since Woocommerce 3 the product visibility is handled by the taxonomy product_visibility for the term exclude-from-catalog, so you need to use a second array in your tax query array:
$terms = array( $product_categories[$wc_arr_key[$wc_cat_id]]->term_id );
$products = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $terms
),
array(
'taxonomy' => 'product_visibility',
'terms' => array('exclude-from-catalog'),
'field' => 'name',
'operator' => 'NOT IN',
),
),
'orderby' => 'menu_order',
'order' => 'ASC',
) );
if(isset($_GET['staging']) && $_GET['staging'] == "true") {
echo "<pre>" . print_r($products) . "</pre>";
}
Tested and works.
Related: Database changes for products in woocommerce 3
Use below code to exclude hidden products and display only visible ones
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
// 'terms' => 'white-wines'
'terms' => $product_categories[$wc_arr_key[$wc_cat_id]]->term_id
)
),
'meta_query' => array(
array(
'key' => '_visibility',
'value' => 'hidden',
'compare' => '!='
)
),
'post_type' => 'product',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$products = new WP_Query( $args );
if(isset($_GET['staging']) && $_GET['staging'] == "true") {
echo "<pre>" . print_r($products) . "</pre>";
}
WooCommerce save this data as metadata so you'll need to run a Meta Query against the name _visibility. This will look like
'meta_query' => array(
array(
'key' => '_visibility',
'value' => 'hidden',
'compare' => '!=',
)
)
This will pull all posts that do not have meta _visibility equal to hidden
I am trying to create a short-code to get products from specific category in woocommerce. I am using 'tax_query' to target specific category and 'shortcode_atts' to get parameter from shortcode itself. The code is as follow:
function quick_launch_products($atts) {
extract(shortcode_atts(array(
'product_category_ID' => '',
), $atts));
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $product_category_ID,
'operator' => 'IN'
)
)
);
echo $product_category_ID;
$products = null;
$products = new WP_Query($args);
}
add_shortcode("quick_launch_product_slider", "quick_launch_products");
The shortcode:
[quick_launch_product_slider product_category_ID="383"]
The return value is blank. I saw a lot of demo codes and followed exactly as they were, but its not working at all.
What am i missing here?
Thanks in advance.
I want to display all products from multiple categories at once.
When i want to display all products from one category my $args array looks like this:
$args = array(
'post_type' => 'product',
'product_cat' => 'backpacks',
'orderby' => '_sku'
);
I remember that I can simply make an array inside my $args:
$args = array(
'post_type' => 'product',
'product_cat' => array(
'backpacks','accessoires',
),
'orderby' => '_sku'
);
But it gives me the following error:
Warning: urlencode() expects parameter 1 to be string, array given in C:\xampp\htdocs\live\wp-includes\formatting.php on line 4312
I know this is a simple thing but i cant figure out why its not working.
Thanks for any help!
Please try below snippet.
$sortcolumn = 'ID';
$prod_categories = array(12, 17); //category IDs
$product_args = array(
'numberposts' => -1,
'post_status' => array('publish', 'pending', 'private', 'draft'),
'post_type' => array('product', 'product_variation'), //skip types
'orderby' => $sortcolumn,
'order' => 'ASC',
);
if (!empty($prod_categories)) {
$product_args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $prod_categories,
'operator' => 'IN',
));
}
$products = get_posts($product_args);
Found a simple way to do it
$args = array(
'post_type' => 'product',
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'backpacks'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'accessoires'
)
),
);