简体   繁体   中英

Woocommerce how to display products by product SKU

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?

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

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;
    }

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, az; 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' ); 

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'
                                    )
                                ),
                    );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM