nshelper

WPGlobaCart - wcfm

Jul 29th, 2025
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.49 KB | None | 0 0
  1. <?php
  2.  
  3. class Custom_WooGC_wcfm
  4. {
  5.  
  6. function __construct()
  7. {
  8. add_action('woogc/api/order-sync/order_created', array ( $this, 'schedule_event' ), 10, 2 );
  9. }
  10.  
  11.  
  12. function schedule_event( $order_id, $data )
  13. {
  14. if (!wp_next_scheduled('my_custom_event_with_args')) {
  15. wp_schedule_single_event(time() + 1, 'Custom_WooGC_wcfm_create_order_action', [ $order_id, $data ]);
  16. error_log( 'Scheduled ' );
  17. }
  18. }
  19.  
  20.  
  21.  
  22. }
  23.  
  24. new Custom_WooGC_wcfm();
  25.  
  26.  
  27. add_action('Custom_WooGC_wcfm_create_order_action', 'Custom_WooGC_wcfm_create_order', 10, 2);
  28. function Custom_WooGC_wcfm_create_order( $order_id, $data )
  29. {
  30. error_log( 'Starting ' );
  31. global $wpdb, $blog_id;
  32.  
  33. $current_blog_id = $blog_id;
  34.  
  35. $_woogc_origin_shop = $data['meta_data']['_woogc_origin_shop'];
  36. $_woogc_origin_order_id = $data['meta_data']['_woogc_origin_order_id'];
  37.  
  38.  
  39. switch_to_blog( $_woogc_origin_shop );
  40.  
  41. $origin_table_wcfm_marketplace_orders = $wpdb->prefix . 'wcfm_marketplace_orders';
  42. $origin_table_wcfm_marketplace_orders_meta = $wpdb->prefix . 'wcfm_marketplace_orders_meta';
  43.  
  44. restore_current_blog();
  45.  
  46. $current_table_wcfm_marketplace_orders = $wpdb->prefix . 'wcfm_marketplace_orders';
  47. $current_table_wcfm_marketplace_orders_meta = $wpdb->prefix . 'wcfm_marketplace_orders_meta';
  48.  
  49. $mysql_query = $wpdb->prepare ( "SELECT * FROM " . $origin_table_wcfm_marketplace_orders . " WHERE order_id = %d", $_woogc_origin_order_id );
  50. $origin_wcfm_marketplace_orders_data = $wpdb->get_row ( $mysql_query );
  51.  
  52. if ( ! is_object ( $origin_wcfm_marketplace_orders_data ) )
  53. {
  54. error_log( 'Unable to find the origin data: ' . $wpdb->last_error );
  55. return;
  56. }
  57.  
  58.  
  59. // 1) Cast to array:
  60. $row = (array) $origin_wcfm_marketplace_orders_data;
  61.  
  62. // 2) Remove the primary key field (assuming it’s 'id'):
  63. $origin_row_id = 0;
  64. if ( isset( $row['ID'] ) ) {
  65. $origin_row_id = $row['ID'];
  66. unset( $row['ID'] );
  67. }
  68.  
  69. // 3) Insert into the current blog’s wcfm_marketplace_orders:
  70. // Note: $current_table_wcfm_marketplace_orders already includes the right prefix
  71. $inserted = $wpdb->insert(
  72. $current_table_wcfm_marketplace_orders,
  73. $row,
  74. // 4) And a quick way to build the formats array (all strings or numbers):
  75. // you can customize these if you know certain columns are ints ('%d') vs strings ('%s')
  76. array_fill( 0, count( $row ), '%s' )
  77. );
  78.  
  79. if ( false === $inserted )
  80. {
  81. // error
  82. error_log( 'WPDB Insert Error: ' . $wpdb->last_error );
  83. return;
  84. }
  85.  
  86. // success! new ID is:
  87. $current_record_id = $wpdb->insert_id;
  88. error_log( $order_id );
  89.  
  90. $data_to_update = array(
  91. 'order_id' => $order_id, // Replace with the column and value you want to update
  92. // add more columns as needed
  93. );
  94.  
  95. $where_condition = array(
  96. 'order_id' => $row['order_id'], // The order_id value to match the row to update
  97. );
  98.  
  99. $updated = $wpdb->update( $current_table_wcfm_marketplace_orders, $data_to_update, $where_condition );
  100.  
  101.  
  102.  
  103. //update the product and variation
  104. switch_to_blog( $_woogc_origin_shop );
  105.  
  106. $pi_woogc_ps_is_child_of_pid = get_post_meta( $row['product_id'], '_woogc_ps_is_child_of_pid', TRUE );
  107. $_woogc_ps_is_child_of_bid = get_post_meta( $row['product_id'], '_woogc_ps_is_child_of_bid', TRUE );
  108.  
  109. error_log( '$_woogc_ps_is_child_of_pid ' . $pi_woogc_ps_is_child_of_pid );
  110. error_log( '$_woogc_ps_is_child_of_bid ' . $_woogc_ps_is_child_of_bid );
  111.  
  112. restore_current_blog();
  113.  
  114. if ( ! empty ($pi_woogc_ps_is_child_of_pid ) && $_woogc_ps_is_child_of_bid == $current_blog_id )
  115. {
  116. $data_to_update = array(
  117. 'product_id' => $pi_woogc_ps_is_child_of_pid, // Replace with the column and value you want to update
  118. // add more columns as needed
  119. );
  120.  
  121. $where_condition = array(
  122. 'order_id' => $order_id, // The order_id value to match the row to update
  123. );
  124.  
  125. $updated = $wpdb->update( $current_table_wcfm_marketplace_orders, $data_to_update, $where_condition );
  126.  
  127. error_log( 'Updating product_id' );
  128. }
  129.  
  130. switch_to_blog( $_woogc_origin_shop );
  131.  
  132. $vi_woogc_ps_is_child_of_pid = get_post_meta( $row['variation_id'], '_woogc_ps_is_child_of_pid', TRUE );
  133. $_woogc_ps_is_child_of_bid = get_post_meta( $row['variation_id'], '_woogc_ps_is_child_of_bid', TRUE );
  134.  
  135. restore_current_blog();
  136.  
  137. if ( ! empty ($vi_woogc_ps_is_child_of_pid ) && $_woogc_ps_is_child_of_bid == $current_blog_id )
  138. {
  139. $data_to_update = array(
  140. 'variation_id' => $vi_woogc_ps_is_child_of_pid, // Replace with the column and value you want to update
  141. // add more columns as needed
  142. );
  143.  
  144. $where_condition = array(
  145. 'order_id' => $order_id, // The order_id value to match the row to update
  146. );
  147.  
  148. $updated = $wpdb->update( $current_table_wcfm_marketplace_orders, $data_to_update, $where_condition );
  149. }
  150.  
  151.  
  152.  
  153. //update the woocommerce meta data
  154. // table names (adjust if your WP tables use a different prefix)
  155. $items_table = $wpdb->prefix . 'woocommerce_order_items';
  156. $meta_table = $wpdb->prefix . 'woocommerce_order_itemmeta';
  157.  
  158. // 1) fetch all line‐item IDs for this order
  159. $order_item_ids = $wpdb->get_col(
  160. $wpdb->prepare(
  161. "
  162. SELECT order_item_id
  163. FROM {$items_table}
  164. WHERE order_id = %d
  165. AND order_item_type = 'line_item'
  166. ",
  167. $order_id
  168. )
  169. );
  170.  
  171. error_log( '$order_item_ids ' . $order_item_ids );
  172.  
  173. // 2) loop through each item and update its meta
  174. foreach ( $order_item_ids as $item_id ) {
  175. // update _product_id
  176.  
  177. if ( ! empty ( $pi_woogc_ps_is_child_of_pid ) )
  178. {
  179. $updated1 = $wpdb->update(
  180. $meta_table,
  181. [ 'meta_value' => $pi_woogc_ps_is_child_of_pid ],
  182. [
  183. 'order_item_id' => $item_id,
  184. 'meta_key' => '_product_id',
  185. ],
  186. [ '%d' ], // format for new value
  187. [ '%d', '%s' ] // formats for where clause values
  188. );
  189. }
  190.  
  191. if ( ! empty ( $vi_woogc_ps_is_child_of_pid ) )
  192. {
  193. // update _variation_id
  194. $updated2 = $wpdb->update(
  195. $meta_table,
  196. [ 'meta_value' => $vi_woogc_ps_is_child_of_pid ],
  197. [
  198. 'order_item_id' => $item_id,
  199. 'meta_key' => '_variation_id',
  200. ],
  201. [ '%d' ],
  202. [ '%d', '%s' ]
  203. );
  204. }
  205.  
  206. }
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213. $origin_meta_rows = $wpdb->get_results(
  214. $wpdb->prepare(
  215. "SELECT * FROM {$origin_table_wcfm_marketplace_orders_meta}
  216. WHERE order_commission_id = %d",
  217. $origin_row_id
  218. ),
  219. ARRAY_A
  220. );
  221.  
  222. foreach ( $origin_meta_rows as $meta_row )
  223. {
  224. unset ( $meta_row['ID'] );
  225.  
  226. // swap in the new commission ID
  227. $meta_row['order_commission_id'] = $current_record_id;
  228.  
  229. // build a format array: %d for commission ID, %s for everything else
  230. $formats = array_map( function( $key ) {
  231. return $key === 'order_commission_id' ? '%d' : '%s';
  232. }, array_keys( $meta_row ) );
  233.  
  234. // do the insert
  235. $inserted = $wpdb->insert(
  236. $current_table_wcfm_marketplace_orders_meta,
  237. $meta_row,
  238. $formats
  239. );
  240.  
  241. if ( false === $inserted ) {
  242. error_log( 'WPDB Meta Insert Error: ' . $wpdb->last_error );
  243. return;
  244. }
  245. }
  246.  
  247.  
  248. return;
  249.  
  250. }
Advertisement
Add Comment
Please, Sign In to add comment