Created
April 27, 2020 12:03
-
-
Save gillesgoetsch/b0d1ea8c473f676a5620daa9cb8e1a56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Filter Orders by currency | |
* Description: Adds the ability to filter orders by currency. | |
* Author: Gilles Goetsch | |
* Author URI: https://gillesgoetsch.ch/ | |
* Version: 1.0.0 | |
* Text Domain: wc-filter-orders | |
* Reference: http://www.skyverge.com/product/woocommerce-filter-orders/ | |
* | |
* GitHub Plugin URI: bekarice/woocommerce-filter-orders/ | |
* GitHub Branch: master | |
* | |
* License: GNU General Public License v3.0 | |
* License URI: http://www.gnu.org/licenses/gpl-3.0.html | |
* | |
* @package WC-Filter-Orders-currency | |
* @author Gilles Goetsch | |
* @category Admin | |
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 | |
* | |
*/ | |
// fire it up! | |
add_action( 'plugins_loaded', 'wc_filter_orders_by_currency' ); | |
/** | |
* Plugin Description | |
* | |
* Adds custom filtering to the orders screen to allow filtering by currency used. | |
*/ | |
class WC_Filter_Orders_By_currency { | |
const VERSION = '1.0.0'; | |
/** @var WC_Filter_Orders_By_currency single instance of this plugin */ | |
protected static $instance; | |
/** | |
* WC_Filter_Orders constructor. | |
* | |
* @since 1.0.0 | |
*/ | |
public function __construct() { | |
// load translations | |
add_action( 'init', array( $this, 'load_translation' ) ); | |
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) { | |
// adds the currency filtering dropdown to the orders page | |
add_action( 'restrict_manage_posts', array( $this, 'filter_orders_by_currency_used' ) ); | |
// makes currencys filterable | |
add_action('pre_get_posts', array( $this, 'filter_query_by_currency')); | |
} | |
} | |
/** Plugin methods ***************************************/ | |
/** | |
* Adds the currency filtering dropdown to the orders list | |
* | |
* @since 1.0.0 | |
*/ | |
public function filter_orders_by_currency_used() { | |
global $typenow; | |
if ( 'shop_order' === $typenow ) { | |
$currencies = array('CHF','EUR','GBP','USD'); // could be automated | |
if ( ! empty( $currencies ) ) : ?> | |
<select name="_currency_used" id="dropdown_currency_used"> | |
<option value=""> | |
<?php esc_html_e( 'Currency', 'wc-filter-orders' ); ?> | |
</option> | |
<?php foreach ( $currencies as $currency ) : ?> | |
<option value="<?php echo esc_attr( $currency ); ?>" <?php echo esc_attr( isset( $_GET['_currency_used'] ) ? selected( $currency, $_GET['_currency_used'], false ) : '' ); ?>> | |
<?php echo esc_html( $currency ); ?> | |
</option> | |
<?php endforeach; ?> | |
</select> | |
<?php endif; | |
} | |
} | |
function filter_query_by_currency($query) { | |
global $typenow; | |
if ( 'shop_order' === $typenow && isset( $_GET['_currency_used'] ) && ! empty( $_GET['_currency_used'] ) ) { | |
$query->set('meta_query', array( array( | |
'key' => '_order_currency', | |
'value' => $_GET['_currency_used'] | |
) ) ); | |
} | |
} | |
/** Helper methods ***************************************/ | |
/** | |
* Load Translations | |
* | |
* @since 1.0.0 | |
*/ | |
public function load_translation() { | |
// localization | |
load_plugin_textdomain( 'wc-filter-orders', false, dirname( plugin_basename( __FILE__ ) ) . '/i18n/languages' ); | |
} | |
/** | |
* Main WC_Filter_Orders_By_currency Instance, ensures only one instance | |
* is/can be loaded | |
* | |
* @since 1.1.0 | |
* | |
* @see wc_filter_orders_by_currency() | |
* @return WC_Filter_Orders_By_currency | |
*/ | |
public static function instance() { | |
if ( is_null( self::$instance ) ) { | |
self::$instance = new self(); | |
} | |
return self::$instance; | |
} | |
} | |
/** | |
* Returns the One True Instance of WC_Filter_Orders_By_currency | |
* | |
* @since 1.1.0 | |
* | |
* @return \WC_Filter_Orders_By_currency | |
*/ | |
function wc_filter_orders_by_currency() { | |
return WC_Filter_Orders_By_currency::instance(); | |
} | |
/* | |
if(is_admin()) { | |
wc_filter_orders_by_currency(); // do this if added to the functions.php | |
} | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment