PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / trunk
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell vtrunk
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 2.8.17 2.8.18 All 259 releases
wpfunnels / admin / Migrations / OrderToStatTableMigration.php

OrderToStatTableMigration.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell trunk, at admin/Migrations/OrderToStatTableMigration.php

242 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPFunnels\Admin\Migrations;
4
5 use Automattic\WooCommerce\Utilities\OrderUtil;
6
7
8 /**
9 * Class OrderToStatTableMigration
10 *
11 * Handles the migration of orders to the wpfnl_stat table.
12 *
13 * @package WPFunnels\Admin\Migrations
14 * @since 3.5.0
15 */
16 class OrderToStatTableMigration extends AbstractMigrations {
17
18 /**
19 * Migration key
20 *
21 * @var string
22 */
23 public $key = 'order_table_migration';
24
25
26 /**
27 * Check if the migration should be enqueued
28 *
29 * Determines if the migration process should be added to the queue.
30 *
31 * @return bool
32 *
33 * @since 3.5.0
34 */
35 public function should_enqueue_migration_instance() {
36 $is_migration_completed = get_option( "wpfunnels_".$this->key."_status", '' );
37 $is_stat_table_created = get_option( 'wpfunnels_wpf_create_350_stat_table_update' );
38
39 if ( 'completed' !== $is_stat_table_created ) {
40 return false;
41 }
42
43 if ( 'completed' === $is_migration_completed ) {
44 return false;
45 }
46 return true;
47 }
48
49
50 /**
51 * Get the next batch to process
52 *
53 * Retrieves the next batch of orders to be processed in the migration.
54 *
55 * @return mixed
56 *
57 * @since 3.5.0
58 */
59 public function get_next_batch_to_process() {
60 if ( !$this->is_on_queue() ) {
61 $batch = get_option( "wpfunnels_".$this->key."_batch", '1' );
62 return $batch;
63 }
64 return false;
65 }
66
67
68 /**
69 * Get SQL query
70 *
71 * Generates the SQL query to fetch orders based on the WooCommerce version.
72 *
73 * @return string
74 *
75 * @since 3.5.0
76 */
77 private function get_sql() {
78 global $wpdb;
79 if ( class_exists( '\Automattic\WooCommerce\Utilities\OrderUtil' ) && function_exists( 'wc_get_container' ) && OrderUtil::custom_orders_table_usage_is_enabled() ) {
80 return "
81 SELECT o.id as id
82 FROM {$wpdb->prefix}wc_orders o
83 INNER JOIN {$wpdb->prefix}wc_orders_meta om ON o.id = om.order_id
84 WHERE om.meta_key = '_wpfunnels_order'
85 AND om.meta_value = 'yes'
86 ORDER BY o.id ASC
87 LIMIT %d OFFSET %d;
88 ";
89 }
90
91 return "SELECT p.ID as id
92 FROM wp_posts p
93 INNER JOIN wp_postmeta pm ON p.ID = pm.post_id
94 WHERE p.post_type = 'shop_order'
95 AND pm.meta_key = '_wpfunnels_order'
96 AND pm.meta_value = 'yes'
97 ORDER BY p.ID ASC
98 LIMIT %d OFFSET %d;";
99 }
100
101
102
103 /**
104 * Check if order exists in order table
105 *
106 * Verifies if a given order already exists in the stats table.
107 *
108 * @param array $order The order data.
109 * @return bool
110 *
111 * @since 3.5.0
112 */
113 public function check_if_order_exists_on_order_table( $order ) {
114 global $wpdb;
115 $table = $wpdb->prefix.'wpfnl_stats';
116 $order_id = $order['order_id'];
117 $sql = "SELECT count(id) from $table WHERE order_id=%d;";
118 $result = $wpdb->get_var($wpdb->prepare($sql, $order_id));
119 return $result > 0;
120 }
121
122
123
124 /**
125 * Get orders
126 *
127 * Retrieves a batch of orders to be processed.
128 *
129 * @param int $limit The number of orders to retrieve.
130 * @param int $offset The offset for the SQL query.
131 * @return array
132 *
133 * @since 3.5.0
134 */
135 public function get_orders( $limit, $offset ) {
136 global $wpdb;
137 $sql = $this->get_sql();
138 $order_ids = $wpdb->get_results($wpdb->prepare( $sql, $limit, $offset ));
139 $orders = array();
140 foreach ( $order_ids as $_order ) {
141
142 if ( !isset( $_order->id ) ) {
143 continue;
144 }
145
146 $order = wc_get_order( $_order->id );
147
148 if ( false === is_a( $order, 'WC_Order' ) ) {
149 continue;
150 }
151
152 $total_sales = $order->get_total();
153 $orderbump_sales = 0;
154 $upsell_sales = 0;
155 $downsell_sales = 0;
156 $ob_products = $order->get_meta('_wpfunnels_order_bump_products');
157
158 foreach ( $order->get_items() as $item ) {
159
160 $is_upsell = $item->get_meta('_wpfunnels_upsell');
161 $is_downsell = $item->get_meta('_wpfunnels_downsell');
162
163 $product_id = $item->get_product_id();
164
165 // If the product is a variation, get its variation ID
166 if ( $item->get_variation_id() ) {
167 $product_id = $item->get_variation_id();
168 }
169
170 if ( $ob_products ) {
171 if ( in_array( $product_id, $ob_products ) ) {
172 $orderbump_sales += $item->get_total();
173 }
174 }
175
176 if ( 'yes' === $is_upsell ) {
177 $upsell_sales += $item->get_total();
178 }
179
180 if ( 'yes' === $is_downsell ) {
181 $downsell_sales += $item->get_total();
182 }
183 }
184
185 $current_date_time = current_time( 'mysql' );
186 $current_date_time_gmt = current_time( 'mysql', 1 );
187 $paid_data = $order->get_date_paid();
188
189 $orders[] = array(
190 'order_id' => $order->get_id(),
191 'funnel_id' => $order->get_meta('_wpfunnels_funnel_id'),
192 'parent_id' => $order->get_parent_id(),
193 'customer_id' => $order->get_customer_id(),
194 'total_sales' => $total_sales,
195 'orderbump_sales' => $orderbump_sales,
196 'upsell_sales' => $upsell_sales,
197 'downsell_sales' => $downsell_sales,
198 'status' => $order->get_status(),
199 'paid_date' => $paid_data ? $paid_data->date('Y-m-d H:i:s') : $current_date_time,
200 'date_created' => $current_date_time,
201 'date_created_gmt' => $current_date_time_gmt,
202 );
203 }
204
205 return $orders;
206 }
207
208
209 /**
210 * Process batch
211 *
212 * Processes a batch of orders and inserts them into the stats table.
213 *
214 * @since 3.5.0
215 */
216 public function process_batch() {
217 global $wpdb;
218 $table = $wpdb->prefix.'wpfnl_stats';
219 $batch = get_option( "wpfunnels_".$this->key."_batch" , '1' );
220 $offset = ( $batch - 1 ) * 50;
221 $this->set_migration_status('running');
222 $orders = $this->get_orders( 50, $offset );
223
224 if ( empty($orders) ) {
225 $this->set_migration_status('completed');
226 return;
227 }
228 foreach ( $orders as $order ) {
229 $is_exists = $this->check_if_order_exists_on_order_table($order);
230 if ($is_exists) {
231 continue;
232 }
233
234 $wpdb->insert(
235 $table,
236 $order
237 );
238 }
239 $this->set_batch($batch+1);
240 }
241 }
242