Advertisement
businessdad

WooCommerce Currency Switcher - Orders list by currency

Dec 31st, 2017
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | None | 0 0
  1. /**
  2.  * Aelia Currency Switcher for WooCommerce - Filter the orders on the orders list
  3.  * page, showing only the ones with specific currencies depending on users' role.
  4.  *
  5.  * HOW TO USE THIS CODE
  6.  * Add the code to the bottom of your theme's functions.php file (see https://www.skyverge.com/blog/add-custom-code-to-wordpress/).
  7.  * The code will run on the Orders List page, check the user's role, and only show
  8.  * orders the specific currency associated to that role.
  9.  *
  10.  * GPL DISCLAIMER
  11.  * Because this code program is free of charge, there is no warranty for it, to the extent permitted by applicable law.
  12.  * Except when otherwise stated in writing the copyright holders and/or other parties provide the program "as is"
  13.  * without warranty of any kind, either expressed or implied, including, but not limited to, the implied warranties of
  14.  * merchantability and fitness for a particular purpose. The entire risk as to the quality and performance of the program
  15.  * is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction.
  16.  *
  17.  * Need a consultation, or assistance to customise this code? Find us on Codeable: https://aelia.co/hire_us
  18.  */
  19. add_filter('request', function($vars) {
  20.   global $typenow, $wpdb;
  21.  
  22.   // Only alter the query on the Orders List page
  23.   if($typenow == 'shop_order')  {
  24.     // By default, don't filter orders
  25.     $currency = '';
  26.  
  27.     // Check if the user can only see GBP orders
  28.     if(current_user_can('see_only_gbp_orders')) {
  29.       $currency = 'GBP';
  30.     }
  31.     // Check if the user can only see EUR orders
  32.     if(current_user_can('see_only_eur_orders')) {
  33.       $currency = 'EUR';
  34.     }
  35.  
  36.     // Filter orders by currency
  37.     if(!empty($currency)) {
  38.       $vars['meta_query']['relation'] = 'AND';
  39.       // Only show orders in the specific currency
  40.       $vars['meta_query'] = array(
  41.         array(
  42.           'key'   => '_order_currency',
  43.           'value' => $currency,
  44.           'compare' => '=',
  45.         ),
  46.       );
  47.     }
  48.   }
  49.   return $vars;
  50. }, 15 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement