SureCart_Cron_Job.php
1 year ago
SureCart_Order.php
1 year ago
SureCart_Store.php
1 year ago
WooCommerce_Order.php
1 year ago
WooCommerce_Referrer_Meta_Box.php
1 year ago
WooCommerce_Status_Manager.php
1 year ago
WooCommerce_Order.php
143 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Ecommerce; |
| 4 | |
| 5 | use IAWP\Illuminate_Builder; |
| 6 | use IAWP\Models\Visitor; |
| 7 | use IAWP\Query; |
| 8 | use IAWP\Utils\Plugin; |
| 9 | /** @internal */ |
| 10 | class WooCommerce_Order |
| 11 | { |
| 12 | private $order_id; |
| 13 | private $status; |
| 14 | private $total; |
| 15 | private $total_refunded; |
| 16 | private $total_refunds; |
| 17 | private $is_discounted; |
| 18 | /** |
| 19 | * @param int $order_id WooCommerce order ID |
| 20 | */ |
| 21 | public function __construct(int $order_id) |
| 22 | { |
| 23 | $order = wc_get_order($order_id); |
| 24 | $total = \intval(\round($order->get_total() * 100)); |
| 25 | // Total based on order currency, not shop currency |
| 26 | $total_refunded = \intval(\round(\floatval($order->get_total_refunded()) * 100)); |
| 27 | // Refund amount based on order currency, not shop currency |
| 28 | // Aelia Currency Switcher |
| 29 | $aelia_exchange_rate = $order->get_meta('_base_currency_exchange_rate'); |
| 30 | if (\is_numeric($aelia_exchange_rate)) { |
| 31 | // Exchange rate is from order currency to shop currency (multiply) |
| 32 | $total = \intval(\round($total * \floatval($aelia_exchange_rate))); |
| 33 | $total_refunded = \intval(\round($total_refunded * \floatval($aelia_exchange_rate))); |
| 34 | } |
| 35 | // WPML |
| 36 | $wpml_exchange_rate = $this->wpml_exchange_rate($order->get_currency()); |
| 37 | if (\is_float($wpml_exchange_rate)) { |
| 38 | // Exchange rate is from shop currency to order currency (divide) |
| 39 | $total = \intval(\round($total / $wpml_exchange_rate)); |
| 40 | $total_refunded = \intval(\round($total_refunded / $wpml_exchange_rate)); |
| 41 | } |
| 42 | $this->order_id = $order_id; |
| 43 | $this->status = $order->get_status(); |
| 44 | $this->total = $total; |
| 45 | $this->total_refunded = $total_refunded; |
| 46 | $this->total_refunds = \count($order->get_refunds()); |
| 47 | $this->is_discounted = $this->is_discounted_order($order); |
| 48 | } |
| 49 | public function insert() : void |
| 50 | { |
| 51 | $visitor = Visitor::fetch_current_visitor(); |
| 52 | if (!$visitor->has_recorded_session()) { |
| 53 | return; |
| 54 | } |
| 55 | $orders_table = Query::get_table_name(Query::ORDERS); |
| 56 | Illuminate_Builder::get_builder()->from($orders_table)->insertOrIgnore(['is_included_in_analytics' => (new \IAWP\Ecommerce\WooCommerce_Status_Manager())->is_tracked_status($this->status), 'woocommerce_order_id' => $this->order_id, 'woocommerce_order_status' => $this->status, '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, 'is_discounted' => $this->is_discounted, 'created_at' => (new \DateTime())->format('Y-m-d H:i:s')]); |
| 57 | } |
| 58 | public function update() : void |
| 59 | { |
| 60 | $orders_table = Query::get_table_name(Query::ORDERS); |
| 61 | Illuminate_Builder::get_builder()->from($orders_table)->where('woocommerce_order_id', '=', $this->order_id)->update(['is_included_in_analytics' => (new \IAWP\Ecommerce\WooCommerce_Status_Manager())->is_tracked_status($this->status), 'woocommerce_order_status' => $this->status, 'total' => $this->total, 'total_refunded' => $this->total_refunded, 'total_refunds' => $this->total_refunds, 'is_discounted' => $this->is_discounted]); |
| 62 | } |
| 63 | private function is_discounted_order($order) : bool |
| 64 | { |
| 65 | if ($order->get_total_discount() > 0) { |
| 66 | return \true; |
| 67 | } |
| 68 | foreach ($order->get_items() as $item) { |
| 69 | if ($item->get_product()->is_on_sale()) { |
| 70 | return \true; |
| 71 | } |
| 72 | } |
| 73 | return \false; |
| 74 | } |
| 75 | private function wpml_exchange_rate(string $currency_code) : ?float |
| 76 | { |
| 77 | if (!\is_plugin_active('woocommerce-multilingual/wpml-woocommerce.php')) { |
| 78 | return null; |
| 79 | } |
| 80 | $wcml_options = \get_option('_wcml_settings'); |
| 81 | if (!\is_array($wcml_options)) { |
| 82 | return null; |
| 83 | } |
| 84 | if (!\array_key_exists('currency_options', $wcml_options)) { |
| 85 | return null; |
| 86 | } |
| 87 | if (!\is_array($wcml_options['currency_options']) || !\array_key_exists($currency_code, $wcml_options['currency_options'])) { |
| 88 | return null; |
| 89 | } |
| 90 | if (!\is_array($wcml_options['currency_options'][$currency_code]) || !\array_key_exists('rate', $wcml_options['currency_options'][$currency_code])) { |
| 91 | return null; |
| 92 | } |
| 93 | $exchange_rate = \floatval($wcml_options['currency_options'][$currency_code]['rate']); |
| 94 | // Was there an error parsing value as float? |
| 95 | if ($exchange_rate === 0.0) { |
| 96 | return null; |
| 97 | } |
| 98 | return $exchange_rate; |
| 99 | } |
| 100 | public static function register_hooks() |
| 101 | { |
| 102 | // Required for block checkout |
| 103 | \add_action('woocommerce_store_api_checkout_order_processed', function ($order) { |
| 104 | try { |
| 105 | $woocommerce_order = new self($order->get_id()); |
| 106 | $woocommerce_order->insert(); |
| 107 | } catch (\Throwable $e) { |
| 108 | \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.'); |
| 109 | \error_log($e->getMessage()); |
| 110 | } |
| 111 | }); |
| 112 | // Required for shortcode checkout |
| 113 | \add_action('woocommerce_checkout_order_created', function ($order) { |
| 114 | try { |
| 115 | $woocommerce_order = new self($order->get_id()); |
| 116 | $woocommerce_order->insert(); |
| 117 | } catch (\Throwable $e) { |
| 118 | \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.'); |
| 119 | \error_log($e->getMessage()); |
| 120 | } |
| 121 | }); |
| 122 | \add_action('woocommerce_order_status_changed', function ($order_id) { |
| 123 | try { |
| 124 | $woocommerce_order = new self($order_id); |
| 125 | $woocommerce_order->update(); |
| 126 | } catch (\Throwable $e) { |
| 127 | \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.'); |
| 128 | \error_log($e->getMessage()); |
| 129 | } |
| 130 | }); |
| 131 | // Captures a partial refund, something that woocommerce_order_status_changed will not do |
| 132 | \add_action('woocommerce_order_refunded', function ($order_id) { |
| 133 | try { |
| 134 | $woocommerce_order = new self($order_id); |
| 135 | $woocommerce_order->update(); |
| 136 | } catch (\Throwable $e) { |
| 137 | \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.'); |
| 138 | \error_log($e->getMessage()); |
| 139 | } |
| 140 | }); |
| 141 | } |
| 142 | } |
| 143 |