Reputation: 2401
One my client want to display all woocommerce product by product SKU
.
Normally i used following code for display products.
$postArg = array('post_type'=>'product',
'post_status'=>'publish',
'posts_per_page'=>-1,
'orderby'=>'data',
'order'=>'DESC',
);
$queryGetFiles = get_posts($postArg);
But now my client want to show all products by product SKU
in front side.
SKU like this 1041-14, 1041-12, 1041-16 ,1041,2001,3501
all product has different sku value and display which doesn't have "-"
character
Anyone know how should i do this?
Upvotes: 4
Views: 5396
Reputation: 1
I registered just now to reply.
I had the same exact issue with a customer who wanted her products sorted by sku. The scripts above order the products by the general Wordpress id and not the product sku. The following is what sorted the products by the sku:
add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
function am_woocommerce_catalog_orderby( $args ) {
$args['meta_key'] = '_sku';
$args['orderby'] = '_sku';
$args['order'] = 'asc';
return $args;
}
Upvotes: 0
Reputation: 1
It seems later versions of woo-commerce have added a manual drag-and-drop sort on the product pages and this overrides any function we use to try and order by SKU. The following worked for me.
add_action( 'woocommerce_product_query', 'custom_sort_products_by_sku' );
function custom_sort_products_by_sku( $query ) {
// Check if on a product category archive page
if ( ! is_admin() && is_product_category() && $query->is_main_query() ) {
// Set the orderby parameter to 'meta_value' for SKU
$query->set( 'orderby', 'meta_value' );
// Set the meta key to '_sku' which is the SKU
$query->set( 'meta_key', '_sku' );
// Set the order parameter to 'ASC' for ascending order
$query->set( 'order', 'ASC' );
}
}
// Add the 'Sort by SKU' option to the
sorting dropdown
add_filter( 'woocommerce_catalog_orderby',
'add_sort_by_sku_to_dropdown' );
function add_sort_by_sku_to_dropdown( $options ) {
// Add 'Sort by SKU' option
$options['sort_by_sku'] = __('Sort by SKU', 'woocommerce');
return $options;
}
// Handle the sorting logic for 'Sort by SKU'
add_action( 'woocommerce_product_query', 'sort_products_by_sku' );
function sort_products_by_sku( $query ) {
// Check if 'orderby' is set to 'sort_by_sku'
if ( isset( $_GET['orderby'] ) && 'sort_by_sku' === $_GET['orderby'] ) {
$query->set( 'orderby', 'meta_value' );
$query->set( 'meta_key', '_sku' );
$query->set( 'order', 'ASC' );
}
}
// Ensure the 'Sort by SKU' option works correctly with WooCommerce's session
handling
add_filter( 'woocommerce_default_catalog_orderby_options', 'add_sort_by_sku_to_dropdown' );
add_filter( 'woocommerce_catalog_orderby', 'add_sort_by_sku_to_dropdown' );
Upvotes: 0
Reputation: 2401
Thank You All guys for support.
I have resolved it by my self using following.
$postArg = array('post_type'=>'product',
'post_status'=>'publish',
'posts_per_page'=>-1,
'orderby'=>'date',
'order'=>'DESC',
'meta_query' => array(
array(
'key' => '_sku',
'value' => '-',
'compare' => 'NOT LIKE'
)
),
);
Upvotes: 0
Reputation: 1624
Please try this. Let me know if this works perfectly....
/**
* Adds the ability to sort products in the shop based on the SKU
* Can be combined with tips here to display the SKU on the shop page: https://www.skyverge.com/blog/add-information-to-woocommerce-shop-page/
*/
function sv_add_sku_sorting( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'sku' == $orderby_value ) {
$args['orderby'] = 'meta_value';
$args['order'] = 'asc';
// ^ lists SKUs alphabetically 0-9, a-z; change to desc for reverse alphabetical
$args['meta_key'] = '_sku';
}
return $args;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'sv_add_sku_sorting' );
function sv_sku_sorting_orderby( $sortby ) {
$sortby['sku'] = 'Sort by SKU';
// Change text above as desired; this shows in the sorting dropdown
return $sortby;
}
add_filter( 'woocommerce_catalog_orderby', 'sv_sku_sorting_orderby' );
add_filter( 'woocommerce_default_catalog_orderby_options', 'sv_sku_sorting_orderby' );
Upvotes: 0
Reputation: 336
Try to put code in function.php
add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
function am_woocommerce_catalog_orderby( $args ) {
$args['meta_key'] = '_sku';
$args['orderby'] = 'meta_value_num';
$args['order'] = 'desc';
return $args;
}
Upvotes: 2
Reputation: 2755
Try this
$postArg = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_key' => '_sku',
'orderby' => 'meta_value' // meta_value_num if ordered by intergers
'order' => 'DESC',
);
$queryGetFiles = get_posts($postArg);
Answered with help of this post
Upvotes: 6