How to implement "One More Order" in WooCommerce

When building an e-commerce website, "Convenience to others is convenience to yourself". Customers who find it convenient to buy things on your website will come back to your website again, and may introduce your website to their friends. On the order list page, add the wordOne more.The "feature is a great time for customer convenience.

This is especially suitable for FMCG, consumables, and other e-commerce sites that require frequent repeat purchases. WooCommerce doesn't come with a re-order feature, and in this post, I'll show you how to implement the re-order feature.

First, add the "Place another order" button to the order list page.

First of all, we need to add anOne more.WooCommerce provides us with the corresponding Hook, just add the following code to the functions.php of the theme.

add_filter('woocommerce_my_account_my_orders_actions', 'wprs_add_reorder_button', 10, 2);
function wprs_add_reorder_button($actions, $order) {
    // Check if the order status is complete
    if ($order->has_status('completed')) {
        $actions['order'] = array(
            'url' => wp_nonce_url(add_query_arg('reorder', $order->get_id()), 'woocommerce-reorder'),
            'name' => 'reorder'
        );
    }
    return $actions.
}

Handling of re-order requests and realization of re-order functions

The user will submit a request to the back-end after clicking on the come again button, and we need to process this request in several steps to realize the come again functionality.

  1. Security validation to confirm that the request was indeed submitted by the user.
  2. Get the original order, according to the request sent by the front-end, that is, reorder this parameter in the order ID to get the original order.
  3. Empty the current cart (this step is based on your logic to see if it is needed)
  4. Add the items in the original order to the shopping cart, before adding, we need to check whether these items are still in stock, add to the cart if they are in stock, and notify the user if they are not in stock.
  5. After the first few steps are completed, jump to the shopping cart page, once again the user to confirm the product, to facilitate the checkout order.
add_action('wp_loaded', 'wprs_handle_reorder_request', 20);
function wprs_handle_reorder_request() {
    if (!isset($_GET['reorder']) || !is_numeric($_GET['reorder'])) {
        return;
    }

    // Validate the nonce
    if (!wp_verify_nonce($_GET['_wpnonce'], 'woocommerce-reorder')) {
        wc_add_notice('Invalid request', 'error');
        wp_safe_redirect(wc_get_page_permalink('myaccount'));
        exit.
    }

    // Get the original order
    $order_id = absint($_GET['reorder']);
    $order = wc_get_order($order_id);

    if (!$order) {
        wc_add_notice('Order doesn't exist', 'error');
        wp_safe_redirect(wc_get_page_permalink('myaccount'));
        exit;
    }

    // Empty the current cart
    WC()->cart->empty_cart();

    // Add the items from the order to the cart
    foreach ($order->get_items() as $item) {
        $product_id = $item->get_product_id();
        $variation_id = $item->get_variation_id();
        $quantity = $item->get_quantity();

        // Check if the product still exists and is in stock
        $product = wc_get_product($product_id);
        if (!$product | !$product->is_purchasable() | !$product->is_in_stock()) {
            wc_add_notice(sprintf('Product "%s" is no longer available for purchase or is out of stock', $item->get_name()), 'error');
            continue; }
        }

        // Add to cart
        if ($variation_id) {
            WC()->cart->add_to_cart($product_id, $quantity, $variation_id);
        } else {
            WC()->cart->add_to_cart($product_id, $quantity);
        }
    }

    // Jump to the cart page
    wp_safe_redirect(wc_get_cart_url()); }
    exit; }
}

Implementing "One More Order" via Plugin

In addition to custom code, there are some plug-ins can help us achieve the function of another single, the following are two good plug-ins, the need for friends can try.

Related Posts

Leave a Reply

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