show low in stock products - wordpress

for my website, I need a page to display the low in stock products only, as same as choosing to display only top rated, sales, recent products.
I found a code to display the out of stock .. so I just replace "outofstock" value to "lowinstock", but that doesn't work, it shows all of the products
add_shortcode( 'out_of_stock_products', 'bbloomer_out_of_stock_products_shortcode' );
function bbloomer_out_of_stock_products_shortcode() {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'outofstock',
)
),
'fields' => 'ids',
);
$product_ids = get_posts( $args );
$product_ids = implode( ",", $product_ids );
return do_shortcode("[products ids='$product_ids']");
}
Do you have any solutions for this??
I appreciate your help

You could filter the product IDs and check if the amount in stock is lower than the low stock threshold but greater than zero (out of stock).
add_shortcode( 'out_of_stock_products', 'bbloomer_out_of_stock_products_shortcode' );
function bbloomer_out_of_stock_products_shortcode() {
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'fields' => 'ids',
);
$product_ids = get_posts( $args );
foreach ( $product_ids as $key => $product_id ) {
if ( $product = wc_get_product( $product_id ) ) {
$stock = $product->get_stock_quantity();
$low_stock_amount = $product->get_low_stock_amount();
if ( !empty( $stock ) && !empty( $low_stock_amount ) ) {
if ( $stock > $low_stock_amount || $stock < 1 ) {
unset( $product_ids[$key] );
}
} else { //Not enough stock data
unset( $product_ids[$key] );
}
}
}
$product_ids = implode( ",", $product_ids );
return do_shortcode("[products ids='$product_ids']");
}

Related

Related Products display first same subcategory

Trying to display related products in WooCommerce first by same subcategory and when there are no products to show in that, display another products from parent category, avoiding being empty.
For example:
Fashion > Clothes > T-Shirts
When there are no t-shirts available to show, display another products from parent category (clothes).
Tried it but it is not working:
function wc_related_products_by_last_available_depth_term( $related_posts, $product_id, $args ) {
$product = wc_get_product( $product_id );
$terms = wp_get_post_terms( $product_id, 'product_cat' );
$hierarchy = array();
$cat_id = '';
// find the depth of terms
foreach ( $terms as $key => $term ) {
$ancestors = get_ancestors( $term->term_id, 'product_cat' );
if( $ancestors && count( $ancestors ) > 1 ) {
$hierarchy[$term->term_id] = max($ancestors);
}elseif( $ancestors ) {
$hierarchy[$term->term_id] = $ancestors[0];
}
$cat_id = $term->term_id;
}
// if level of depth term available replace $cat_id
if( $hierarchy ){
$cat_id = max( array_keys( $hierarchy ) );
}
$related_posts = get_posts( array(
'post_type' => 'product',
'post_status' => 'publish',
'fields' => 'ids',
'posts_per_page' => -1,
'exclude' => array( $product_id ),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => array( $cat_id )
)
)
));
return $related_posts;
}
add_filter( 'woocommerce_related_products', 'wc_related_products_by_last_available_depth_term', 99, 3 );

Woocommerce get parent's description from variation product

