PluginProbe
Independent Analytics – WordPress Analytics Plugin / 1.28.3
Independent Analytics – WordPress Analytics Plugin v1.28.3
2.15.5 2.15.4 2.15.3 2.15.2 2.15.1 2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 All 120 releases
independent-analytics / IAWP / WooCommerce_Order.php
WooCommerce_Order.php
78 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IAWP_SCOPED\IAWP;
4
5 use IAWP_SCOPED\IAWP\Models\Visitor;
6 use IAWP_SCOPED\IAWP\Utils\Request;
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 $this->order_id = $order_id;
21 $this->total = \floatval($order->get_total());
22 $this->total_refunded = \floatval($order->get_total_refunded());
23 $this->total_refunds = \count($order->get_refunds());
24 $this->status = $order->get_status();
25 }
26 /**
27 * Insert or update a row in wp_independent_analytics_wc_orders
28 *
29 * @return void
30 */
31 public function upsert() : void
32 {
33 global $wpdb;
34 $wc_orders_table = Query::get_table_name(Query::WC_ORDERS);
35 $existing_wc_order = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wc_orders_table} WHERE order_id = %d", $this->order_id));
36 if (!\is_null($existing_wc_order)) {
37 $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]);
38 return;
39 }
40 $most_recent_view_id = $this->most_recent_view_id();
41 if (\is_null($most_recent_view_id)) {
42 return;
43 }
44 $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')]);
45 }
46 public static function initialize_order_tracker()
47 {
48 \add_action('woocommerce_checkout_order_created', function ($order) {
49 try {
50 $woocommerce_order = new self($order->get_id());
51 $woocommerce_order->upsert();
52 } catch (\Throwable $e) {
53 \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.');
54 \error_log($e->getMessage());
55 }
56 });
57 \add_action('woocommerce_order_status_changed', function ($order_id) {
58 try {
59 $woocommerce_order = new self($order_id);
60 $woocommerce_order->upsert();
61 } catch (\Throwable $e) {
62 \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.');
63 \error_log($e->getMessage());
64 }
65 });
66 }
67 /**
68 * Get the ID of the most recent view so the order can be associated with that view
69 *
70 * @return int|null ID of the most recent view
71 */
72 private function most_recent_view_id() : ?int
73 {
74 $visitor = new Visitor(Request::ip(), Request::user_agent());
75 return $visitor->most_recent_view_id();
76 }
77 }
78