Woocommerce order products by attribute name asc - php

I have a custom page template with products in Woocommerce, but I want order the products by attribute's terms name asc. I have three terms. I don't know the right way to do it, I have tried this:
<?php
args = array(
'post_type' => 'product',
'posts_per_page' => 20,
'meta_key' => 'pa_tire-type',
'orderby' => 'meta_value_num',
'order' => 'asc'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product; ?>

— Updated —
You need to add a tax_query for your product attribute with the related terms (slugs) in it this way:
// Set HERE the attibute taxonomy slug
$taxonomy = 'pa_tire-type';
// Set HERE the term slugs for this attribute
$terms_array = array('term_slug1', 'term_slug2', 'term_slug3');
// The loop query
$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => 20,
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $terms_array,
'operator' => 'IN',
) ),
'order_by' => 'terms',
'order' => 'asc'
) );
$product_ids = array();
if ( $loop->have_posts() ): while ( $loop->have_posts() ) : $loop->the_post();
$product_ids[] = $loop->post->ID;
endwhile;
wp_reset_postdata();
endif;
// Testing output
print_pr($product_ids);
This code is tested and works.

Related

WooCommerce products custom loop filtered by an array of SKUs

I have an array which has a couple of product SKU's in it I want to dispay the products that are connected to the SKU's.
I found the custom woocommerce product loop, but can't find any documentation on how to extend this.
$skus = array(sku1, sku2, sku3);
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
}
wp_reset_postdata();
In a WP_Query, you will need to use a meta query to get products that are connected to an array of SKUs.
The code:
$skus = array('sku1', 'sku2', 'sku3');
$loop = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array( array(
'key' => '_sku',
'value' => $skus,
'compare' => 'IN',
) ),
) );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
endif;
wp_reset_postdata();
Tested and works.
See WP_Query and custom field post meta parameters documentation
You could try to do this
$skus = array(sku1, sku2, sku3);
$meta_query = array['relation' => 'OR',];
foreach ($skus as $sku) {
$meta_query[] = array(
'key' => '_sku',
'value' => $sku,
'compare' => '='
),
}
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => $meta_query
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
}
wp_reset_postdata();

Filter WooCommerce products by taxonomy terms and custom fields in a WP_Query

In WooCommerce, I have a product category boots-2 (term slug) with 350 products in it.
I need to return all products that are in this category that have the meta key _translate_lock with a meta value 1 as defined in the WP_Query below:
Here is my code
$args = array(
'posts_per_page' => '400',
'category' => 'boots-2',
'post_type' => 'product',
'orderby' => 'name',
'meta_query' => array(
array(
'key' => '_translate_lock',
'value' => '1',
'compare' => '='
)
),
);
$query = new WP_Query($args);
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$productId = get_the_ID() ;
println("retrieved product id is $productId");
}
}
wp_reset_query();
This code returns products that don't belong the the category with the category slug boots-2, i've tried changing the query string to cat=881 where 881 is the tag_id of the category but the code still returns products from other categories.
What am I missing ?
First Woocommerce product category is a custom taxonomy that has nothing to do with WordPress category. Now for a WP_Query and taxonomy parameters is better to use a tax query like:
$query = new WP_Query( array(
'posts_per_page' => -1,
'post_type' => 'product',
'post_status' => 'publish',
'orderby' => 'name',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => '_translate_lock',
'value' => '1',
// 'compare' => '=' // not needed as default value is '='
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat', // Woocommerce product category taxonomy
'field' => 'slug', // can be: 'name', 'slug' or 'term_id'
'terms' => array('boots-2'),
)
),
) );
$results = []; // Initializing
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
$product_id = get_the_ID();
$product_name = get_the_title();
$results[] = $product_name .' ('.$product_id.')';
endwhile;
wp_reset_postdata();
// Displaying results
printf( '<p><strong>Retrieved products are</strong>:<br> %s<p>', implode(',<br>', $results) );
else :
echo '<p>No results found.</p>';
endif;
wp_reset_query();
Tested and works.
See WP_Query - taxonomy-parameters section documentation

How to show related posts by category from different post type

