AJAX
3 years ago
Migrations
3 years ago
Models
3 years ago
Queries
3 years ago
Tables
3 years ago
Utils
3 years ago
Campaign_Builder.php
3 years ago
Capability_Manager.php
3 years ago
Chart.php
3 years ago
Chart_Geo.php
3 years ago
Chart_SVG.php
3 years ago
Current_Resource.php
3 years ago
Dashboard_Options.php
3 years ago
Dashboard_Widget.php
3 years ago
Email_Reports.php
3 years ago
Filters.php
3 years ago
Geo_Database.php
3 years ago
Geo_Database_Download_Job.php
3 years ago
Health_Check.php
3 years ago
Independent_Analytics.php
3 years ago
Known_Referrers.php
3 years ago
PDF.php
3 years ago
Query.php
3 years ago
Quick_Stats.php
3 years ago
REST_API.php
3 years ago
Real_Time.php
3 years ago
Settings.php
3 years ago
Track_Resource_Changes.php
3 years ago
View_Counter.php
3 years ago
WP_Option_Cache_Bust.php
3 years ago
WooCommerce_Order.php
3 years ago
WooCommerce_Order.php
51 lines
| 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 | public function __construct(int $order_id) |
| 11 | { |
| 12 | $this->order_id = $order_id; |
| 13 | } |
| 14 | public function insert() : void |
| 15 | { |
| 16 | global $wpdb; |
| 17 | $wc_orders_table = Query::get_table_name(Query::WC_ORDERS); |
| 18 | $order_id = $this->order_id(); |
| 19 | $view_id = $this->view_id(); |
| 20 | if (\is_null($view_id)) { |
| 21 | return; |
| 22 | } |
| 23 | $existing_wc_order = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wc_orders_table} WHERE order_id = %d", $order_id)); |
| 24 | if (!\is_null($existing_wc_order)) { |
| 25 | return; |
| 26 | } |
| 27 | $wpdb->insert($wc_orders_table, ['view_id' => $view_id, 'order_id' => $order_id, 'created_at' => (new \DateTime())->format('Y-m-d H:i:s')]); |
| 28 | } |
| 29 | private function view_id() : ?int |
| 30 | { |
| 31 | $visitor = new Visitor(Request::ip(), Request::user_agent()); |
| 32 | return $visitor->most_recent_view_id(); |
| 33 | } |
| 34 | private function order_id() : int |
| 35 | { |
| 36 | return $this->order_id; |
| 37 | } |
| 38 | public static function initialize_order_tracker() |
| 39 | { |
| 40 | \add_action('woocommerce_checkout_order_created', function ($order) { |
| 41 | try { |
| 42 | $woocommerce_order = new self($order->id); |
| 43 | $woocommerce_order->insert(); |
| 44 | } catch (\Throwable $e) { |
| 45 | \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.'); |
| 46 | \error_log($e->getMessage()); |
| 47 | } |
| 48 | }); |
| 49 | } |
| 50 | } |
| 51 |