Advertisement
Guest User

Untitled

a guest
Mar 11th, 2014
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.75 KB | None | 0 0
  1. /************* Add sorting by attributes **************/
  2.  
  3. /**
  4.  *  Defines the criteria for sorting with options defined in the method below
  5.  */
  6. add_filter('woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args');
  7.  
  8. function custom_woocommerce_get_catalog_ordering_args( $args ) {
  9.     global $wp_query;
  10.         // Changed the $_SESSION to $_GET
  11.     if (isset($_GET['orderby'])) {
  12.         switch ($_GET['orderby']) :
  13.             case 'velikost1' :
  14.                 $args['order'] = 'ASC';
  15.                 $args['meta_key'] = 'velikost';
  16.                 $args['orderby'] = 'meta_value_num';
  17.             break;
  18.             case 'velikost2' :
  19.                 $args['order'] = 'DESC';
  20.                 $args['meta_key'] = 'velikost';
  21.                 $args['orderby'] = 'meta_value_num';
  22.             break;
  23.         endswitch;
  24.     }
  25.     return $args;
  26. }
  27.  
  28. /**
  29.  *  Adds the sorting options to dropdown list .. The logic/criteria is in the method above
  30.  */
  31. add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby');
  32.  
  33. function custom_woocommerce_catalog_orderby( $sortby ) {
  34.     $sortby['velikost_1'] = 'Seřadit podle velikosti 1';
  35.     $sortby['velikost_2'] = 'Seřadit podle velikosti 2';
  36.     return $sortby;
  37. }
  38.  
  39.  
  40.  
  41. /**
  42.  *  Save custom attributes as post's meta data as well so that we can use in sorting and searching
  43.  */
  44. add_action( 'save_post', 'save_woocommerce_attr_to_meta' );
  45. function save_woocommerce_attr_to_meta( $post_id ) {
  46.         // Get the attribute_names .. For each element get the index and the name of the attribute
  47.         // Then use the index to get the corresponding submitted value from the attribute_values array.
  48.     foreach( $_REQUEST['attribute_names'] as $index => $value ) {
  49.         update_post_meta( $post_id, $value, $_REQUEST['attribute_values'][$index] );
  50.     }
  51. }
  52. /************ End of Sorting ***************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement