Form to repeat an order via shortcode

Sometimes a customer doesn't need to search through the entire catalogue to buy the same thing as last time. All they need is the previous order number and an email. This snippet creates a simple form that, after verifying the data, automatically populates the cart with identical products and redirects the customer to checkout.

How does it work?

This code registers the shortcode [repeat_order_form], that you can embed anywhere - in a page, a post or a widget.

  1. Form: It displays two fields (Email and Order Number).
  2. Verification: Via AJAX (without refreshing the page), it checks if the order with the given number exists and if the specified email is associated with it.
  3. Action: If the data matches, the system empties the current cart, adds the items from the original order and sends the customer directly to checkout.
PHP
function zopakovat_objednavku_formular() {
    ob_start();
    ?>
    <style>
    #repeat-order-form .form-grid {
      display: grid;
      grid-template-columns: 1fr 1fr;
      column-gap: 1rem;
      row-gap: 0.5rem;
      margin-bottom: 1.5rem;
    }
    #repeat-order-form .form-group {
      display: flex;
      flex-direction: column;
    }
    #repeat-order-form button {
      margin-bottom: 0.5rem;
    }
    #repeat-order-form #response-message {
      display: none;
    }
    </style>

    <form id="repeat-order-form">
      <div class="form-grid">
        <div class="form-group">
          <label for="email">Váš e-mail:</label>
          <input type="email" name="email" id="email" required class="input-text" />
        </div>

        <div class="form-group">
          <label for="order_id">Číslo objednávky:</label>
          <input type="text" name="order_id" id="order_id" required class="input-text" />
        </div>
      </div>

      <button type="submit" class="button alt">Zopakovať objednávku</button>
      <p id="response-message" class="woocommerce-error"></p>
    </form>

    <script>
    jQuery(document).ready(function($) {
      $('#repeat-order-form').submit(function(e) {
        e.preventDefault();

        var email = $('#email').val();
        var order_id = $('#order_id').val();

        $.ajax({
          type: "POST",
          url: "<?php echo admin_url('admin-ajax.php'); ?>",
          data: {
            action: "repeat_order",
            email: email,
            order_id: order_id
          },
          success: function(response) {
            if (response.success) {
              // Tu zmeňte URL na vašu stránku pokladne/košíka
              window.location.href = "https://vasadomena.sk/pokladna/";
            } else {
              $('#response-message')
                .text(response.data)
                .show();
            }
          }
        });
      });
    });
    </script>
    <?php
    return ob_get_clean();
}
add_shortcode('repeat_order_form', 'zopakovat_objednavku_formular');

// AJAX proces na pozadí
function zopakovat_objednavku_ajax() {
    if (!isset($_POST['email']) || !isset($_POST['order_id'])) {
        wp_send_json_error("Neplatné údaje.");
    }

    $email = sanitize_email($_POST['email']);
    $order_id = intval($_POST['order_id']);
    $order = wc_get_order($order_id);

    if ($order && $order->get_billing_email() === $email) {
        WC()->cart->empty_cart(); // Vyčistíme košík pred pridaním nových vecí

        foreach ($order->get_items() as $item) {
            WC()->cart->add_to_cart($item->get_product_id(), $item->get_quantity());
        }

        wp_send_json_success();
    } else {
        wp_send_json_error("Objednávka nebola nájdená alebo e-mail nesedí.");
    }
}
add_action('wp_ajax_repeat_order', 'zopakovat_objednavku_ajax');
add_action('wp_ajax_nopriv_repeat_order', 'zopakovat_objednavku_ajax');

What to watch out for?

  • The URL of the redirect (line 60): In the script (line with window.location.href) is set to a static address. Be sure to change it to your real checkout address before deployment.
  • Styling: I've added basic CSS to keep the fields side by side, but the snippet takes over the visuals of your theme (buttons, fonts). If it doesn't look to your liking, edit the CSS in the function header.
  • Safety: The snippet checks for a match between the email and the order ID. This prevents someone from randomly „trying“ order numbers and seeing what was in them.

Leave a Reply

Your email address will not be published. Required fields are marked *