PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 3.10.8
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v3.10.8
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / includes / core / Reporting / StatHookHandler.php

StatHookHandler.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 3.10.8, at includes/core/Reporting/StatHookHandler.php

278 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPFunnels\Report;
4
5 use Automattic\WooCommerce\Utilities\OrderUtil;
6 use FKWCS\Gateway\Stripe\P24;
7 use WPFunnels\Wpfnl_functions;
8
9 class StatHookHandler
10 {
11
12 public function __construct()
13 {
14 add_action('wpfunnels/funnel_order_placed', array($this, 'update_stat_data_from_order'), 10, 3);
15 add_action('woocommerce_order_status_changed', array($this, 'change_order_status'), 10, 4);
16
17 if (class_exists('\Automattic\WooCommerce\Utilities\OrderUtil') && OrderUtil::custom_orders_table_usage_is_enabled()) {
18 add_action('woocommerce_delete_order', array($this, 'delete_stat_data'));
19 } else {
20 add_action('delete_post', array($this, 'delete_stat_data'));
21 }
22
23 add_action('woocommerce_order_fully_refunded', array($this, 'update_stat_table_for_full_refund'), 10, 1);
24 add_action('woocommerce_order_partially_refunded', array($this, 'update_stat_table_for_partial_refund'), 10, 2);
25 }
26
27
28 /**
29 * Update reporting data from order
30 *
31 * @param $order_id
32 * @param $funnel_id
33 * @param $checkout_id
34 */
35 public function update_stat_data_from_order($order_id, $funnel_id, $checkout_id)
36 {
37 global $wpdb;
38
39 $table = $wpdb->prefix . 'wpfnl_stats';
40 $order = wc_get_order($order_id);
41
42 if (!$order) {
43 return;
44 }
45
46 $status = $order->get_status();
47 $total = $order->get_total();
48 $customer_id = $order->get_customer_id();
49 $ob_revenue = $this->get_order_bump_revenue($order);
50 $current_date_time = current_time('mysql');
51 $current_date_time_gmt = current_time('mysql', 1);
52
53 $wpdb->insert(
54 $table,
55 array(
56 'order_id' => $order_id,
57 'funnel_id' => $funnel_id,
58 'customer_id' => $customer_id,
59 'total_sales' => $total,
60 'orderbump_sales' => $ob_revenue,
61 'status' => $status,
62 'date_created' => $current_date_time,
63 'date_created_gmt' => $current_date_time_gmt
64 )
65 );
66 }
67
68
69 /**
70 * Get order bump revenue from order
71 *
72 * @param $order
73 * @return int
74 *
75 * @since 3.1.7
76 */
77 public function get_order_bump_revenue($order)
78 {
79
80 $ob_products = $order->get_meta('_wpfunnels_order_bump_products');
81
82 if (empty($ob_products)) {
83 return 0;
84 }
85
86 $total = 0;
87
88 foreach ($order->get_items() as $item) {
89 $product_id = $item->get_product_id();
90
91 // If the product is a variation, get its variation ID
92 if ($item->get_variation_id()) {
93 $product_id = $item->get_variation_id();
94 }
95
96 if (in_array($product_id, $ob_products)) {
97 $total += $item->get_total();
98 }
99 }
100 return round($total, 2);
101 }
102
103
104 /**
105 * Update the order status if status changed.
106 *
107 * @param $order_id
108 * @param $old_status
109 * @param $new_status
110 * @param $order
111 *
112 * @since 3.2.0
113 */
114 public function change_order_status($order_id, $old_status, $new_status, $order)
115 {
116 global $wpdb;
117 $table = $wpdb->prefix . 'wpfnl_stats';
118
119 $offer_settings = Wpfnl_functions::get_offer_settings();
120 if ($offer_settings['offer_orders'] == 'main-order') {
121 $order_id = $order->get_id();
122 } else {
123 $parent_id = $order->get_meta('_wpfunnels_offer_parent_id');
124 $order_id = !empty($parent_id) ? $parent_id : $order->get_id();
125 }
126
127 if ('completed' === $new_status) {
128 $paid_date_time = current_time('mysql');
129 $wpdb->update(
130 $table,
131 array(
132 'status' => $new_status,
133 'paid_date' => $paid_date_time,
134 ),
135 array(
136 'order_id' => $order_id
137 )
138 );
139 } else {
140 $wpdb->update(
141 $table,
142 array(
143 'status' => $new_status,
144 ),
145 array(
146 'order_id' => $order_id
147 )
148 );
149 }
150
151 }
152
153
154 /**
155 * Delete stat if order is deleted
156 *
157 * @param $order_id
158 *
159 * @since 3.2.0
160 */
161 public function delete_stat_data($order_id)
162 {
163 if (empty($order_id) || absint(0 === $order_id)) {
164 return;
165 }
166
167 global $wpdb;
168 $table = $wpdb->prefix . 'wpfnl_stats';
169
170 if (0 < did_action('delete_post')) {
171 $get_post_type = get_post_type($order_id);
172 if ('shop_order' !== $get_post_type) {
173 return;
174 }
175 }
176 $wpdb->delete($table, ['order_id' => $order_id], ['%d']);
177 }
178
179
180 /**
181 * Update the stat table if fully refund is initiated
182 *
183 * @param $order_id
184 * @since 3.2.0
185 */
186 public function update_stat_table_for_full_refund($order_id)
187 {
188 global $wpdb;
189 $table = $wpdb->prefix . 'wpfnl_stats';
190 $wpdb->update(
191 $table,
192 array(
193 'total_sales' => 0,
194 'orderbump_sales' => 0,
195 'upsell_sales' => 0,
196 'downsell_sales' => 0,
197 ),
198 array(
199 'order_id' => $order_id
200 )
201 );
202 }
203
204
205
206 /**
207 * Update the stat table if partial refund is initiated
208 *
209 * @param $order_id
210 * @param $refund_id
211 *
212 * @since 3.2.0
213 */
214 public function update_stat_table_for_partial_refund($order_id, $refund_id)
215 {
216 global $wpdb;
217 $table = $wpdb->prefix . 'wpfnl_stats';
218 $order = wc_get_order($order_id);
219 $refund = wc_get_order($refund_id);
220 $refund_amount = 0;
221
222 if (!$order instanceof \WC_Order) {
223 return;
224 }
225
226 if (!$refund instanceof \WC_Order_Refund) {
227 return;
228 }
229
230 if (!Wpfnl_functions::check_if_funnel_order($order)) {
231 return;
232 }
233
234 $types = array(
235 'line_item',
236 'tax',
237 'shipping',
238 'fee',
239 'coupon',
240 );
241 if (0 < count($refund->get_items($types))) {
242 foreach ($refund->get_items($types) as $refund_item) {
243 $item_id = $refund_item->get_meta('_refunded_item_id', true);
244 if (empty($item_id)) {
245 continue;
246 }
247 $item = $order->get_item($item_id);
248 if (!$item instanceof \WC_Order_Item) {
249 continue;
250 }
251
252 $is_orderbump = $item->get_meta('_wpfunnels_order_bump');
253 $is_upsell = $item->get_meta('_wpfunnels_upsell');
254 $is_downsell = $item->get_meta('_wpfunnels_downsell');
255
256 if (('' === $is_orderbump) && ('' === $is_upsell) && ('' === $is_downsell)) {
257 $refund_amount += abs($refund_item->get_total());
258 }
259 }
260
261 if ($refund_amount > 0) {
262 $total = $wpdb->get_var("SELECT total_sales FROM " . $table . " WHERE order_id = " . $order_id);
263 $refund_amount = ($total <= $refund_amount) ? 0 : $total - $refund_amount;
264 $wpdb->update(
265 $table,
266 array(
267 'total_sales' => $refund_amount
268 ),
269 array(
270 'order_id' => $order_id
271 )
272 );
273 }
274 }
275 }
276
277 }
278