AJAX
2 years ago
Admin_Page
2 years ago
Custom_WordPress_Columns
2 years ago
Date_Range
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
Email_Chart.php
2 years ago
Email_Reports.php
2 years ago
Empty_Report_Option.php
2 years ago
Env.php
2 years ago
Filters.php
2 years ago
Form.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
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
Quick_Stat.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
108 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 = \floatval($order->get_total()); |
| 21 | $total_refunded = \floatval($order->get_total_refunded()); |
| 22 | $base_currency_exchange_rate = $order->get_meta('_base_currency_exchange_rate'); |
| 23 | // Only convert using the exchange rate if one is found (Aelia Currency Switcher) |
| 24 | if (\is_numeric($base_currency_exchange_rate)) { |
| 25 | $total = \round($total * \floatval($base_currency_exchange_rate), 2); |
| 26 | $total_refunded = \round($total_refunded * \floatval($base_currency_exchange_rate), 2); |
| 27 | } |
| 28 | $this->order_id = $order_id; |
| 29 | $this->total = $total; |
| 30 | $this->total_refunded = $total_refunded; |
| 31 | $this->total_refunds = \count($order->get_refunds()); |
| 32 | $this->status = $order->get_status(); |
| 33 | } |
| 34 | public function insert() : void |
| 35 | { |
| 36 | global $wpdb; |
| 37 | $wc_orders_table = \IAWP\Query::get_table_name(\IAWP\Query::WC_ORDERS); |
| 38 | $visitor = Visitor::fetch_current_visitor(); |
| 39 | if (!$visitor->has_recorded_session()) { |
| 40 | return; |
| 41 | } |
| 42 | $wpdb->insert($wc_orders_table, ['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')]); |
| 43 | } |
| 44 | public function update() : void |
| 45 | { |
| 46 | global $wpdb; |
| 47 | $wc_orders_table = \IAWP\Query::get_table_name(\IAWP\Query::WC_ORDERS); |
| 48 | $existing_wc_order = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wc_orders_table} WHERE order_id = %d", $this->order_id)); |
| 49 | if (\is_null($existing_wc_order)) { |
| 50 | return; |
| 51 | } |
| 52 | $wpdb->update($wc_orders_table, ['total' => $this->total, 'total_refunded' => $this->total_refunded, 'total_refunds' => $this->total_refunds, 'status' => $this->status], ['order_id' => $this->order_id]); |
| 53 | } |
| 54 | public static function initialize_order_tracker() |
| 55 | { |
| 56 | \add_action('woocommerce_checkout_order_created', function ($order) { |
| 57 | try { |
| 58 | $woocommerce_order = new self($order->get_id()); |
| 59 | $woocommerce_order->insert(); |
| 60 | } catch (\Throwable $e) { |
| 61 | \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.'); |
| 62 | \error_log($e->getMessage()); |
| 63 | } |
| 64 | }); |
| 65 | \add_action('woocommerce_order_status_changed', function ($order_id) { |
| 66 | try { |
| 67 | $woocommerce_order = new self($order_id); |
| 68 | $woocommerce_order->update(); |
| 69 | } catch (\Throwable $e) { |
| 70 | \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.'); |
| 71 | \error_log($e->getMessage()); |
| 72 | } |
| 73 | }); |
| 74 | // Captures a partial refund, something that woocommerce_order_status_changed will not do |
| 75 | \add_action('woocommerce_order_refunded', function ($order_id) { |
| 76 | try { |
| 77 | $woocommerce_order = new self($order_id); |
| 78 | $woocommerce_order->update(); |
| 79 | } catch (\Throwable $e) { |
| 80 | \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.'); |
| 81 | \error_log($e->getMessage()); |
| 82 | } |
| 83 | }); |
| 84 | } |
| 85 | public static function tracked_order_statuses() : array |
| 86 | { |
| 87 | return [ |
| 88 | 'wc-completed', |
| 89 | 'completed', |
| 90 | 'wc-processing', |
| 91 | 'processing', |
| 92 | 'wc-refunded', |
| 93 | 'refunded', |
| 94 | 'wc-shipped', |
| 95 | 'shipped', |
| 96 | 'wc-partial-shipped', |
| 97 | 'partial-shipped', |
| 98 | 'wc-delivered', |
| 99 | 'delivered', |
| 100 | // Specific to WooCommerce Amazon Fulfillment plugin |
| 101 | 'wc-sent-to-fba', |
| 102 | 'sent-to-fba', |
| 103 | 'wc-part-to-fba', |
| 104 | 'part-to-fba', |
| 105 | ]; |
| 106 | } |
| 107 | } |
| 108 |