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