Create a WooCommerce order on the cart page, without the checkout
WooCommerce tasks are very different. Today we will look at how to create an order manually, right from the cart page.
Problem: The customer has to place the order on the WooCommerce cart page, skipping the checkout step. Payment is handled offline, the shop only needs the order and the contact details.
Solution: The solutions I found online override WooCommerce templates. This one is less beautiful, but it may be useful to someone: a Contact Form 7 form on the cart page collects the details, and after the form is sent we create the order from the cart with wc_create_order().
Step 1. The form
Create a form in Contact Form 7 with the fields you need. In this example they are customer-name, customer-email, customer-phone, organization-name, address, and three fields for the order note: add-info, using and info. Then add the form shortcode to the cart template (woocommerce/cart/cart.php in your theme), below the cart table.
Step 2. Create the order after the form is sent
In my first version, a script copied the product IDs and quantities from the cart table into a hidden field before submission. But the cart is already stored on the server, so it is simpler and safer to read it there. Customers cannot change the contents of a hidden field this way, and the CF7 events are not a problem: wpcf7submit fires after the form has already been sent.
Add to functions.php:
<?php
add_action( 'wpcf7_mail_sent', 'create_order_from_cart_form' );
function create_order_from_cart_form( $contact_form ) {
$submission = WPCF7_Submission::get_instance();
if ( ! $submission ) {
return;
}
$data = $submission->get_posted_data();
// CF7 sends forms through the REST API, where WooCommerce does not load the cart by default.
if ( null === WC()->cart && function_exists( 'wc_load_cart' ) ) {
wc_load_cart();
}
if ( ! WC()->cart || WC()->cart->is_empty() ) {
return;
}
$order = wc_create_order();
foreach ( WC()->cart->get_cart() as $item ) {
$order->add_product( $item['data'], $item['quantity'] );
}
$order->set_address( array(
'first_name' => sanitize_text_field( $data['customer-name'] ),
'email' => sanitize_email( $data['customer-email'] ),
'company' => sanitize_text_field( $data['organization-name'] ),
'phone' => sanitize_text_field( $data['customer-phone'] ),
'address_1' => sanitize_text_field( $data['address'] ),
), 'billing' );
$order->add_order_note(
'Additional info: ' . sanitize_textarea_field( $data['add-info'] ) . '<br>' .
'Intended use: ' . sanitize_text_field( $data['using'] ) . '<br>' .
'How did you hear about us? ' . sanitize_text_field( $data['info'] )
);
// Payment method: cash on delivery
$gateways = WC()->payment_gateways->payment_gateways();
if ( isset( $gateways['cod'] ) ) {
$order->set_payment_method( $gateways['cod'] );
}
$order->calculate_totals();
$order->update_status( 'processing', 'Order from the cart page form.', true );
WC()->cart->empty_cart();
}
calculate_totals() takes the prices from the products, not from the form, so the totals are always correct. update_status() saves the order and, with the third argument true, records the status change in the order notes.
Step 3. Hide the cart after success
The page is not reloaded after a CF7 submission, so hide the cart table yourself when the form has been sent:
<?php
add_action( 'wp_footer', 'hide_cart_after_order_form', 99 );
function hide_cart_after_order_form() {
if ( ! is_cart() ) {
return;
}
?>
<script>
document.addEventListener( 'wpcf7mailsent', function () {
jQuery( '.woocommerce-cart-form' ).fadeOut( 'slow', function () {
jQuery( this ).replaceWith( '<div class="woocommerce-notices-wrapper"><p class="cart-empty woocommerce-info">Thank you! Your order has been received.</p></div>' );
} );
}, false );
</script>
<?php
}
NOTE: I closed this project soon after, so test it on your WooCommerce version, especially the cart loading in the REST request. The order is created only after the email has been sent successfully (wpcf7_mail_sent). If the mail fails, there is no order, so check the mail settings first.
Sources: