Created
March 19, 2021 13:52
-
-
Save niloyBrightVessel/187db243055e8c013ae1edf9a1628a7b to your computer and use it in GitHub Desktop.
custom query for show products on catgeory page based on attribute
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* List WooCommerce Products by attributes | |
* | |
* ex: [woo_products_by_attributes attribute="colour" per_page="5"] | |
*/ | |
function woo_products_by_attributes_shortcode( $atts, $content = null ) { | |
global $woocommerce, $woocommerce_loop; | |
// Get attribuets | |
extract(shortcode_atts(array( | |
'per_page' => '-1', | |
'order' => 'desc', | |
), $atts)); | |
// Default ordering args | |
$ordering_args = $woocommerce->query->get_catalog_ordering_args( $order ); | |
$term = get_queried_object(); | |
$team_slug = $term->slug; 'amis-cv'; // $term->slug | |
ob_start(); | |
$get_the_attribute = bv_get_attribute_terms_in_product_cat($team_slug, 'pa_age-groups'); | |
$i = 0; | |
foreach($get_the_attribute as $attribute){ | |
// fw_print($attribute); | |
$term_desc = (term_description($attribute->term_id)) ? term_description($attribute->term_id) : ''; | |
echo '<h3 class="age-title-cat">'.$term_desc .$attribute->name.'</h3>'; | |
$i++; | |
$args = array( | |
'post_type' => 'product_variation', | |
'post_status' => 'publish', | |
'ignore_sticky_posts' => 1, | |
// 'orderby' => 'meta_value', | |
// 'meta_key' => 'product_tag', | |
'order' => $ordering_args['order'], | |
'posts_per_page' => $per_page, | |
'product_cat' => $team_slug, | |
'meta_query' => array( | |
array( | |
'key' => 'attribute_pa_age-groups', | |
'value' => $attribute->slug, | |
) | |
) | |
); | |
$products = new WP_Query( $args ); | |
if ( $products->have_posts() ) : | |
while ( $products->have_posts() ) : $products->the_post(); | |
$bp_product_ids_{$i}[] = wp_get_post_parent_id(get_the_id()) ; | |
endwhile; | |
endif; // have post | |
wp_reset_postdata(); | |
// fw_print($bp_product_ids_{$i}); | |
$args = array( | |
'post_type' => 'product', | |
'post_status' => 'publish', | |
'post__in' => $bp_product_ids_{$i}, | |
); | |
echo '<div class="class__content__cart container class__content__product__loop">'; | |
$loop = new WP_Query( $args ); | |
if ( $products->have_posts() ) : | |
while ( $loop->have_posts() ) : $loop->the_post(); | |
wc_get_template_part( 'contentt', 'productt' ); | |
endwhile; | |
endif; // have post | |
wp_reset_postdata(); | |
echo '</div>'; | |
} // for loop | |
return '<div class="woocommerce">' . ob_get_clean() . '</div>'; | |
} | |
add_shortcode("woo_products_by_attributes", "woo_products_by_attributes_shortcode"); | |
function bv_get_attribute_terms_in_product_cat( $category_term_name, $attribute_taxonomy ){ | |
global $wpdb; | |
$terms = $wpdb->get_results( "SELECT DISTINCT t.* | |
FROM {$wpdb->prefix}terms as t | |
JOIN {$wpdb->prefix}term_taxonomy as tt ON tt.term_id = t.term_id | |
JOIN {$wpdb->prefix}term_relationships as tr ON tt.term_taxonomy_id = tr.term_taxonomy_id | |
WHERE tt.taxonomy LIKE '$attribute_taxonomy' | |
AND tr.object_id IN ( SELECT DISTINCT tr2.object_id | |
FROM {$wpdb->prefix}term_relationships as tr2 | |
JOIN {$wpdb->prefix}term_taxonomy as tt2 ON tt2.term_taxonomy_id = tr2.term_taxonomy_id | |
JOIN {$wpdb->prefix}terms as t2 ON tt2.term_id = t2.term_id | |
WHERE tt2.taxonomy LIKE 'product_cat' AND t2.slug = '$category_term_name' | |
)" ); | |
return $terms; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment