I have a Woocommerce store with more than 1000 products. I would like that all products which have a price below 500 should be shown on a separate page, so I can tag that Page in my menu.
Is there a shortcode that i can use to display {atleast first 4 products} the same on a block on the home page?
this should do the trick
add_shortcode( 'products_below_500', 'products_below_500_shortcode' );
function products_below_500_shortcode() {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_price',
'value' => 500,
'compare' => '<'
)
),
'fields' => 'ids',
);
$product_ids = get_posts( $args );
$product_ids = implode( ",", $product_ids );
return do_shortcode("[products ids='$product_ids' columns= 4 ]");
}
Usage : [products_below_500] // on page you want to use
Related
I want to change number of displayed products in loop on archive pages. I tried two snippets with no results:
// Change the Number of WooCommerce Products Displayed Per Page
add_filter( 'loop_shop_per_page', 'lw_loop_shop_per_page', 30 );
function lw_loop_shop_per_page( $products ) {
$products = 12;
return $products;
}
If the above snippet does not work for you, you can use the following
code snippet.
// Change the Number of WooCommerce Products Displayed Per Page
add_filter( 'loop_shop_per_page', create_function( '$products', 'return 12;' ), 30 );
My goal is to display 50 products per page.
You can do it with your own loop like
$products = new WP_Query([
'post_status' => 'publish',
'post_type' => 'product',
]);
if($products->have_posts()){
while ( $products->have_posts() ) {
$products->the_post();
wc_get_template_part( 'content', 'product' );
}
}
it will show all products which are published also you can filter them by tax_query, and do pagination
$products = new WP_Query([
'post_status' => 'publish',
'post_type' => 'product',
'post_per_page' => 0,
'paged' => get_query_var( 'paged' ),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'pa_size',
'field' => 'slug',
'terms' => 'small',
);
),
]);
you can find everything about WP_Query here: https://developer.wordpress.org/reference/classes/wp_query/
Well, it may sound easy but I searched a lot but could not find the appropriate answer. I want to check if a category contains any sale products.
For example, there are 10 categories. I want to get categories that contain products with sale and exclude those categories that don't contain any sale items.
Well, I am new to WP.
I searched a lot about the solution and finally I managed to write my own logic for answering this.
Steps:
Get all categories
Get all ids of sale products
loop through categories of step 1
get products id of this category
loop through ids of ids of step 4
check if that id is present in list of ids of sale product
if found, set flag to true and exit
if flag is false (no product on sale), unset the category
Here is my sample code:
<?php
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
'parent' => 0,
);
$categories = get_terms( 'product_cat', $args );
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query_args = array(
'post_status' => 'publish',
'post_type' => 'product',
'posts_per_page' => -1,
'paged' => $paged,
'orderby' => 'ASC',
'meta_query' => WC()->query->get_meta_query(),
'post__in' => array_merge( array( 0 ), wc_get_product_ids_on_sale() )
);
$sale_products = new WP_Query( $query_args );
$products_ids_on_sale = wc_get_product_ids_on_sale();
if($sale_products->have_posts()) {
foreach ($categories as $key => $category) {
//get product ids
$category_product_ids = get_posts( array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $category->term_id,
'operator' => 'IN',
)
),
) );
$is_product_exist = false;
if ( ! empty($category_product_ids) ) {
foreach ($category_product_ids as $product_id) {
if (in_array($product_id, $products_ids_on_sale)) {
$is_product_exist = true;
break;
}
}
if ( $is_product_exist === false ) {
unset($categories[$key]);
}
}
}
wp_reset_query();
}
I have written a article on my blog and explain in more detail about the produces and code at https://www.kodementor.com/get-only-categories-that-contain-products-on-sale/
This code display only on sale product in your store.
Please add below code in your active theme functions.php file
add_action( 'woocommerce_product_query', 'only_sale_product_in_store' );
function only_sale_product_in_store( $q ){
$product_ids_on_sale = wc_get_product_ids_on_sale();
$q->set( 'post__in', $product_ids_on_sale );
}
May is useful for you. Thanks
I want to only get 5 posts within current category by random using the following code.
// Get all posts within current category, but exclude current post
$category_posts = new WP_Query( array(
'cat' => $categories[0]->term_id,
'post__not_in' => array( get_the_ID() ),
) );
How do you apply the '5 posts' limit and 'order by random' to the above code?
as for me, I would use get_posts(), but these arguments should work in your case as well:
<?php $args = array(
'numberposts' => 5,
'category' => $categories[0]->term_id,
'orderby' => 'rand',
'exclude' => array( get_the_ID() ),
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
More about this here: https://codex.wordpress.org/Template_Tags/get_posts
I used the following code.
// Get all posts within current category, but exclude current post
$category_posts = new WP_Query( array(
'orderby' => 'rand',
'cat' => $categories[0]->term_id,
'post__not_in' => array( get_the_ID() ),
'posts_per_page' => 3,
) );
I'm trying to filter the products shown in "related products" in woocommerce so that it only outputs related products that are in stock.
I've tried editing the "related.php" template as below - but it ain't working!
$args = apply_filters( 'woocommerce_related_products_args', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'post__in' => $related,
'post__not_in' => array( $product->id ),
'meta-key' => '_stock_status',
'meta-value' => 'outofstock',
'compare' => '!='
) );
I'd appreciate any help
Many thanks
None of the answers given here worked for me (I believe the woocommerce_output_related_products_args filter mentioned does not accept meta_queries), and I wanted a solution that didn't use an SQL query, so I put together the solution below:
add_filter( 'woocommerce_related_products', 'mysite_filter_related_products', 10, 1 );
function mysite_filter_related_products( $related_product_ids ) {
foreach( $related_product_ids as $key => $value ) {
$relatedProduct = wc_get_product( $value );
if( ! $relatedProduct->is_in_stock() ) {
unset( $related_product_ids["$key"] );
}
}
return $related_product_ids;
}
Hope that helps someone looking for a similar solution.
I think You have done this task, but if anyone having this issue then they can use this code to show only "in stock" product on related products, change into related.php:
$args = apply_filters( 'woocommerce_related_products_args', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'post__in' => $related,
'post__not_in' => array( $product->id ),
'meta_key' => '_stock_status',
'meta_value' => 'instock',
'compare' => '!='
)
);
This one's driving me nuts.. I'm trying to query and output WooCommerce products based on a specific attribute. For example, I set up an Attribute called on, with possible values of yes or no.
I query using the following:
$args = array(
'post_type' => 'product',
'meta_key' => 'pa_on',
'meta_value' => 'yes',
'posts_per_page' => -1
);
query_posts($args);
The meta_key is crucial perhaps; if I call it on I get nothing. If I call it pa_on (because that's how I understand WooCommerce custom attributes to be constructed) I get nothing.
However, if I try a different query and use _featured, which is a standard WooCommerce custom meta thingy, it returns the relevant featured posts. Help, anyone?
I know this is an old one, but just in case someone stumbles upon it like I did today -- Woocommerce (I'm using v2.6.2) appears to store these custom attributes as taxonomies.
I suspect the correct args for the original question would look like this:
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'pa_on',
'field' => 'name',
'terms' => 'yes'
)
)
);
Using the appropriate values for my installation, it solved my problem.
For new woocommerce use:
$attribute = 'on';
$value = 'yes';
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'pa_' . $attribute,
'terms' => $value,
'field' => 'slug',
'operator' => 'IN'
)
)
);
If product attribute is saved as specific product attribute (i.e. not global), then you can't query it as taxonomy, instead you can use this snippet (copied from http://snippet.fm/snippets/query-woocommerce-products-product-specific-custom-attribute/):
// Set custom attribute name and value to search for
$attribute_name = 'color';
$attribute_value = 'green';
$serialized_value = serialize( 'name' ) . serialize( $attribute_name ) . serialize( 'value' ) . serialize( $attribute_value ); // extended version: $serialized_value = serialize( $attribute_name ) . 'a:6:{' . serialize( 'name' ) . serialize( $attribute_name ) . serialize( 'value' ) . serialize( $attribute_value ) . serialize( 'position' );
$args = array(
'post_type' => 'product',
'post_status' => 'any',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_product_attributes',
'value' => $serialized_value,
'compare' => 'LIKE',
),
),
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) {
$loop->the_post();
// do stuff here... e.g. get_the_ID()
}
wp_reset_postdata();
Guess what you need is a query with meta_query value set:
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'pa_on',
'value' => 'yes',
'compare' => '='
)
)
);
$query = new WP_Query( $args );
You can learn more about those here:
http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters