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

  1. Log in to your WordPress dashboard.
  2. Go to Pages > Add New.
  3. Enter a title for your page, e.g., "Add New Order".
  4. In the content area, add a brief description of the page, if needed.
  5. Click the "Publish" button to save the page.

Step 2: Add a form to the page

  1. Install and activate the "WPForms" plugin (or any other form builder plugin you prefer).
  2. Go to WPForms > Add New Form.
  3. 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.)
  4. Add a submit button to the form.
  5. Click the "Save" button to save the form.

Step 3: Add the form to the page

  1. Go back to the page you created in Step 1.
  2. Click the "Edit" button to edit the page.
  3. In the content area, click the "Add Block" button.
  4. Search for "WPForms" and select the form you created in Step 2.
  5. Click the "Insert" button to add the form to the page.
  6. Click the "Update" button to save the changes.

Step 4: Create a custom function to process the form

  1. Install and activate the "Code Snippets" plugin.

  2. Go to Code Snippets > Add New Snippet.

  3. Enter a title for your snippet, e.g., "Add New Order".

  4. 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');
    ?>
  5. Click the "Save" button to save the snippet.

Step 5: Add a JavaScript code to submit the form

  1. Go back to the page you created in Step 1.
  2. Click the "Edit" button to edit the page.
  3. In the content area, click the "Add Block" button.
  4. Search for "JavaScript" and select the "JavaScript" block.
  5. 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>
  6. Click the "Update" button to save the changes.

Step 6: Test the form

  1. Go to the page you created in Step 1.
  2. Fill out the form and submit it.
  3. 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.