AJAX
2 years ago
Admin_Page
2 years ago
Custom_WordPress_Columns
2 years ago
Data_Pruning
2 years ago
Date_Range
2 years ago
Email_Reports
2 years ago
Filter_Lists
2 years ago
Form_Submissions
2 years ago
Interval
2 years ago
Menu_Bar_Stats
2 years ago
Migrations
2 years ago
Models
2 years ago
Public_API
2 years ago
Rows
2 years ago
Statistics
2 years ago
Tables
2 years ago
Utils
2 years ago
Campaign_Builder.php
2 years ago
Capability_Manager.php
2 years ago
Chart.php
2 years ago
Chart_Geo.php
2 years ago
Cron_Manager.php
2 years ago
Current_Traffic_Finder.php
2 years ago
Dashboard_Options.php
2 years ago
Dashboard_Widget.php
2 years ago
Database.php
2 years ago
Database_Manager.php
2 years ago
Empty_Report_Option.php
2 years ago
Env.php
2 years ago
Filters.php
2 years ago
Geo_Database_Background_Job.php
2 years ago
Geo_Database_Manager.php
2 years ago
Geoposition.php
2 years ago
Icon_Directory.php
2 years ago
Icon_Directory_Factory.php
2 years ago
Illuminate_Builder.php
2 years ago
Independent_Analytics.php
2 years ago
Interrupt.php
2 years ago
Known_Referrers.php
2 years ago
Patch.php
2 years ago
Plugin_Conflict_Detector.php
2 years ago
Plugin_Group.php
2 years ago
Plugin_Group_Option.php
2 years ago
Query.php
2 years ago
Query_Taps.php
2 years ago
Quick_Stats.php
2 years ago
REST_API.php
2 years ago
Real_Time.php
2 years ago
Report.php
2 years ago
Report_Finder.php
2 years ago
Report_Options_Parser.php
2 years ago
Resource_Identifier.php
2 years ago
Settings.php
2 years ago
Sort_Configuration.php
2 years ago
Track_Resource_Changes.php
2 years ago
View.php
2 years ago
View_Counter.php
2 years ago
Visitors_Over_Time_Finder.php
2 years ago
WP_Option_Cache_Bust.php
2 years ago
WooCommerce_Order.php
2 years ago
WooCommerce_Referrer_Meta_Box.php
2 years ago
WooCommerce_Order.php
149 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | use IAWP\Models\Visitor; |
| 6 | /** @internal */ |
| 7 | class WooCommerce_Order |
| 8 | { |
| 9 | private $order_id; |
| 10 | private $total; |
| 11 | private $total_refunded; |
| 12 | private $total_refunds; |
| 13 | private $status; |
| 14 | /** |
| 15 | * @param int $order_id WooCommerce order ID |
| 16 | */ |
| 17 | public function __construct(int $order_id) |
| 18 | { |
| 19 | $order = wc_get_order($order_id); |
| 20 | $total = $order->get_total(); |
| 21 | // Total based on order currency, not shop currency |
| 22 | $total_refunded = \floatval($order->get_total_refunded()); |
| 23 | // Refund amount based on order currency, not shop currency |
| 24 | // Aelia Currency Switcher |
| 25 | $aelia_exchange_rate = $order->get_meta('_base_currency_exchange_rate'); |
| 26 | if (\is_numeric($aelia_exchange_rate)) { |
| 27 | // Exchange rate is from order currency to shop currency (multiply) |
| 28 | $total = \round($total * \floatval($aelia_exchange_rate), 2); |
| 29 | $total_refunded = \round($total_refunded * \floatval($aelia_exchange_rate), 2); |
| 30 | } |
| 31 | // WPML |
| 32 | $wpml_exchange_rate = $this->wpml_exchange_rate($order->get_currency()); |
| 33 | if (\is_float($wpml_exchange_rate)) { |
| 34 | // Exchange rate is from shop currency to order currency (divide) |
| 35 | $total = \round($total / $wpml_exchange_rate, 2); |
| 36 | $total_refunded = \round($total_refunded / $wpml_exchange_rate, 2); |
| 37 | } |
| 38 | $this->order_id = $order_id; |
| 39 | $this->total = $total; |
| 40 | $this->total_refunded = $total_refunded; |
| 41 | $this->total_refunds = \count($order->get_refunds()); |
| 42 | $this->status = $order->get_status(); |
| 43 | } |
| 44 | public function insert() : void |
| 45 | { |
| 46 | $visitor = Visitor::fetch_current_visitor(); |
| 47 | if (!$visitor->has_recorded_session()) { |
| 48 | return; |
| 49 | } |
| 50 | $wc_orders_table = \IAWP\Query::get_table_name(\IAWP\Query::WC_ORDERS); |
| 51 | \IAWP\Illuminate_Builder::get_builder()->from($wc_orders_table)->insertOrIgnore(['order_id' => $this->order_id, 'view_id' => $visitor->most_recent_view_id(), 'initial_view_id' => $visitor->most_recent_initial_view_id(), 'total' => $this->total, 'total_refunded' => $this->total_refunded, 'total_refunds' => $this->total_refunds, 'status' => $this->status, 'created_at' => (new \DateTime())->format('Y-m-d H:i:s')]); |
| 52 | } |
| 53 | public function update() : void |
| 54 | { |
| 55 | $wc_orders_table = \IAWP\Query::get_table_name(\IAWP\Query::WC_ORDERS); |
| 56 | \IAWP\Illuminate_Builder::get_builder()->from($wc_orders_table)->where('order_id', '=', $this->order_id)->update(['total' => $this->total, 'total_refunded' => $this->total_refunded, 'total_refunds' => $this->total_refunds, 'status' => $this->status]); |
| 57 | } |
| 58 | private function wpml_exchange_rate(string $currency_code) : ?float |
| 59 | { |
| 60 | $active_plugins = \get_option('active_plugins'); |
| 61 | if (!\in_array('woocommerce-multilingual/wpml-woocommerce.php', $active_plugins)) { |
| 62 | return null; |
| 63 | } |
| 64 | $wcml_options = \get_option('_wcml_settings'); |
| 65 | if (!\is_array($wcml_options)) { |
| 66 | return null; |
| 67 | } |
| 68 | if (!\array_key_exists('currency_options', $wcml_options)) { |
| 69 | return null; |
| 70 | } |
| 71 | if (!\is_array($wcml_options['currency_options']) || !\array_key_exists($currency_code, $wcml_options['currency_options'])) { |
| 72 | return null; |
| 73 | } |
| 74 | if (!\is_array($wcml_options['currency_options'][$currency_code]) || !\array_key_exists('rate', $wcml_options['currency_options'][$currency_code])) { |
| 75 | return null; |
| 76 | } |
| 77 | $exchange_rate = \floatval($wcml_options['currency_options'][$currency_code]['rate']); |
| 78 | // Was there an error parsing value as float? |
| 79 | if ($exchange_rate === 0.0) { |
| 80 | return null; |
| 81 | } |
| 82 | return $exchange_rate; |
| 83 | } |
| 84 | public static function initialize_order_tracker() |
| 85 | { |
| 86 | // Required for block checkout |
| 87 | \add_action('woocommerce_store_api_checkout_order_processed', function ($order) { |
| 88 | try { |
| 89 | $woocommerce_order = new self($order->get_id()); |
| 90 | $woocommerce_order->insert(); |
| 91 | } catch (\Throwable $e) { |
| 92 | \error_log('Independent Analytics was unable to track the analytics for a WooCommerce order. Please report this error to Independent Analytics. The error message is below.'); |
| 93 | \error_log($e->getMessage()); |
| 94 | } |
| 95 | }); |
| 96 | // Required for shortcode checkout |
| 97 | \add_action('woocommerce_checkout_order_created', function ($order) { |
| 98 | try { |
| 99 | $woocommerce_order = new self($order->get_id()); |
| 100 | $woocommerce_order->insert(); |
| 101 | } catch (\Throwable $e) { |
| 102 | \error_log('Independent Analytics was unable to track the analytics for a WooCommerce order. Please report this error to Independent Analytics. The error message is below.'); |
| 103 | \error_log($e->getMessage()); |
| 104 | } |
| 105 | }); |
| 106 | \add_action('woocommerce_order_status_changed', function ($order_id) { |
| 107 | try { |
| 108 | $woocommerce_order = new self($order_id); |
| 109 | $woocommerce_order->update(); |
| 110 | } catch (\Throwable $e) { |
| 111 | \error_log('Independent Analytics was unable to track the analytics for a WooCommerce order. Please report this error to Independent Analytics. The error message is below.'); |
| 112 | \error_log($e->getMessage()); |
| 113 | } |
| 114 | }); |
| 115 | // Captures a partial refund, something that woocommerce_order_status_changed will not do |
| 116 | \add_action('woocommerce_order_refunded', function ($order_id) { |
| 117 | try { |
| 118 | $woocommerce_order = new self($order_id); |
| 119 | $woocommerce_order->update(); |
| 120 | } catch (\Throwable $e) { |
| 121 | \error_log('Independent Analytics was unable to track the analytics for a WooCommerce order. Please report this error to Independent Analytics. The error message is below.'); |
| 122 | \error_log($e->getMessage()); |
| 123 | } |
| 124 | }); |
| 125 | } |
| 126 | public static function tracked_order_statuses() : array |
| 127 | { |
| 128 | return [ |
| 129 | 'wc-completed', |
| 130 | 'completed', |
| 131 | 'wc-processing', |
| 132 | 'processing', |
| 133 | 'wc-refunded', |
| 134 | 'refunded', |
| 135 | 'wc-shipped', |
| 136 | 'shipped', |
| 137 | 'wc-partial-shipped', |
| 138 | 'partial-shipped', |
| 139 | 'wc-delivered', |
| 140 | 'delivered', |
| 141 | // Specific to WooCommerce Amazon Fulfillment plugin |
| 142 | 'wc-sent-to-fba', |
| 143 | 'sent-to-fba', |
| 144 | 'wc-part-to-fba', |
| 145 | 'part-to-fba', |
| 146 | ]; |
| 147 | } |
| 148 | } |
| 149 |