AJAX
2 years ago
Admin_Page
2 years ago
Date_Range
2 years ago
Filter_Lists
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
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
Query.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
87 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | use IAWP\Models\Visitor; |
| 6 | use IAWP\Utils\Request; |
| 7 | /** @internal */ |
| 8 | class WooCommerce_Order |
| 9 | { |
| 10 | private $order_id; |
| 11 | private $total; |
| 12 | private $total_refunded; |
| 13 | private $total_refunds; |
| 14 | private $status; |
| 15 | /** |
| 16 | * @param int $order_id WooCommerce order ID |
| 17 | */ |
| 18 | public function __construct(int $order_id) |
| 19 | { |
| 20 | $order = wc_get_order($order_id); |
| 21 | $total = \floatval($order->get_total()); |
| 22 | $total_refunded = \floatval($order->get_total_refunded()); |
| 23 | $base_currency_exchange_rate = $order->get_meta('_base_currency_exchange_rate'); |
| 24 | // Only convert using the exchange rate if one is found (Aelia Currency Switcher) |
| 25 | if (\is_numeric($base_currency_exchange_rate)) { |
| 26 | $total = \round($total * \floatval($base_currency_exchange_rate), 2); |
| 27 | $total_refunded = \round($total_refunded * \floatval($base_currency_exchange_rate), 2); |
| 28 | } |
| 29 | $this->order_id = $order_id; |
| 30 | $this->total = $total; |
| 31 | $this->total_refunded = $total_refunded; |
| 32 | $this->total_refunds = \count($order->get_refunds()); |
| 33 | $this->status = $order->get_status(); |
| 34 | } |
| 35 | /** |
| 36 | * Insert or update a row in wp_independent_analytics_wc_orders |
| 37 | * |
| 38 | * @return void |
| 39 | */ |
| 40 | public function upsert() : void |
| 41 | { |
| 42 | global $wpdb; |
| 43 | $wc_orders_table = \IAWP\Query::get_table_name(\IAWP\Query::WC_ORDERS); |
| 44 | $existing_wc_order = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wc_orders_table} WHERE order_id = %d", $this->order_id)); |
| 45 | if (!\is_null($existing_wc_order)) { |
| 46 | $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]); |
| 47 | return; |
| 48 | } |
| 49 | $most_recent_view_id = $this->most_recent_view_id(); |
| 50 | if (\is_null($most_recent_view_id)) { |
| 51 | return; |
| 52 | } |
| 53 | $wpdb->insert($wc_orders_table, ['order_id' => $this->order_id, 'view_id' => $most_recent_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')]); |
| 54 | } |
| 55 | /** |
| 56 | * Get the ID of the most recent view so the order can be associated with that view |
| 57 | * |
| 58 | * @return int|null ID of the most recent view |
| 59 | */ |
| 60 | private function most_recent_view_id() : ?int |
| 61 | { |
| 62 | $visitor = new Visitor(Request::ip(), Request::user_agent()); |
| 63 | return $visitor->most_recent_view_id(); |
| 64 | } |
| 65 | public static function initialize_order_tracker() |
| 66 | { |
| 67 | \add_action('woocommerce_checkout_order_created', function ($order) { |
| 68 | try { |
| 69 | $woocommerce_order = new self($order->get_id()); |
| 70 | $woocommerce_order->upsert(); |
| 71 | } catch (\Throwable $e) { |
| 72 | \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.'); |
| 73 | \error_log($e->getMessage()); |
| 74 | } |
| 75 | }); |
| 76 | \add_action('woocommerce_order_status_changed', function ($order_id) { |
| 77 | try { |
| 78 | $woocommerce_order = new self($order_id); |
| 79 | $woocommerce_order->upsert(); |
| 80 | } catch (\Throwable $e) { |
| 81 | \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.'); |
| 82 | \error_log($e->getMessage()); |
| 83 | } |
| 84 | }); |
| 85 | } |
| 86 | } |
| 87 |