edubellomo
Forum Replies Created
-
Forum: Plugins
In reply to: [Product Configurator for WooCommerce] Hooking onto Configurator ContentI am also marking this thread as resolved ??
Forum: Plugins
In reply to: [Product Configurator for WooCommerce] Hooking onto Configurator ContentHi Marc,
Thanks! this is useful. I have expanded the code to also include a Form field that codes the “Quantity” in a numeric variable.
The code now prints the configuration in the email body but also adds a txt (tab delimited) with a few more information. I am sharing it in case someone needed it.
// Add a custom function to send an email and generate a tab-delimited file when an order is placed add_action('woocommerce_checkout_order_processed', 'send_custom_order_email_and_file', 10, 3); function send_custom_order_email_and_file($order_id, $posted_data, $order) { // Get the shop admin email $admin_email = get_option('admin_email'); // Set the email subject and content $subject = 'New Configuration Notification'; $message = 'A new configuration has been discussed.'; // Prepare the file content header $file_content = "order_id\tend_user_name\tend_user_surname\tend_user_email\tinstitute\tchoice_name\tsku\tquantity\n"; // Get billing information $billing_first_name = $order->get_billing_first_name(); $billing_last_name = $order->get_billing_last_name(); $billing_email = $order->get_billing_email(); $billing_company = $order->get_billing_company(); // Loop through each item in the order foreach ($order->get_items() as $item_id => $item) { // Check if the item has the 'Configuration' meta content if ($data = $item->get_meta('_configurator_data')) { foreach ($data as $configurator_item) { $layer_name = $configurator_item->get_layer('name'); $choice_name = $configurator_item->get_choice('name'); $sku = $configurator_item->get_choice('sku'); $text_field_choice = $configurator_item->get_choice('text_field_type'); if ($text_field_choice === 'number' && isset($configurator_item->field_value) && is_numeric($configurator_item->field_value)) { $quantity = floatval($configurator_item->field_value); } else { $quantity = 1; } // Add a new row for each product to the email content if choice_name and sku are not empty if (!empty($choice_name) && !empty($sku)) { $message .= "\n\nOrder ID: " . $order_id; $message .= "\nLayer Name: " . $layer_name; $message .= "\nItem Name: " . $choice_name; $message .= "\nItem SKU: " . $sku; $message .= "\nQuantity: " . $quantity; // Append row to the file content $file_content .= $order_id . "\t" . $billing_first_name . "\t" . $billing_last_name . "\t" . $billing_email . "\t" . $billing_company . "\t" . $choice_name . "\t" . $sku . "\t" . $quantity . "\n"; } } } } // Generate the file path and name $file_name = 'order_data_' . $order_id . '.txt'; $file_path = ABSPATH . $file_name; // Write the file content to the file file_put_contents($file_path, $file_content); // Send the email to the shop admin with the attached file $attachments = array($file_path); wp_mail($admin_email, $subject, $message, '', $attachments); // Remove the temporary file unlink($file_path); }
Forum: Plugins
In reply to: [Product Configurator for WooCommerce] Hooking onto Configurator ContentThanks marc. If anybody needed it. I have written a function that extracts the product_name, sku, and product_quantity from the meta html contetn of the configurator.
function extract_product_data_from_html($html) { $product_data = array(); // Create a new DOMDocument instance $dom = new DOMDocument(); // Load the HTML content $dom->loadHTML($html); // Create a new DOMXPath instance $xpath = new DOMXPath($dom); // Find all the span elements with the class "mkl_pc-choice-name" that are not empty $choices = $xpath->query('//span[contains(@class, "mkl_pc-choice-name")][normalize-space()]'); // Loop through the choices foreach ($choices as $choice) { // Get the product name $productName = $choice->textContent; // Get the parent div element $div = $choice->parentNode; // Get the SKU $sku = $xpath->query('.//span[contains(@class, "sku")]', $div)->item(0)->textContent; // Get the quantity $quantityNode = $xpath->query('.//span[contains(@class, "mkl-pc-form-value--arrow")]/following-sibling::text()[normalize-space()]', $div)->item(0); $quantity = $quantityNode ? intval(trim($quantityNode->textContent)) : 1; // Store the product data in an array $product_data[] = array( 'name' => trim($productName), 'sku' => trim($sku), 'quantity' => $quantity ); } return $product_data; }
This function can be called, for example, to generate an email that summarizes the content of a configuration.
// Add a custom function to send an email when an order is placed add_action('woocommerce_checkout_order_processed', 'send_custom_order_email', 10, 3); function send_custom_order_email($order_id, $posted_data, $order) { // Get the shop admin email $admin_email = get_option('admin_email'); // Set the email subject and content $subject = 'New Order Notification'; $message = 'A new order has been placed.'; // Loop through each item in the order foreach ($order->get_items() as $item_id => $item) { // Check if the item has the 'Configuration' meta content if ($item->get_meta('Configuration')) { // Extract product data from HTML using the extract_product_data_from_html function $html = $item->get_meta('Configuration'); $product_data = extract_product_data_from_html($html); // Loop through the extracted product data foreach ($product_data as $data) { // Get the product name, SKU, and quantity $product_name = $data['name']; $sku = $data['sku']; $quantity = $data['quantity']; // Add a new row for each product to the email content $message .= "\n\nOrder ID: " . $order_id; $message .= "\nItem Name: " . $product_name; $message .= "\nItem SKU: " . $sku; $message .= "\nQuantity: " . $quantity; } } else { // Item does not have the 'Configuration' meta content, handle it accordingly } } // Send the email to the shop admin wp_mail($admin_email, $subject, $message); }
My actual goal is to further edit this code so that it also prints a txt file.
Why?
Because we are actually processing our orders in a different system and exporting the order content to a separate file would make the import in our system easier.
Closed!
Ace! thanks!