php - Custom sorting products when title is made of letters and numbers in WooCommerce -


so, did digging wasn't able find correct way this.

i have products on woocommerce (wordpress), title composed of letters mixed numbers. need products print (e.g.):

fnc21000-c
fnc24500-c
fnc232500-c

but, of course, wordpress gives me:

fnc21000-c
fnc232500-c
fnc24500-c

is there anyway achieve this?

i think there's might on wp_query i'm newbie php, , know of possible fixes on php, others on mysql... , fact title both string , integer, makes me feel clueless on how write this...

does has suggestions?

below, class-wc-query.php, believe file need edit:

/**  * returns array of arguments ordering products based on selected values.  *  * @access public  * @return array  */ public function get_catalog_ordering_args( $orderby = '', $order = '' ) {     // ordering query string unless defined     if ( ! $orderby ) {         $orderby_value = isset( $_get['orderby'] ) ? wc_clean( $_get['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );          // order + orderby args string         $orderby_value = explode( '-', $orderby_value );         $orderby       = esc_attr( $orderby_value[0] );         $order         = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order;     }      $orderby = strtolower( $orderby );     $order   = strtoupper( $order );     $args    = array();      // default - menu_order     $args['orderby'] = "strlen('title') => 'asc', 'title' => 'asc'";     //      $args['orderby']  = 'title';     $args['order']    = $order == 'desc' ? 'desc' : 'asc';     $args['meta_key'] = '';      switch ( $orderby ) {         case 'rand' :             $args['orderby']  = 'rand';         break;         case 'date' :             $args['orderby']  = 'date id';             $args['order']    = $order == 'asc' ? 'asc' : 'desc';         break;         case 'price' :             $args['orderby']  = "meta_value_num id";             $args['order']    = $order == 'desc' ? 'desc' : 'asc';             $args['meta_key'] = '_price';         break;         case 'popularity' :             $args['meta_key'] = 'total_sales';              // sorting handled later though hook             add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );         break;         case 'rating' :             // sorting handled later though hook             add_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );         break;         case 'title' :         $args['orderby'] = "strlen('title') => 'asc', 'title' => 'asc'"; //$args['orderby']  = 'meta_value meta_value_num title';             $args['order']    = $order == 'desc' ? 'desc' : 'asc';         break;     }      return apply_filters( 'woocommerce_get_catalog_ordering_args', $args ); } 

in php, call natsort().

code: (demo)

$array=['fnd100000-c','fnc24500-c','fnc232500-c','fmc30000-c','fnc21000-c']; natsort($array); var_export($array); 

output:

array (   3 => 'fmc30000-c',   4 => 'fnc21000-c',   1 => 'fnc24500-c',   2 => 'fnc232500-c',   0 => 'fnd100000-c', ) 

in mysql, long format is...

  • 3 letters then
  • a series of numbers then
  • a hyphen then
  • one or more characters

...you can order calling substring() , substring_index()

table & query: (demo)

create table `test` (   `prdid` varchar(50) not null ) engine=myisam default charset=latin1;  insert `test` (`prdid`) values ('fmc30000-c'), ('fnc21000-b'), ('fnc21000-c'), ('fnc232500-c'), ('fnc24500-c'), ('fnd100000-c');  alter table `test`   add primary key (`prdid`);  select prdid,        substring(prdid,1,3),        substring(substring_index(prdid,'-',1),4),        substring_index(prdid,'-',-1) `test` order     substring(prdid,1,3),     cast(substring(substring_index(prdid,'-',1),4) unsigned),     substring_index(prdid,'-',-1) 

output:

prdid       | substring(prdid,1,3) | substring(substring_index(prdid,'-',1),4) | substring_index(prdid,'-',-1) ---------------------------------------------------------------------------------------------------------------- fmc30000-c  |         fmc          |                  30000                    |               c fnc21000-b  |         fnc          |                  21000                    |               b fnc21000-c  |         fnc          |                  21000                    |               c fnc24500-c  |         fnc          |                  24500                    |               c fnc232500-c |         fnc          |                  232500                   |               c fnd100000-c |         fnd          |                  100000                   |               c 

here's shot in dark non-wordpress developer...

$args['orderby'] = "substr('title',0,3) => 'asc', substr(strstr('title','-',true),3) => 'asc', substr('title',strpos('title','-')+1) => 'asc'";

demo

after looking at: https://github.com/woocommerce/woocommerce/blob/master/includes/class-wc-query.php looks orderby components space delimited, not comma delimited. try this:

$args['orderby'] = "substr('title',0,3) substr(strstr('title','-',true),3) substr('title',strpos('title','-')+1)"; $args['order'] = $order==='desc'?'desc':'asc'; 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -