Allow woocommerce users to add new order
To allow WooCommerce users to add new orders, you'll need to create a custom page and add a form to it. Here's a step-by-step guide to help you achieve this:
Step 1: Create a new page
- Log in to your WordPress dashboard.
- Go to Pages > Add New.
- Enter a title for your page, e.g., "Add New Order".
- In the content area, add a brief description of the page, if needed.
- Click the "Publish" button to save the page.
Step 2: Add a form to the page
- Install and activate the "WPForms" plugin (or any other form builder plugin you prefer).
- Go to WPForms > Add New Form.
- Create a new form with the following fields:
- Text field: "Order Number" (required)
- Text field: "Customer Name" (required)
- Text field: "Email" (required)
- Text field: "Phone" (optional)
- Text field: "Order Date" (required)
- Text area: "Order Notes" (optional)
- Select field: "Payment Method" (e.g., "Cash", "Credit Card", etc.)
- Select field: "Shipping Method" (e.g., "Standard", "Express", etc.)
- Add a submit button to the form.
- Click the "Save" button to save the form.
Step 3: Add the form to the page
- Go back to the page you created in Step 1.
- Click the "Edit" button to edit the page.
- In the content area, click the "Add Block" button.
- Search for "WPForms" and select the form you created in Step 2.
- Click the "Insert" button to add the form to the page.
- Click the "Update" button to save the changes.
Step 4: Create a custom function to process the form
-
Install and activate the "Code Snippets" plugin.
-
Go to Code Snippets > Add New Snippet.
-
Enter a title for your snippet, e.g., "Add New Order".
-
In the code area, add the following code:
<?php function add_new_order() { // Get the form data $order_number = $_POST['order_number']; $customer_name = $_POST['customer_name']; $email = $_POST['email']; $phone = $_POST['phone']; $order_date = $_POST['order_date']; $order_notes = $_POST['order_notes']; $payment_method = $_POST['payment_method']; $shipping_method = $_POST['shipping_method']; // Create a new order $order = wc_create_order(array( 'customer_name' => $customer_name, 'email' => $email, 'phone' => $phone, 'order_date' => $order_date, 'order_notes' => $order_notes, 'payment_method' => $payment_method, 'shipping_method' => $shipping_method, )); // Add order items // (You can add order items programmatically using the WC_Order_Item class) // Save the order $order->save(); // Send a confirmation email to the customer // (You can use the WC_Email class to send an email) // Redirect the user to a success page wp_redirect(admin_url('admin.php?page=woocommerce-orders&view=order&id='. $order->get_id())); exit; } add_action('wp_ajax_add_new_order', 'add_new_order'); ?>
-
Click the "Save" button to save the snippet.
Step 5: Add a JavaScript code to submit the form
- Go back to the page you created in Step 1.
- Click the "Edit" button to edit the page.
- In the content area, click the "Add Block" button.
- Search for "JavaScript" and select the "JavaScript" block.
- Add the following code:
<script> jQuery(document).ready(function($) { $('#add-new-order-form').submit(function(event) { event.preventDefault(); $.ajax({ type: 'POST', url: '<?php echo admin_url('admin-ajax.php');?>', data: $(this).serialize(), success: function(response) { // Redirect the user to a success page window.location.href = '<?php echo admin_url('admin.php?page=woocommerce-orders&view=order&id='. $order->get_id());?>'; } }); }); }); </script>
- Click the "Update" button to save the changes.
Step 6: Test the form
- Go to the page you created in Step 1.
- Fill out the form and submit it.
- The form should submit successfully and redirect you to a success page.
That's it! You have now allowed WooCommerce users to add new orders.