-
AuthorPosts
-
July 24, 2014 at 8:41 pm #73286
Hi Guys,
Hope you can help.
Would like to add additional filters to orderby class allowing users to sort by category or tag from the woocommerce dropdown within woocommerce orderby.php<form class=”woocommerce-ordering” method=”get”>
<select name=”orderby” class=”orderby”>
<?php
$catalog_orderby = apply_filters( ‘woocommerce_catalog_orderby’, array(
‘menu_order’ => __( ‘Default sorting’, ‘woocommerce’ ),
‘popularity’ => __( ‘Sort by popularity’, ‘woocommerce’ ),
‘rating’ => __( ‘Sort by average rating’, ‘woocommerce’ ),
‘date’ => __( ‘Sort by newness’, ‘woocommerce’ ),
‘price’ => __( ‘Sort by price: low to high’, ‘woocommerce’ ),
‘price-desc’ => __( ‘Sort by price: high to low’, ‘woocommerce’ )) );
Hope you can help.
Cheers,
Claire 🙂July 25, 2014 at 10:17 pm #73692Hi Claire,
Thank you for writing in!
Sorting by category is not possible using wp query, but filtering is.
Not sure if this will work, this sample is for “Audio” and “Image” category. But you may hire a developer that will get you this working using this idea. And you could also get him work for category sorting using raw mysql queries rather than using wp query.
Add this code at your child theme’s functions.php
add_filter( 'woocommerce_catalog_orderby', function($orders){ $orders['category-audio']=>'Sort by Category : Audio'; $orders['category-image']=>'Sort by Category : Image'; return $orders; } ); add_action( 'pre_get_posts', function ( $query ) { if ( is_shop() ) { $category = explode( '-', $query->get('orderby') ); if($category[0] == 'category') { array_shift($category); $category = implode('-', $category); $query->set( 'tax_query', array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $category )); $query->set('orderby', 'title date'); //hire someone and replace this with sorting using raw mysql query. } } });
Thanks!
-
AuthorPosts