wordpress query , that shows featured products - php

I Want show featured products but my query return nothing!
I featured one of my products but not shown any thing.
<?php
$terms = array(
"post_type" => "product",
"orderby" => "date",
"order" => "DESC",
"posts_per_page" => 12,
"meta_query" => array(array('key' => '_featured','value' => 'yes'))
);
$query = new WP_Query( $terms );
while($query->have_posts()){
$query->the_post();
global $product;
?>
Thanks Also

use short code [featured_products per_page="12" columns="4"]
or custom logic as :
$args = array(
'post_type' => 'product',
'meta_key' => '_featured',
'meta_value' => 'yes',
'posts_per_page' => 12
);
$featured_query = new WP_Query( $args );
if ($featured_query->have_posts()) :
while ($featured_query->have_posts()) :
$featured_query->the_post();
$product = get_product( $featured_query->post->ID );
// Output product information here
echo "<pre>";print_r($product);echo "</pre>";
endwhile;
endif;
wp_reset_query(); // Remember to reset
Ref here : http://biostall.com/how-to-display-woocommerce-featureds-product-without-a-shortcode/

Related

Loop through the meta value of WooCommerce products using a WP Query

I am working in WooCommerce API and I would like to make a new endpoint which lists me specific products. We have made previously new fields with a plugin and I would like to check them. If the property is ticked in the settings, then show it, else do not.
The new property of the products (checkbox in advanced settings):
woocommerce_wp_checkbox(
array(
'id' => '_checkbox_badge_hot',
'desc' => __('set the checkbox', 'woocommerce'),
'label' => __('Hot', 'woocommerce'),
'desc_tip' => 'true',
'value' => get_post_meta( $post->ID, '_checkbox_badge_hot', true )
)
);
The Code which should list me the products (now if I try to request to the endpoint it just keeps loading):
function get_hot_products(){
$args_for_hot_product = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$loop = new WP_Query( $args_for_hot_product );
$hotproducts = [];
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
while ( $loop->have_posts() ): $loop->the_post(){
$hot = $loop->get_meta('_checkbox_badge_hot');
if( $hot === "yes" ){
array_push($hotproducts, $hot);
}
}
//wp_reset_postdata();
return $hotproducts;
}
add_action( 'rest_api_init', function () {
register_rest_route( '/wc/v3', '/featuredproducts', array(
'methods' => 'GET',
'callback' => 'get_hot_products',
) );
} );
Thanks for your help guys!
To get and display all products that have a specific custom field value, using a WP_Query, you will simply use a eta query as follow:
function display_hot_products(){
$loop = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array( array(
'key' => '_checkbox_badge_hot',
'value' => 'yes',
'compare' => '=',
)),
));
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
the_title(); // Display product title
endwhile;
wp_reset_postdata();
endif;
}
Don't forget that when using a WP_Query you get WC_Post objects and not WC_Product Objects.

How to check if a product is onsale - WooCommerce

I'm trying to get onsale products in a custom query, anybody can help to form the query or the condition to make it happen...
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
echo '<ul class="owl-carousel">';
$products_onsale = array();
while ( $query->have_posts() ) : $query->the_post();
// get_template_part('template-parts/product-slider');
$product_id = get_the_ID();
$product = new WC_Product( $product_id );
if ( $product->is_on_sale() ) {
echo 'on sale';
}
endwhile;
echo '</ul>';
print_r( $products_onsale );
endif;
here is the working i'm working on
global $product;
if ( $product->is_on_sale() ) {
do_something();
}
I have two types of code for done this thing
<!-- Get WooCommerce On-Sale Products fetch -->
<ul class="get-onsale-product">
<?php
$args_for_onsale_product = array(
'post_type' => 'product',
'posts_per_page' => 4, //If you want all the post replace 4 with -1.
'meta_query' => array(
'relation' => 'OR',
array( // Simple products type
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array( // Variable products type
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
)
);
$onsale_product_items = new WP_Query( $args_for_onsale_product );
if ( $onsale_product_items->have_posts() ) {
while ( $onsale_product_items->have_posts() ) : $onsale_product_items->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'Sorry We have no products.' );
}
wp_reset_postdata();
?>
</ul>
<!-- End WooCommerce On-Sale Products fetch -->
And second is following, Before you getting this code please review this link
<!-- Get WooCommerce On-Sale Products fetch -->
<ul class="get-onsale-product">
<?php
$args_for_onsale_product = array(
'post_type' => 'product',
'posts_per_page' => 4,
'post__in' => wc_get_product_ids_on_sale(),
);
$onsale_product_items = new WP_Query( $args_for_onsale_product );
if ( $onsale_product_items->have_posts() ) {
while ( $onsale_product_items->have_posts() ) : $onsale_product_items->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'Sorry We have no products.' );
}
wp_reset_postdata();
?>
</ul>
<!-- End WooCommerce On-Sale Products fetch -->
You can use following to check if product has sale price:
$sale_price = get_post_meta( $product_id, '_sale_price', true);
If the $sale_price is greater than 0 and not empty, the product is on sale.
Hope this helps!
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>'
)
),
);
$query = new WP_Query($args);
if ($query->have_posts()) :
echo '<ul class="owl-carousel">';
$products_onsale = array();
while ($query->have_posts()) : $query->the_post();
echo 'on sale';
endwhile;
echo '</ul>';
print_r($products_onsale);
endif;
It will fetch only products with sale price greater than zero ( onsale )

