Woocommerce how to display products by product SKU - php

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

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

Related

WP All Import don't importing values from custom fields

I have custom tab for Frequently Bought Together products in my woocommerce site, that shows up on single product page. When i update products via WP All Import, only one product show up in custom tab and should be a two. Here is the settings of custom fields import and actually view of the single product page.
custom-fields-settings
single-product-accessory-tab
I'll provide Single Product Accessories template, just to make it clear
accessorie template function
global $product;
$loop_columns = apply_filters( 'mc_accessories_loop_columns', 4 );
$posts_per_page = 4;
if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.3', '<' ) ) {
global $woocommerce_loop;
$woocommerce_loop['columns'] = $loop_columns;
} else {
wc_set_loop_prop( 'columns', $loop_columns );
}
$product_id = mc_wc_get_product_id( $product );
$accessories = MediaCenter_WC_Helper::get_accessories( $product );
array_unshift( $accessories, $product_id );
if ( sizeof( $accessories ) === 0 && !array_filter( $accessories ) ) {
return;
}
$meta_query = WC()->query->get_meta_query();
$args = apply_filters( 'mc_accessories_query_args', array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'no_found_rows' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => 'post__in',
'post__in' => $accessories,
'meta_query' => $meta_query
) );
unset( $args['meta_query'] );
$products = new WP_Query( $args );
$add_to_cart_checkbox = '';
$total_price = 0;
$count = 0;
Any help will be appreciated
Solution is so easy, just need to import post Id's, i lost two weeks on that problem.

Allow to sort products by featured on WooCommerce archive pages

I am trying to give users the ability to sort products on a store page by featured. I use the following code for this purpose:
add_filter( 'woocommerce_get_catalog_ordering_args', 'victor_get_catalog_ordering_args' );
function victor_get_catalog_ordering_args( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'featured' == $orderby_value ) {
$args['orderby'] = '_featured';
$args['order'] = 'DESC';
$args['meta_key'] = '_featured';
}
return $args;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'victor_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'victor_catalog_orderby' );
function victor_catalog_orderby( $sortby ) {
$sortby['featured'] = 'Featured';
return $sortby;
}
But it doesn't work. I'm trying to sort products by featured using: /?orderby=featured But nothing is displayed, except for the message that no products were found for the request. But I know that in the admin panel I have more than 10 products marked as featured. Please help with this issue.I need to be sure to understand what I'm doing wrong.
Your code is a bit outdated and deprecated since Woocommerce 3, where featured products are now set as product_visibility taxonomy for the term name featured, so this needs to be handled in a different way as follows:
add_filter( 'woocommerce_catalog_orderby', 'hugo_boss_catalog_orderby' );
function hugo_boss_catalog_orderby( $orderby ) {
$orderby['featured'] = __('Featured', 'woocommerce');
return $orderby;
}
add_action( 'woocommerce_product_query', 'obama_trump_product_query' );
function obama_trump_product_query( $q ) {
if ( ! is_admin() && isset($_GET['orderby']) && 'featured' === esc_attr($_GET['orderby']) ) {
$tax_query = $q->get('tax_query');
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
);
$q->set( 'tax_query', $tax_query );
$q->set( 'order', 'DESC' ); // Or "ASC"
}
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
Related:
Get WooCommerce featured products in a WP_Query
Woocommerce meta_query not working for featured products

Get and display related products in WooCommerce

I have included WooCommerce related products in a theme with the following:
<?php wc_get_template( 'single-product/related.php' ); ?>
This has been copied into my template and is executing.
However, even though I have added various upsells with this product the $related_products variable (used in the loop) is NULL. Is there any other variables at play in order to start showing these related products?
You need much more than that (and the post_id need to be a product):
global $product; // If not set…
if( ! is_a( $product, 'WC_Product' ) ){
$product = wc_get_product(get_the_id());
}
$args = array(
'posts_per_page' => 4,
'columns' => 4,
'orderby' => 'rand',
'order' => 'desc',
);
$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' );
$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', $args['columns'] );
wc_get_template( 'single-product/related.php', $args );
Or in a shorter way (which will give you the same):
global $product;
if( ! is_a( $product, 'WC_Product' ) ){
$product = wc_get_product(get_the_id());
}
woocommerce_related_products( array(
'posts_per_page' => 4,
'columns' => 4,
'orderby' => 'rand'
) );
Both ways are tested and works…

Add a new custom default ordering catalog option in Woocommerce

I am trying to get the default order of Woocommerce to be order by SKU.
I have changed the order in the woocommerce settings and added SKU like this:
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'] = 'Sorteer op referentie';
// 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' );
On pageload it still orders on popularity instead of on SKU.
But the dropdown shows order by SKU (Sorteer op referentie)
If I go to another ordering and go back it will correctly order them with ?orderby=sku in the querystring.
Updated (Dec 2018)
You are not using the right hooks in the right way to get default ordering catalog by sku. There is also some missing needed additional code:
add_filter( 'woocommerce_get_catalog_ordering_args', 'enable_catalog_ordering_by_sku' );
function enable_catalog_ordering_by_sku( $args ) {
if ( isset( $_GET['orderby'] ) ) {
if ( 'sku' == $_GET['orderby'] ) {
return array(
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_key' => '_sku',
);
}
// Make a clone of "menu_order" (default option)
elseif ( 'natural_order' == $_GET['orderby'] ) {
return array( 'orderby' => 'menu_order title', 'order' => 'ASC' );
}
}
return $args;
}
add_filter( 'woocommerce_catalog_orderby', 'add_catalog_orderby_by_sku' );
function add_catalog_orderby_by_sku( $orderby_options ) {
// Insert "Sort by product reference (sku)" and the clone of "menu_order"
return array(
'sku' => __("Sort by product reference (sku)", "woocommerce"),
'natural_order' => __("Sort by natural shop order", "woocommerce"), // <== To be renamed at your convenience
) + $orderby_options ;
}
add_filter( 'woocommerce_default_catalog_orderby', 'default_catalog_orderby_sku' );
function default_catalog_orderby_sku( $default_orderby ) {
return 'sku';
}
add_action( 'woocommerce_product_query', 'product_query_by_sku' );
function product_query_by_sku( $q ) {
if ( ! isset( $_GET['orderby'] ) && ! is_admin() ) {
$q->set( 'orderby', 'meta_value_num' );
$q->set( 'order', 'ASC' );
$q->set( 'meta_key', '_sku');
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Related: Sort WooCommerce Products by SKU Numeric (1.2.3.4.5.6........)

Weird Order of Wordpress Category Post

Does anyone have ever seen this?
When we're accessing one of wordpress category post here:
On the top item, it's not the latest posted article instead it's the oldest one.
Which part of the php code should be changed ? It's for wordpress version of 4.5 from this link.
function change_category_order( $query ) {
$category = get_queried_object();
$cat_id=$category->term_id;
if ( $query->is_category($cat_id) && $query->is_main_query() ) {
$query->set( 'order', 'DESC' );
}
}
add_action( 'pre_get_posts', 'change_category_order' );
If you use any custom query add this also in post loop 'order' => 'DESC'
$args = array(
'post_type' => 'post',
'order' => 'DESC', );
$q = new WP_Query($args);
or paste this in function.php
add_action( 'pre_get_posts', 'my_change_sort_order');
function my_change_sort_order($query){
if(is_archive()):
//If you wanted it for the archive of a custom post type use: is_post_type_archive( $post_type )
//Set the order ASC or DESC
$query->set( 'order', 'DESC' );
//Set the orderby
//$query->set( 'orderby', 'title' );
endif;
};

Categories