Untitled
unknown
plain_text
a year ago
9.0 kB
9
Indexable
// Hook into the WooCommerce order status change event add_action('woocommerce_order_status_changed', 'save_order_summary_as_text', 10, 3); /** * Save order summary (booking IDs, game codes, voucher codes) as a readable text field when order status is changed to 'completed' or 'processing'. * * @param int $order_id The ID of the order. * @param string $old_status The old status of the order. * @param string $new_status The new status of the order. */ function save_order_summary_as_text($order_id, $old_status, $new_status) { error_log("Hook triggered: woocommerce_order_status_changed for order ID: $order_id, old status: $old_status, new status: $new_status"); if ($new_status === 'processing' || $new_status === 'completed') { // Get the order object $order = wc_get_order($order_id); if (!$order) { error_log("Order not found for order ID: $order_id"); return; } // Get the user ID associated with the order $user_id = $order->get_user_id(); error_log("User ID for order ID $order_id: $user_id"); // Get the billing email associated with the order $billing_email = $order->get_billing_email(); error_log("Billing email for order ID $order_id: $billing_email"); global $wpdb; $table_name = $wpdb->prefix . 'game_codes'; $order_summary = []; foreach ($order->get_items() as $item_id => $item) { $product_id = $item->get_product_id(); error_log("Processing item ID: $item_id, product ID: $product_id"); // Get Booking ID $item_booking_id = wc_get_order_item_meta($item_id, '_booking_id', true); if (!$item_booking_id) { error_log("No booking ID found for item ID: $item_id"); continue; } // Retrieve the first unused game-code for this product $game_code_row = $wpdb->get_row($wpdb->prepare( "SELECT * FROM $table_name WHERE product_id = %d AND used = 0 LIMIT 1", $product_id )); if ($game_code_row) { $game_code = $game_code_row->code; $current_time = current_time('mysql'); // Mark the code as used and store the order ID, customer email, and order date $updated = $wpdb->update( $table_name, array( 'used' => 1, 'order_id' => $order_id, 'email' => $billing_email, 'order_date' => $current_time ), array('id' => $game_code_row->id) ); if ($updated !== false) { // Add the game code to the order item meta wc_add_order_item_meta($item_id, 'Game Code', $game_code, true); error_log("Game code assigned: $game_code for item ID: $item_id"); } else { error_log("Failed to update game code record for product ID: $product_id, Game Code ID: $game_code_row->id"); } } else { error_log("No unused game code found for product ID: $product_id"); $game_code = ''; } // Generate Voucher Codes $voucher_codes = []; for ($i = 1; $i <= 3; $i++) { $voucher_number_field = "voucher_number_$i"; $voucher_text_field = "voucher_{$i}_text"; $business_prefix_field = "voucher_{$i}_business_prefix"; // Check if the ACF fields are present and business prefix has a value $voucher_text = get_post_meta($product_id, $voucher_text_field, true); $business_prefix = get_post_meta($product_id, $business_prefix_field, true); error_log("Voucher Text for Voucher $i: $voucher_text, Business Prefix for Voucher $i: $business_prefix"); if (!empty($business_prefix) && !empty($voucher_text)) { // Get or initialize the current voucher number, starting at 500 $current_voucher_number = (int) get_post_meta($product_id, $voucher_number_field, true); $current_voucher_number = max(499, $current_voucher_number); $new_voucher_number = $current_voucher_number + 1; update_post_meta($product_id, $voucher_number_field, $new_voucher_number); // Create the combined voucher text with the business name prefix and a dash $voucher_combined_text = "{$business_prefix}-$new_voucher_number for $voucher_text"; error_log("New Voucher Text for Voucher $i: $voucher_combined_text"); // Save the combined voucher text as order item meta wc_add_order_item_meta($item_id, "voucher_{$i}_combined_text", $voucher_combined_text); // Add the voucher code to the voucher codes array $voucher_codes[] = $voucher_combined_text; } } // Add details for this item to the order summary $order_summary[] = [ 'booking_id' => $item_booking_id, 'game_code' => $game_code, 'voucher_codes' => $voucher_codes ]; } // Create a readable text format of the order summary $order_summary_text = ''; foreach ($order_summary as $index => $details) { $order_summary_text .= "Product " . ($index + 1) . "\n"; $order_summary_text .= "Booking ID: " . $details['booking_id'] . "\n"; $order_summary_text .= "Game Code: " . $details['game_code'] . "\n"; if (!empty($details['voucher_codes'])) { foreach ($details['voucher_codes'] as $i => $voucher_code) { $order_summary_text .= "Voucher " . ($i + 1) . ": " . $voucher_code . "\n"; } } else { $order_summary_text .= "Vouchers: –\n"; } $order_summary_text .= "\n"; // Add a line break between products } error_log("Order summary text: \n$order_summary_text"); // Save the readable order summary if ($user_id) { update_user_meta($user_id, 'order_summary', $order_summary_text); // Removed leading underscore error_log("Order summary text saved for user ID: $user_id."); } else { update_post_meta($order_id, 'order_summary', $order_summary_text); // Removed leading underscore error_log("Order summary text saved as order meta for order ID: $order_id."); } error_log("Order summary text saved. Data: \n$order_summary_text"); } else { error_log("Order status is not 'processing' or 'completed' for order ID: $order_id. Status: $new_status"); } } // Add custom meta boxes to the order details page add_action('add_meta_boxes', 'add_custom_voucher_meta_boxes'); /** * Add a custom meta box to the WooCommerce order details page to display booking and voucher details. */ function add_custom_voucher_meta_boxes() { add_meta_box( 'custom_voucher_meta_box', // ID of the meta box __('Booking and Voucher Details', 'your-text-domain'), // Title of the meta box 'display_custom_voucher_meta_box', // Callback function to display the content 'shop_order', // Screen where to add the meta box (shop_order for WooCommerce orders) 'normal', // Context (normal, side, advanced) 'default' // Priority (default, high, low) ); } /** * Display the content of the custom meta box. * * @param WP_Post $post The post object representing the order. */ function display_custom_voucher_meta_box($post) { // Retrieve the order summary text $order_summary_text = get_post_meta($post->ID, 'order_summary', true); if (!empty($order_summary_text)) { echo '<h3>Order Summary</h3>'; echo nl2br(esc_html($order_summary_text)); } else { echo '<p>' . __('No booking or voucher details found for this order.', 'your-text-domain') . '</p>'; } } // Add the order summary to the order details page in the WooCommerce backend add_action('woocommerce_admin_order_data_after_order_details', 'display_order_summary_in_admin'); /** * Display the order summary in the WooCommerce order backend. * * @param WC_Order $order The WooCommerce order object. */ function display_order_summary_in_admin($order) { $order_id = $order->get_id(); $order_summary_text = get_post_meta($order_id, 'order_summary', true); if (!empty($order_summary_text)) { echo '<div class="order_data_column">'; echo '<h4>' . __('Order Summary', 'your-text-domain') . '</h4>'; echo '<p>' . nl2br(esc_html($order_summary_text)) . '</p>'; echo '</div>'; } }
Editor is loading...
Leave a Comment