Remove Woocommerce free products from a WP_Query loop

I am using the php below in an enhanced text widget to display a list of recently added products. It works fine.
However, my shop also has free products (Price 0), and I do not want these displayed in the list.
What to I need to add to the code to exclude free products?
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 8,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br />'.get_the_title().'';
endwhile;
wp_reset_query();
?>
You could try with wp_query only
$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => 8,
'meta_query' => [[
'key' => '_price',
'value' => 0,
'compare' => '>',
'type' => 'NUMERIC'
]]
) );
Check here for more examples.

Exclude the current product from WooCommerce custom Related Products output

The aim is to display 4 products on the product page however remove the current product from the filter. At present I am pulling through products with related brands and categories, however this is also pulling through the current product to the related products...
Current related.php file for Woocommerce contains the following:
if ( ! $related = $product->get_related( $posts_per_page ) ) {
return;
}
$brands_array = array(0);
$cats_array = array(0);
$cur_product_id = $product->id;
// get categories
$terms = wp_get_post_terms( $product->id, 'product_brand' );
$category_terms = wp_get_post_terms( $product->id, 'product_cat' );
// select only the category which doesn't have any children
foreach ( $terms as $term ) {
$brands_array[] = $term->term_id;
}
foreach ( $category_terms as $category_term ) {
$cats_array[] = $category_term->term_id;
}
$final_array = array_merge($brands_array, $cats_array);
$filtered_array = array_filter($final_array, "test_odd");
function test_odd($var)
{
return($var & 1);
}
var_dump($final_array);
$args = apply_filters( 'woocommerce_related_products_args', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => 4,
'columns' => 4,
'orderby' => $orderby,
'tax_query' => array(
array(
'taxonomy' => 'product_brand',
'field' => 'id',
'terms' => $final_array
),
)
));
$products = new WP_Query( $args );
$woocommerce_loop['name'] = 'related';
$woocommerce_loop['columns'] = apply_filters( 'woocommerce_related_products_columns', $columns );
if ( $products->have_posts() ) : ?>
<div class="related products">
<h2><?php _e( 'Related Products', 'woocommerce' ); ?></h2>
<?php echo $filtered_array ?>
<?php woocommerce_product_loop_start();
while ( $products->have_posts() ) : $products->the_post();
wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
</div>
<?php endif;
wp_reset_postdata();
How do I go about filtering the current product from the array of products that are shown on the product page?
Thanks
To exclude current product, the missing argument in your WP_Query is 'post__not_in' (array). So your $args array is going to be:
$args = apply_filters( 'woocommerce_product_related_posts_query', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'post__not_in' => array( $product->get_id() ), // <==== HERE
'no_found_rows' => 1,
'posts_per_page' => 4,
'columns' => 4,
'orderby' => $orderby,
'tax_query' => array(
array(
'taxonomy' => 'product_brand',
'field' => 'id',
'terms' => $final_array
),
)
));
$products = new WP_Query( $args );
// . . .
See: woocommerce_product_related_posts_query replace woocommerce_related_products_args since WooCommerce 3+ (thanks to #strarsis).

How do I add an echo inside an array?

I'm trying to show products from a certain category on a page like his:
$args = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => 'prcategory1', 'orderby' => 'price');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
This one works. However, I'd like 'prcategory1' to be taken from a custom field of the page. Something like this (incorrect code incoming):
$args = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => 'get_post_meta(get_the_ID(), 'custom_cat_name', TRUE); ?>', 'orderby' => 'price');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
There is some errors in your code, try this for your array :
$args = array(
'post_type' => 'product',
'posts_per_page' => 5,
'product_cat' => get_post_meta(
get_the_ID(),
'custom_cat_name',
TRUE
),
'orderby' => 'price'
)

Categories