I'm trying to get the descritpiton of the parent product from a series of variation.
I tried this way
$args = array(
'numberposts' => -1,
'post_type' => 'product_variation',
'suppress_filters' => false,
'orderby' => 'title',
'order' => 'DESC' );
$products = get_posts( $args );
foreach ( $products as $productId ) {
$product = wc_get_product( $productId );
$parent = $product->get_parent_data();
....
where product is my variation. But in parent data i haven't a description.
Any suggestions on how can I get the description?
You can use get_parent_id to get the parent product id. Try the below code.
$args = array(
'numberposts' => -1,
'post_type' => 'product_variation',
'suppress_filters' => false,
'orderby' => 'title',
'order' => 'DESC'
);
$products = get_posts( $args );
$productsIds = array(); // store product id for avoid duplication.
foreach ( $products as $variation_id ) {
$variation = wc_get_product($variation_id);
$product = wc_get_product( $variation->get_parent_id() );
if( !in_array( $product->get_id(), $productsIds ) && $product ){
$productsIds[] = $product->get_id();
$product_details = $product->get_data();
$product_full_description = $product_details['description'];
$product_short_description = $product_details['short_description'];
}
}

Woocommerce : only show products between start and end dates

I'm trying to display only products between two dates. When I only put 1 array and one date it works, but when I put other arrays and my second date it doesn't work anymore, it shows all the products. Any idea ?
function custom_meta_query( $meta_query ){
$today = current_time('Ymd');
$args = array (
'meta_query' => array(
'relation' => 'AND',
array(
'key'=>'flash_sale_start',
'value' => $today,
'compare'=>'<=',
'type' => 'DATE'
),
array(
'key'=>'flash_sale_end',
'value' => $today,
'compare'=>'>=',
'type' => 'DATE'
)),);
$date_query = new WP_Query( $args );
//return $meta_query;
}
// The main shop and archives meta query
add_filter( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 10, 2 );
function custom_product_query_meta_query( $meta_query, $query ) {
if( ! is_admin() )
return custom_meta_query( $meta_query );
}
// The shortcode products query
add_filter( 'woocommerce_shortcode_products_query', 'custom__shortcode_products_query', 10, 3 );
function custom__shortcode_products_query( $query_args, $atts, $loop_name ) {
if( ! is_admin() )
$query_args['meta_query'] = custom_meta_query( $query_args['meta_query'] );
return $query_args;
}
// The widget products query
add_filter( 'woocommerce_products_widget_query_args', 'custom_products_widget_query_arg', 10, 1 );
function custom_products_widget_query_arg( $query_args ) {
if( ! is_admin() )
$query_args['meta_query'] = custom_meta_query( $query_args['meta_query'] );
return $query_args;
}
Thank you
I've found the solution, it was an array() problem
function custom_meta_query( $meta_query ){
$today = current_time('Ymd');
$args = array (
'numberposts' => -1,
'meta_query' => array(
'relation' => 'AND',
'start_clause' => array(
'key'=>'flash_sale_start',
'value' => $today,
'compare'=> '<=',
'type' => 'DATE'
),
'end_clause' => array(
'key' => 'flash_sale_end',
'value' => $today,
'compare' => '>=',
'type' => 'DATE'
),
));
return $args;
}
// The main shop and archives meta query
add_filter( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 10, 2 );
function custom_product_query_meta_query( $meta_query, $query ) {
if( ! is_admin() )
return custom_meta_query( $meta_query );
}
// The shortcode products query
add_filter( 'woocommerce_shortcode_products_query', 'custom__shortcode_products_query', 10, 3 );
function custom__shortcode_products_query( $query_args, $atts, $loop_name ) {
if( ! is_admin() )
$query_args['meta_query'] = custom_meta_query( $query_args['meta_query'] );
return $query_args;
}
// The widget products query
add_filter( 'woocommerce_products_widget_query_args', 'custom_products_widget_query_arg', 10, 1 );
function custom_products_widget_query_arg( $query_args ) {
if( ! is_admin() )
$query_args['meta_query'] = custom_meta_query( $query_args['meta_query'] );
return $query_args;
}

get only featured image

I search internet, but I found only the reverse scenerio, the thing I want to do is that get only featured image in this query
$attachments = new WP_Query( array(
'post_parent__in' => $published,
'post_type' => 'attachment',
'post_status' => 'inherit',
'fields' => 'ids',
'posts_per_page' => 1,
'orderby' => 'rand',
'update_post_term_cache' => false,
'update_post_meta_cache' => false,
'no_found_rows' => true
) );
if ( $attachments->have_posts() ) {
$image = wp_get_attachment_image_src( $attachments->posts[0], $args[ 'size' ] );
if ( file_exists( $image[0] ) ) {
set_transient( $objects_key, $image, 3 * HOUR_IN_SECONDS );
}
}
}
You really don't want to mess with wp_query() if you can help it try this...
global $post;
$post_thumbnail_id = get_post_thumbnail_id( $post->ID );
if($post_thumbnail_id != null) {
$image = wp_get_attachment_image_src( $post_thumbnail_id, $args[ 'size' ] );
if ( file_exists( $image[0] ) ) {
set_transient( $objects_key, $image, 3 * HOUR_IN_SECONDS );
}
}
I am asuming that $objects_key and HOUR_IN_SECONDS are defined elsewhere

Querying posts from same categories / tags

I want to find all posts from same categories and same tags like a specific post in a plugin. So I do it actually separately by querying for tags and categories:
$taxonomy_arr = wp_get_post_tags( $this->post->ID, array( "fields" => "ids" ) );
add_filter( 'posts_where', array( $this, 'additional_filter' ) );
foreach ( $taxonomy_arr as $tag_id ) {
$posts_arr = get_posts( array(
'posts_per_page' => $this->max_results,
'tag_id' => (int) $tag_id,
'post_type' => array( $this->included_post_types ),
'orderby' => 'rand',
'suppress_filters' => false
) );
// add to categories selection
if ( is_array( $posts_arr ) ) {
foreach ( $posts_arr as $post_obj ) {
if ( is_object( $post_obj ) ) {
$local_taxonomy_selection[] = (int) $post_obj->ID;
}
}
}
}
// get post categories
$category_array = get_the_category( $this->post->ID );
foreach ( $category_array as $category ) {
$posts_arr = get_posts( array(
'posts_per_page' => $this->max_results*2,
'category' => $category->cat_ID,
'post_type' => array( $this->included_post_types ),
'orderby' => 'rand',
'suppress_filters' => false
));
// add to categories selection
if ( is_array( $posts_arr ) ) {
foreach ( $posts_arr as $post_obj ) {
if ( is_object( $post_obj ) ) {
$local_category_selection[] = (int) $post_obj->ID;
}
}
}
}
// combine post id's arrays
$all_posts = $local_taxonomy_selection + $local_category_selection;
It's working but is there a way to do it in one query?
This question already has answer on wordpress.stackexchange. So copying same code here.
https://wordpress.stackexchange.com/questions/4201/how-to-query-posts-by-category-and-tag
global $wp_query;
$args = array(
'category__and' => 'category',
'tag__in' => 'post_tag', //must use tag id for this field
'posts_per_page' => -1
$posts = get_posts($args);
foreach ($posts as $post) :
//do stuff
endforeach;

Resources