WooCommerce Custom Loop to get all product from one specific category - filter

I try to find the code (short code) in the woo comm plugin that made the list of all the product from one category, to be able to modify it... no luck after 1 hours, still no find.
So i start coding it myself (reinventing the wheel) and here what i try to get
get me all the product from category ID="151" and be able to output the name, the permalink etc etc...
this is the code now, that return everything.... way too much ! and i don't know how to filter it
{
$args = array(
'post_type' => 'product',
'posts_per_page' => 99
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
//echo get_title()."<br/>";
var_dump($loop);
endwhile;
}

here is the code i have found, and modify to my needs
function get_me_list_of($atts, $content = null)
{
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => $atts[0], 'orderby' => 'rand' );
$loop = new WP_Query( $args );
echo '<h1 class="upp">Style '.$atts[0].'</h1>';
echo "<ul class='mylisting'>";
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<li>'.get_the_post_thumbnail($loop->post->ID, 'thumbnail').'</li>';
endwhile;
echo "</ul>";
wp_reset_query();
}
?>

The [product_category] shortcode defined in /woocommerce/includes/class-wc-shortcodes.php is a great starting place for this, particularly as Woocommerce is constantly evolving!
The guts of it is just a standard WP_Query with some extra code added on to do pagination, setting sort orders from the Woocommerce settings and some checking to see if the products are marked as visible or not.
So if you strip out the shortcode related code and wanted a function that just got visible products from a category with a specific slug, it would look like this:
function getCategoryProducts($category_slug) {
// Default Woocommerce ordering args
$ordering_args = WC()->query->get_catalog_ordering_args();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $ordering_args['orderby'],
'order' => $ordering_args['order'],
'posts_per_page' => '12',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array( esc_attr( $category_slug ) ),
'field' => 'slug',
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
)
)
);
if ( isset( $ordering_args['meta_key'] ) ) {
$args['meta_key'] = $ordering_args['meta_key'];
}
$products = new WP_Query($args);
woocommerce_reset_loop();
wp_reset_postdata();
return $products;
}
Pass in the slug and you'll get back a standard wordpress posts collection using the same ordering that you've configured in your Woocommerce settings.

I had similar problem as I wanted to get "Fabrics" product for a "Custom Page", here is the code I used.
<ul class="products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'product_cat' => 'fabrics'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
Using the above code means you get the default inline styles, classes and other necessary tags.

Related

Wordpress Lazy Loading using Pagination