In my website, I have two different post-types. One of them is publication with custom category-type as publication-category and the other is service with custom category-type as service-category. I am publishing some brochures in Publication page those are under different services. What I am trying to do is displaying these brochures in Services page by same category type. It is like, if the brochure is published by Education services, then this brochure should be displayed in Education service page.
I am currently using ACF plugin to do so, but whenever there is new brochure, I have to go to service page and add it there. Today I tried below code but it displays all brochures from different category-types not the same category-type.
Perhaps you guys can help me how I can arrange the code a way that works for my above request.
<?php
$custom_taxterms = wp_get_object_terms(
$post->ID,
'publication-category',
array( 'fields' => 'ids' )
);
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'rand',
'order' => 'ASC',
'tax_query' => array( array(
'taxonomy' => 'publication-category',
'field' => 'id',
'terms' => $custom_taxterms
)),
'post__not_in' => array( $post->ID ),
);
$related_items = new WP_Query( $args );
if ( $related_items->have_posts() ) :
echo '<ul>';
while ( $related_items->have_posts() ) : $related_items->the_post();
?>
<li><?php the_title(); ?></li>
<?php
endwhile;
echo '</ul>';
endif;
wp_reset_postdata();
?>
If you're on services page, so why do you use the 'publication-category' in
wp_get_object_terms(
$post->ID,
'publication-category',
array( 'fields' => 'ids' )
);
Seems like you have to use
$custom_taxterms = get_the_terms($post->ID, 'service-category');
$terms = [];
foreach ($custom_taxterms as $term) {
$terms[] = $term->slug
}
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'rand',
'order' => 'ASC',
'tax_query' => array( array(
'taxonomy' => 'publication-category',
'field' => 'slug',
'terms' => $terms
)),
);
And create same slugs for both terms in both categories. From my understanding. It's quite difficult to understand your architecture
I find the solution by changing service-category taxonomy to publication-category as same with other post-type and creating relationship with below code from : https://wordpress.stackexchange.com/questions/139571/display-posts-with-same-taxonomy-term?rq=1
Thanks everyone
<?php
//get the post's venues
$custom_terms = wp_get_post_terms($post->ID, 'publication-category');
if( $custom_terms ){
// going to hold our tax_query params
$tax_query = array();
// add the relation parameter (not sure if it causes trouble if only 1 term so what the heck)
if( count( $custom_terms > 1 ) )
$tax_query['relation'] = 'OR' ;
// loop through venus and build a tax query
foreach( $custom_terms as $custom_term ) {
$tax_query[] = array(
'taxonomy' => 'publication-category',
'field' => 'slug',
'terms' => $custom_term->slug,
);
}
// put all the WP_Query args together
$args = array( 'post_type' => 'publication',
'posts_per_page' => 20,
'tax_query' => $tax_query );
// finally run the query
$loop = new WP_Query($args);
if( $loop->have_posts() ) {
while( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="listing-title"><?php the_title(); ?></div>
<div class="listing-image">
</div>
<?php
endwhile;
}
wp_reset_query();
}?>

Get WooCommerce featured products in a WP_Query

I updated WooCommerce to version 3.0 but I can't show the featured products on my theme, I googled a while and get WC deleted the _feature and add this in taxonomy. But I don't understand so much how my theme get the featured products.
Here is the code of the wrong featured productcs.
$meta_query = WC()->query->get_meta_query();
$meta_query[] = array(
'key' => '_featured',
'value' => 'yes'
);
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $products,
'orderby' => $orderby,
'order' => $order == 'asc' ? 'asc' : 'desc',
'meta_query' => $meta_query
);
And if you know where is the featured item in the DataBase. Thanks so much.
Since Woocommerce 3, you need to use a Tax Query instead as featured products are now handled by product_visibility custom taxonomy for the term featured:
// The tax query
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN', // or 'NOT IN' to exclude feature products
);
// The query
$query = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $products,
'orderby' => $orderby,
'order' => $order == 'asc' ? 'asc' : 'desc',
'tax_query' => $tax_query // <===
) );
References:
Official documentation WP_Query Taxonomy Parameters
Source code Woocommerce WC_Shortcodes featured_products() function
You could use wc_get_featured_product_ids() function to get the featured product IDs array but using a tax query in a WP_Query is just fine and the right way…
Related:
Woocommerce meta_query not working for featured products
Show only featured products in Woocommerce shop page
Get featured products in Woocommerce 3
It should works.
This is an old question, but you can use wc_get_featured_product_ids() too:
$args = array(
'post_type' => 'product',
'posts_per_page' => $products,
'orderby' => $orderby,
'order' => $order == 'asc' ? 'asc' : 'desc',
'post__in' => wc_get_featured_product_ids(),
);
$query = new WP_Query( $args );
Just discovered it here. I hope it helps!
You can now use wc_get_products with parameter featured set to true. See https://github.com/woocommerce/woocommerce/wiki/wc_get_products-and-WC_Product_Query
$args = array(
'featured' => true,
);
$products = wc_get_products( $args );
For people looking for getting featured products by category then you can check my notes about this => https://jameshwartlopez.com/plugin/get-featured-products-of-a-category/
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
),
),
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();

Ordering products by price in custom wp_query loop

I currently have a very simple wp_query loop to loop through my WooCommerce products like so:
$args = array(
'posts_per_page' => -1,
'product_cat' => $cat,
'post_type' => 'product',
'orderby' => 'price',
'order' => 'DESC'
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) {
$the_query->the_post();
wc_get_template_part( 'content', 'product' );
}
This works as I want it to, except I can't get it to order the products by product price (ascending or descending) - what do I need to do to make this work?
Try this:
$args = array(
'posts_per_page' => -1,
'product_cat' => $cat,
'post_type' => 'product',
'orderby' => 'meta_value_num',
'meta_key' => '_price',
'order' => 'asc'
);
Hope it will help you.

Categories