currency-switcher
4 weeks ago
class-wcml-custom-prices.php
4 weeks ago
class-wcml-multi-currency-configuration.php
4 weeks ago
class-wcml-multi-currency-coupons.php
1 year ago
class-wcml-multi-currency-install.php
4 weeks ago
class-wcml-multi-currency-orders.php
4 weeks ago
class-wcml-multi-currency-prices.php
4 weeks ago
class-wcml-multi-currency-reports.php
4 weeks ago
class-wcml-multi-currency-resources.php
4 weeks ago
class-wcml-multi-currency-shipping.php
4 weeks ago
class-wcml-multi-currency-table-rate-shipping.php
4 weeks ago
class-wcml-multi-currency.php
4 weeks ago
class-wcml-price-filter.php
4 weeks ago
class-wcml-w3tc-multi-currency.php
4 weeks ago
class-wcml-multi-currency-reports.php
188 lines
| 1 | <?php |
| 2 | |
| 3 | use WCML\COT\Helper as COTHelper; |
| 4 | |
| 5 | class WCML_Multi_Currency_Reports { |
| 6 | |
| 7 | const TOP_SELLER_QUERY_USE_SELECT_ORDERS_FROM = '8.8.0'; |
| 8 | |
| 9 | private $woocommerce_wpml; |
| 10 | private $wpdb; |
| 11 | |
| 12 | protected $reports_currency; |
| 13 | |
| 14 | public function __construct( woocommerce_wpml $woocommerce_wpml, wpdb $wpdb ) { |
| 15 | $this->woocommerce_wpml = $woocommerce_wpml; |
| 16 | $this->wpdb = $wpdb; |
| 17 | } |
| 18 | |
| 19 | public function add_hooks() { |
| 20 | |
| 21 | add_action( 'init', [ $this, 'reports_init' ] ); |
| 22 | |
| 23 | if ( is_admin() ) { |
| 24 | |
| 25 | add_action( 'wp_ajax_wcml_reports_set_currency', [ $this, 'set_reports_currency' ] ); |
| 26 | |
| 27 | add_action( 'wc_reports_tabs', [ $this, 'reports_currency_selector' ] ); |
| 28 | |
| 29 | if ( current_user_can( 'view_woocommerce_reports' ) || |
| 30 | current_user_can( 'manage_woocommerce' ) || |
| 31 | current_user_can( 'publish_shop_orders' ) |
| 32 | ) { |
| 33 | add_filter( 'woocommerce_dashboard_status_widget_top_seller_query', [ |
| 34 | $this, |
| 35 | 'filterDashboardstatusWidgetTopSellerQuery' |
| 36 | ] ); |
| 37 | } |
| 38 | |
| 39 | add_action( 'current_screen', [ $this, 'admin_screen_loaded' ], 10, 1 ); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | public function admin_screen_loaded( $screen ) { |
| 44 | |
| 45 | if ( $screen->id === 'dashboard' ) { |
| 46 | add_filter( 'woocommerce_reports_get_order_report_query', [ |
| 47 | $this, |
| 48 | 'filterOrdersAsPostsByCurrencyPostmeta' |
| 49 | ] ); |
| 50 | } |
| 51 | |
| 52 | } |
| 53 | |
| 54 | public function reports_init() { |
| 55 | |
| 56 | $isReportsPage = isset( $_GET['page'] ) && 'wc-reports' === $_GET['page']; |
| 57 | |
| 58 | if( $isReportsPage || \WCML\Rest\Functions::isRestApiRequest() ){ |
| 59 | add_filter( 'woocommerce_reports_get_order_report_query', [ $this, 'admin_reports_query_filter' ] ); |
| 60 | } |
| 61 | |
| 62 | if ( $isReportsPage ) { |
| 63 | |
| 64 | $wcml_reports_set_currency_nonce = esc_js( wp_create_nonce( 'reports_set_currency' ) ); |
| 65 | $wcml_reports_set_currency_script = <<<JS |
| 66 | jQuery('#dropdown_shop_report_currency').on('change', function(){ |
| 67 | jQuery.ajax({ |
| 68 | url: ajaxurl, |
| 69 | type: 'post', |
| 70 | data: { |
| 71 | action: 'wcml_reports_set_currency', |
| 72 | currency: jQuery('#dropdown_shop_report_currency').val(), |
| 73 | wcml_nonce: '$wcml_reports_set_currency_nonce' |
| 74 | }, |
| 75 | success: function( response ){ |
| 76 | if(typeof response.error !== 'undefined'){ |
| 77 | alert(response.error); |
| 78 | }else{ |
| 79 | window.location = window.location.href; |
| 80 | } |
| 81 | } |
| 82 | }) |
| 83 | }); |
| 84 | JS; |
| 85 | |
| 86 | $handle = 'wcml_reports_set_currency_dropdown'; |
| 87 | wp_register_script( $handle, '', [ 'jquery' ], WCML_VERSION, true ); |
| 88 | wp_enqueue_script( $handle ); |
| 89 | wp_add_inline_script( $handle, $wcml_reports_set_currency_script ); |
| 90 | |
| 91 | $this->reports_currency = $_COOKIE['_wcml_reports_currency'] ?? wcml_get_woocommerce_currency_option(); |
| 92 | |
| 93 | add_filter( 'woocommerce_currency_symbol', [ $this, '_set_reports_currency_symbol' ] ); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public function admin_reports_query_filter( $query ) { |
| 98 | |
| 99 | if( \WCML\Rest\Functions::isRestApiRequest() ) { |
| 100 | $this->reports_currency = $this->woocommerce_wpml->multi_currency->get_rest_currency(); |
| 101 | } |
| 102 | |
| 103 | if( !$this->reports_currency ){ |
| 104 | return $query; |
| 105 | } |
| 106 | |
| 107 | $query['join'] .= " LEFT JOIN {$this->wpdb->postmeta} AS meta_order_currency ON meta_order_currency.post_id = posts.ID "; |
| 108 | $query['where'] .= sprintf( " AND meta_order_currency.meta_key='_order_currency' AND meta_order_currency.meta_value = '%s' ", |
| 109 | $this->reports_currency ); |
| 110 | |
| 111 | return $query; |
| 112 | } |
| 113 | |
| 114 | public function _set_reports_currency_symbol( $currency ) { |
| 115 | static $no_recur = false; |
| 116 | if ( ! empty( $this->reports_currency ) && empty( $no_recur ) ) { |
| 117 | $no_recur = true; |
| 118 | $currency = get_woocommerce_currency_symbol( $this->reports_currency ); |
| 119 | $no_recur = false; |
| 120 | } |
| 121 | |
| 122 | return $currency; |
| 123 | } |
| 124 | |
| 125 | public function set_reports_currency() { |
| 126 | |
| 127 | $nonce = filter_input( INPUT_POST, 'wcml_nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 128 | if ( ! $nonce || ! wp_verify_nonce( $nonce, 'reports_set_currency' ) ) { |
| 129 | echo json_encode( [ 'error' => __( 'Invalid nonce', 'woocommerce-multilingual' ) ] ); |
| 130 | die(); |
| 131 | } |
| 132 | |
| 133 | $cookie_name = '_wcml_reports_currency'; |
| 134 | setcookie( $cookie_name, filter_input( INPUT_POST, 'currency', FILTER_SANITIZE_FULL_SPECIAL_CHARS ), |
| 135 | time() + 86400, COOKIEPATH, COOKIE_DOMAIN ); |
| 136 | |
| 137 | exit; |
| 138 | |
| 139 | } |
| 140 | |
| 141 | public function reports_currency_selector() { |
| 142 | $currency_codes = $this->woocommerce_wpml->multi_currency->get_currency_codes(); |
| 143 | $currencies = get_woocommerce_currencies(); |
| 144 | |
| 145 | remove_filter( 'woocommerce_currency_symbol', [ $this, '_set_reports_currency_symbol' ] ); |
| 146 | ?> |
| 147 | <select id="dropdown_shop_report_currency" style="margin-left:5px;"> |
| 148 | <?php if ( empty( $currency_codes ) ): ?> |
| 149 | <option value=""><?php _e( 'Currency - no orders found', 'woocommerce-multilingual' ) ?></option> |
| 150 | <?php else: ?> |
| 151 | <?php foreach ( $currency_codes as $currency ): ?> |
| 152 | <option value="<?php echo esc_attr( $currency ) ?>" <?php selected( $currency, $this->reports_currency ); ?>> |
| 153 | <?php printf( "%s (%s)", $currencies[ $currency ], get_woocommerce_currency_symbol( $currency ) ) ?> |
| 154 | </option> |
| 155 | <?php endforeach; ?> |
| 156 | <?php endif; ?> |
| 157 | </select> |
| 158 | <?php |
| 159 | |
| 160 | add_filter( 'woocommerce_currency_symbol', [ $this, '_set_reports_currency_symbol' ] ); |
| 161 | } |
| 162 | |
| 163 | public function filterOrdersAsPostsByCurrencyPostmeta( $query, $tableAlias = 'posts' ) { |
| 164 | |
| 165 | $currency = $this->woocommerce_wpml->multi_currency->admin_currency_selector->get_cookie_dashboard_currency(); |
| 166 | |
| 167 | $query['join'] .= " INNER JOIN {$this->wpdb->postmeta} AS currency_postmeta ON {$tableAlias}.ID = currency_postmeta.post_id"; |
| 168 | $query['where'] .= $this->wpdb->prepare( " AND currency_postmeta.meta_key = '_order_currency' AND currency_postmeta.meta_value = %s", $currency ); |
| 169 | |
| 170 | return $query; |
| 171 | } |
| 172 | |
| 173 | public function filterDashboardstatusWidgetTopSellerQuery( $query ) { |
| 174 | if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, self::TOP_SELLER_QUERY_USE_SELECT_ORDERS_FROM, '<' ) ) { |
| 175 | return $this->filterOrdersAsPostsByCurrencyPostmeta( $query ); |
| 176 | } |
| 177 | |
| 178 | if ( false === COTHelper::isUsageEnabled() ) { |
| 179 | return $this->filterOrdersAsPostsByCurrencyPostmeta( $query, 'orders' ); |
| 180 | } |
| 181 | |
| 182 | $query['where'] .= $this->wpdb->prepare( " AND orders.currency = %s", $this->woocommerce_wpml->multi_currency->admin_currency_selector->get_cookie_dashboard_currency() ); |
| 183 | |
| 184 | return $query; |
| 185 | } |
| 186 | |
| 187 | } |
| 188 |