im doing a lazy loading with WP_Query Pagination
it's working fine but the content duplicate itself when it reaches it's end
and when i search for a specific result it shows the result correctly
but after that it still want to do lazy load so it load random data
here is my code
lazy-load.php
<?php
add_action('wp_ajax_nopriv_load_posts_by_ajax', 'load_posts_by_ajax_callback');
add_action('wp_ajax_load_posts_by_ajax', 'load_posts_by_ajax_callback');
function load_posts_by_ajax_callback(){
// check_ajax_referer( 'load_more_posts', 'security' );
$paged = $_POST['page'];
$args = array(
'post_type' => 'unit',
'post_status' => 'publish',
'posts_per_page' => 4,
'paged' => $paged
);
if ( !empty($_POST['taxonomy']) && !empty($_POST['term_id']) ){
$args['tax_query'] = array (
array(
'taxonomy' => $_POST['taxonomy'],
'terms' => $_POST['term_id'],
),
);
}
if ( ! is_null($_POST['offer']) ) {
$args['meta_query'][] = array(
'key' => 'WAKEB_hot',
'value' => '1',
'compare' => '=',
);
}
if ( ! is_null($_POST['purpose']) ) {
$args['meta_query'][] = array(
'key' => 'WAKEB_vacation',
'value' => '1',
'compare' => '=',
);
}
if (!empty($_POST['project'])){
$args['meta_query'] = array (
array(
'key' => 'WAKEB_project',
'value' => $_POST['project'],
'compare' => '='
),
);
}
// start buffer
ob_start();
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while($query->have_posts()){ $query->the_post();
get_template_part("template-parts/units");
}
endif; wp_reset_postdata();
// start buffered data in data variable
$data = ob_get_clean();
wp_send_json_success( $data );
wp_die();
}
add_action('wp_ajax_nopriv_load_projects_by_ajax', 'load_projects_by_ajax_callback');
add_action('wp_ajax_load_projects_by_ajax', 'load_projects_by_ajax_callback');
function load_projects_by_ajax_callback(){
// check_ajax_referer( 'load_more_posts', 'security' );
$paged = $_POST['page'];
$args = array(
'post_type' => 'project',
'post_status' => 'publish',
'posts_per_page' => 4,
'paged' => $paged
);
if ( ! is_null($_POST['ptype']) ) {
$args['tax_query'] = array (
array(
'taxonomy' => 'pptypes',
'field' => 'slug',
'terms' => $_POST['ptype'],
),
);
}
if ( !empty($_POST['taxonomy']) && !empty($_POST['term_id']) ){
$args['tax_query'] = array (
array(
'taxonomy' => $_POST['taxonomy'],
'terms' => $_POST['term_id'],
),
);
}
// start buffer
ob_start();
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while($query->have_posts()){ $query->the_post();
get_template_part("template-parts/projects");
}
endif; wp_reset_postdata();
// start buffered data in data variable
$data = ob_get_clean();
wp_send_json_success( $data );
wp_die();
}
lazy-load.js
$('.unit-terms li a').each( function() {
if ( this.href == window.location.href ) {
$(this).parent().addClass('current');
}
});
main.js
(function($){
$('.isotope a').on('click', function(){
$('.isotope .active').removeClass('active');
$(this).addClass('active');
var filter = $(this).data('filter');
if(filter=='*'){
$('.property').show();
}else{
$('.property').not(filter).hide();
$('.property'+filter).show();
}
return false;
});
})(jQuery);
so how can i make it work? i don't know what im doing wrong here
Here is the repo link for the full project
https://github.com/Ov3rControl/hoomerz
ok, now I understand what you meant ;) During lazy load you send to backend only page number without current state of filters and / or search string. So it sends all posttype items based on page number only. You should send also current state of filters
main.js: add this to your after-page-load function:
var currentUrl = new URL(window.location.href);
var searchQuery = urlObj.searchParams.get("k");
lazy-load.js: add search param to data posted to backend
var data = {
'action': 'load_posts_by_ajax',
'page': page,
'search: searchQuery // new field
};
lazy-load.php: add search param to WP_Query
if ( isset($_POST['search']) && !empty($_POST['search']) ){ // new section
$args['s'] = sanitize_text_field($_POST['search']);
}
That's example for text search filter. For all filters you must
1. match every filter from front (URL get param) (main.js)
2. than put it in data object sent to backend (lazy-load.js)
3. address this variable in lazy-load.php in if(isset($_POST['param-name'])) section ( new or existing one as there are some )
I suggest to try without ob_start() / ob_get_clean(). Also if you generate html instead of raw data structure, I would simply print it to output without wp_send_json_success().
Other solution would be sending raw data (1. array in php, 2. json_encode(), 3. wp_send_json() ) and than processing in javascript (dynamic dom element creation after request to backend made).

Get sku and product image in woocommerce emails

I made a copy of the file "email-order-details.php" to my child theme. Now I can modify the html, but would like to add an extra column to my table for the product image. With this code I am able to display the image and sku, but it's in the same column with product name, sku, product description.
<?php
echo wc_get_email_order_items( $order, array( // WPCS: XSS ok.
'show_sku' => $true,
'show_image' => false,
'image_size' => array( 32, 32 ),
'plain_text' => $plain_text,
'sent_to_admin' => $sent_to_admin,
) );
?>
I also want to display the sku without the () and I want to place it on top of the column. I think there must be an array or variables or something like this to get the dates.
This line, for example displays the Price, can anybody tell me how to display sku and product image in the same way?
<th class="td" scope="col" style="text-align:<?php echo esc_attr( $text_align ); ?>;"><?php esc_html_e( 'Price', 'woocommerce' ); ?></th>
Thank you in advance
try this snippet code, I use this in functions.php in child theme:
function sww_add_wc_order_email_images( $table, $order ) {
ob_start();
$template = $plain_text ? 'emails/plain/email-order-items.php' : 'emails/email-order-items.php';
wc_get_template( $template, array(
'order' => $order,
'items' => $order->get_items(),
'show_download_links' => $show_download_links,
'show_sku' => $show_sku,
'show_purchase_note' => true,
'show_image' => true,
'image_size' => array( 200, 200 )
) );
return ob_get_clean();
}
add_filter( 'woocommerce_email_order_items_table', 'sww_add_wc_order_email_images', 10, 2 );

Woocommerce / sort related products by price

I added this filter to functions.php, but unfortunately it doesn't work. May anybody please help to find the reason
add_filter( 'woocommerce_grouped_children_args', 'custom_grouped_children_args', 18 );
function custom_grouped_children_args( $args ){
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'orderby' => 'meta_value_num',
'meta_key' => '_price',
'order' => 'desc'
);
}
just found out the solution, in case anybody needs it. I just had to remove the default filter and then to add mine
function custom_remove_hook(){
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
add_action( 'woocommerce_after_single_product_summary', 'custom_function_to_sort_related', 22 );
}
add_action( 'woocommerce_after_single_product_summary', 'custom_remove_hook' );
function custom_function_to_sort_related( $args = array() ) {
global $product;
if ( ! $product ) {
return;
}
$defaults = array(
'posts_per_page' => 4,
'columns' => 4,
'orderby' => 'price', // #codingStandardsIgnoreLine.
'order' => 'desc'
);
$args = wp_parse_args( $args, $defaults );
// Get visible related products then sort them at random.
$args['related_products'] = array_filter( array_map( 'wc_get_product', wc_get_related_products( $product->get_id(), $args['posts_per_page'], $product->get_upsell_ids() ) ), 'wc_products_array_filter_visible' );
// Handle orderby.
$args['related_products'] = wc_products_array_orderby( $args['related_products'], $args['orderby'], $args['order'] );
// Set global loop values.
wc_set_loop_prop( 'name', 'related' );
wc_set_loop_prop( 'columns', apply_filters( 'woocommerce_related_products_columns', $args['columns'] ) );
wc_get_template( 'single-product/related.php', $args );
}

output child terms of current custom taxonomy term with images

Just wondering if anyone can help with this - banging my head for days...
From a custom taxonomy page (taxonomy_genre.php), I need to output the child terms with images.
I'm using the 'Taxonomy Images' plugin to set the image.
Code below - thanks in advance if anyone can give me some pointers!
code #1 outputs all terms including parent terms.
code #2 outputs the correct terms but with no images
essentially I'm trying to amalgamate the two.
code #1:
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args = array(
'taxonomy' => $current_term->taxonomy,
'child_of' => $current_term->term_id,
'term_args' => array(
'orderby' => 'id',
'order' => 'ASC',
'hierarchical' => 0,
),
);
$cats = apply_filters( 'taxonomy-images-get-terms', '', $args );
foreach ($cats as $cat) {
echo '<li><a href="' . get_category_link($cat) . '" title="'. $cat->name .'">' ;
echo wp_get_attachment_image( $cat->image_id, 'detail' );
echo $cat->name ;
echo '</a></li>';
}
code #2
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$cats = wp_list_categories( array(
'child_of' => $current_term->term_id,
'taxonomy' => $current_term->taxonomy,
'hide_empty' => 0,
'hierarchical' => false,
'depth' => 2,
'title_li' => ''
));
foreach ((array)$cats as $cat) {
$catdesc = $cat->category_description;
echo '<li>'. wp_get_attachment_image( $cat->image_id, 'detail' ) . $cat->cat_name . '</li>'; }
Sorted - used:
'parent' => $current_term->term_id,
instead of:
child_of => $current_term->term_id,

Wordpress get image url (currently using wp_get_attachment_url)

Having a little trouble in getting image attachment urls in wordpress. This is my code so far:
<?php // find images attached to this post / page.
global $post;
$thumb_ID = get_post_thumbnail_id( $post->ID );
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'exclude' => $thumb_ID,
'post_parent' => $post->ID
);
$images = get_posts($args);
?>
<?php // Loop through the images and show them
if($images)
{
foreach($images as $image)
{
echo wp_get_attachment_image_url($image->ID, $size='attached-image');
}
}
?>
Which returns nothing. If I swap out wp_get_attachment_image_url($image->ID, $size='attached-image'); for wp_get_attachment_image($image->ID, $size='attached-image'); this works fine, but brings in the image rather than just the url.
The following code will loop through all image attachments and output the src url. Note that there are two methods shown, depending on your need.
<?php
global $post;
$args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_mime_type' => 'image', 'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
// Method #1: Allows you to define the image size
$src = wp_get_attachment_image_src( $attachment->ID, "attached-image");
if ($src) {echo $src[0];}
// Method #2: Would always return the "attached-image" size
echo $attachment->guid;
}
}
?>

Resources