Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Custom_WooGC_wcfm
- {
- function __construct()
- {
- add_action('woogc/api/order-sync/order_created', array ( $this, 'schedule_event' ), 10, 2 );
- }
- function schedule_event( $order_id, $data )
- {
- if (!wp_next_scheduled('my_custom_event_with_args')) {
- wp_schedule_single_event(time() + 1, 'Custom_WooGC_wcfm_create_order_action', [ $order_id, $data ]);
- error_log( 'Scheduled ' );
- }
- }
- }
- new Custom_WooGC_wcfm();
- add_action('Custom_WooGC_wcfm_create_order_action', 'Custom_WooGC_wcfm_create_order', 10, 2);
- function Custom_WooGC_wcfm_create_order( $order_id, $data )
- {
- error_log( 'Starting ' );
- global $wpdb, $blog_id;
- $current_blog_id = $blog_id;
- $_woogc_origin_shop = $data['meta_data']['_woogc_origin_shop'];
- $_woogc_origin_order_id = $data['meta_data']['_woogc_origin_order_id'];
- switch_to_blog( $_woogc_origin_shop );
- $origin_table_wcfm_marketplace_orders = $wpdb->prefix . 'wcfm_marketplace_orders';
- $origin_table_wcfm_marketplace_orders_meta = $wpdb->prefix . 'wcfm_marketplace_orders_meta';
- restore_current_blog();
- $current_table_wcfm_marketplace_orders = $wpdb->prefix . 'wcfm_marketplace_orders';
- $current_table_wcfm_marketplace_orders_meta = $wpdb->prefix . 'wcfm_marketplace_orders_meta';
- $mysql_query = $wpdb->prepare ( "SELECT * FROM " . $origin_table_wcfm_marketplace_orders . " WHERE order_id = %d", $_woogc_origin_order_id );
- $origin_wcfm_marketplace_orders_data = $wpdb->get_row ( $mysql_query );
- if ( ! is_object ( $origin_wcfm_marketplace_orders_data ) )
- {
- error_log( 'Unable to find the origin data: ' . $wpdb->last_error );
- return;
- }
- // 1) Cast to array:
- $row = (array) $origin_wcfm_marketplace_orders_data;
- // 2) Remove the primary key field (assuming it’s 'id'):
- $origin_row_id = 0;
- if ( isset( $row['ID'] ) ) {
- $origin_row_id = $row['ID'];
- unset( $row['ID'] );
- }
- // 3) Insert into the current blog’s wcfm_marketplace_orders:
- // Note: $current_table_wcfm_marketplace_orders already includes the right prefix
- $inserted = $wpdb->insert(
- $current_table_wcfm_marketplace_orders,
- $row,
- // 4) And a quick way to build the formats array (all strings or numbers):
- // you can customize these if you know certain columns are ints ('%d') vs strings ('%s')
- array_fill( 0, count( $row ), '%s' )
- );
- if ( false === $inserted )
- {
- // error
- error_log( 'WPDB Insert Error: ' . $wpdb->last_error );
- return;
- }
- // success! new ID is:
- $current_record_id = $wpdb->insert_id;
- error_log( $order_id );
- $data_to_update = array(
- 'order_id' => $order_id, // Replace with the column and value you want to update
- // add more columns as needed
- );
- $where_condition = array(
- 'order_id' => $row['order_id'], // The order_id value to match the row to update
- );
- $updated = $wpdb->update( $current_table_wcfm_marketplace_orders, $data_to_update, $where_condition );
- //update the product and variation
- switch_to_blog( $_woogc_origin_shop );
- $pi_woogc_ps_is_child_of_pid = get_post_meta( $row['product_id'], '_woogc_ps_is_child_of_pid', TRUE );
- $_woogc_ps_is_child_of_bid = get_post_meta( $row['product_id'], '_woogc_ps_is_child_of_bid', TRUE );
- error_log( '$_woogc_ps_is_child_of_pid ' . $pi_woogc_ps_is_child_of_pid );
- error_log( '$_woogc_ps_is_child_of_bid ' . $_woogc_ps_is_child_of_bid );
- restore_current_blog();
- if ( ! empty ($pi_woogc_ps_is_child_of_pid ) && $_woogc_ps_is_child_of_bid == $current_blog_id )
- {
- $data_to_update = array(
- 'product_id' => $pi_woogc_ps_is_child_of_pid, // Replace with the column and value you want to update
- // add more columns as needed
- );
- $where_condition = array(
- 'order_id' => $order_id, // The order_id value to match the row to update
- );
- $updated = $wpdb->update( $current_table_wcfm_marketplace_orders, $data_to_update, $where_condition );
- error_log( 'Updating product_id' );
- }
- switch_to_blog( $_woogc_origin_shop );
- $vi_woogc_ps_is_child_of_pid = get_post_meta( $row['variation_id'], '_woogc_ps_is_child_of_pid', TRUE );
- $_woogc_ps_is_child_of_bid = get_post_meta( $row['variation_id'], '_woogc_ps_is_child_of_bid', TRUE );
- restore_current_blog();
- if ( ! empty ($vi_woogc_ps_is_child_of_pid ) && $_woogc_ps_is_child_of_bid == $current_blog_id )
- {
- $data_to_update = array(
- 'variation_id' => $vi_woogc_ps_is_child_of_pid, // Replace with the column and value you want to update
- // add more columns as needed
- );
- $where_condition = array(
- 'order_id' => $order_id, // The order_id value to match the row to update
- );
- $updated = $wpdb->update( $current_table_wcfm_marketplace_orders, $data_to_update, $where_condition );
- }
- //update the woocommerce meta data
- // table names (adjust if your WP tables use a different prefix)
- $items_table = $wpdb->prefix . 'woocommerce_order_items';
- $meta_table = $wpdb->prefix . 'woocommerce_order_itemmeta';
- // 1) fetch all line‐item IDs for this order
- $order_item_ids = $wpdb->get_col(
- $wpdb->prepare(
- "
- SELECT order_item_id
- FROM {$items_table}
- WHERE order_id = %d
- AND order_item_type = 'line_item'
- ",
- $order_id
- )
- );
- error_log( '$order_item_ids ' . $order_item_ids );
- // 2) loop through each item and update its meta
- foreach ( $order_item_ids as $item_id ) {
- // update _product_id
- if ( ! empty ( $pi_woogc_ps_is_child_of_pid ) )
- {
- $updated1 = $wpdb->update(
- $meta_table,
- [ 'meta_value' => $pi_woogc_ps_is_child_of_pid ],
- [
- 'order_item_id' => $item_id,
- 'meta_key' => '_product_id',
- ],
- [ '%d' ], // format for new value
- [ '%d', '%s' ] // formats for where clause values
- );
- }
- if ( ! empty ( $vi_woogc_ps_is_child_of_pid ) )
- {
- // update _variation_id
- $updated2 = $wpdb->update(
- $meta_table,
- [ 'meta_value' => $vi_woogc_ps_is_child_of_pid ],
- [
- 'order_item_id' => $item_id,
- 'meta_key' => '_variation_id',
- ],
- [ '%d' ],
- [ '%d', '%s' ]
- );
- }
- }
- $origin_meta_rows = $wpdb->get_results(
- $wpdb->prepare(
- "SELECT * FROM {$origin_table_wcfm_marketplace_orders_meta}
- WHERE order_commission_id = %d",
- $origin_row_id
- ),
- ARRAY_A
- );
- foreach ( $origin_meta_rows as $meta_row )
- {
- unset ( $meta_row['ID'] );
- // swap in the new commission ID
- $meta_row['order_commission_id'] = $current_record_id;
- // build a format array: %d for commission ID, %s for everything else
- $formats = array_map( function( $key ) {
- return $key === 'order_commission_id' ? '%d' : '%s';
- }, array_keys( $meta_row ) );
- // do the insert
- $inserted = $wpdb->insert(
- $current_table_wcfm_marketplace_orders_meta,
- $meta_row,
- $formats
- );
- if ( false === $inserted ) {
- error_log( 'WPDB Meta Insert Error: ' . $wpdb->last_error );
- return;
- }
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment