Gernx IT specializes in WordPress development, custom plugin engineering, Elementor, and WooCommerce solutions. We help businesses build scalable, high-performance websites tailored to their needs.

© Gernx IT – All rights reserved.
Programmatically Create Order in WooCommerce The Complete Guide

How to Create Orders Programmatically in WooCommerce: Step-by-Step Guide

By: / WooCommerce / Comments Off on How to Create Orders Programmatically in WooCommerce: Step-by-Step Guide

Would you like to create orders programmatically in WooCommerce? Great, you found an appropriate tutorial. In this article, we’ll discuss how to create orders programmatically in WooCommerce. This is a simple guide and a very short tutorial!

We’ll use the wc_create_order() function to create an order, and we’ll set the order ID to get a programmatically ordered ID.

Add the following code to the functions.php file and change the get_product( 1061 ) ID to add the product number to orders.

add_action( 'woocommerce_before_checkout_form', 'create_wc_auto_order' );
function create_wc_auto_order() {
	global $woocommerce;
	$address = array(
		'first_name' => 'Tanmoy',
		'last_name'  => 'San',
		'company'    => 'Automattic',
		'email'      => 'no@spam.com',
		'phone'      => '123-123-123',
		'address_1'  => '123 Main Woo st.',
		'address_2'  => '100',
		'city'       => 'San Francisco',
		'state'      => 'CA',
		'postcode'   => '92121',
		'country'    => 'US'
	);
	
	// Now we create the order
	$order = wc_create_order();
	
	// The add_product() function below is located in /plugins/woocommerce/includes/abstracts/abstract_wc_order.php
	$order->add_product( get_product( 1061 ), 1); // Use the product IDs to add
	
	// Set addresses
	$order->set_address( $address, 'billing' );
	$order->set_address( $address, 'shipping' );
	
	// Set payment gateway
	$payment_gateways = WC()->payment_gateways->payment_gateways();
	$order->set_payment_method( $payment_gateways['bacs'] );
	
	// Calculate totals
	$order->calculate_totals();
	$order->update_status( 'Completed', 'Imported order', TRUE);
}

Done!
Now, check the admin panel for WooCommerce to Orders.
In this guide, we have spotlighted creating orders programmatically in WooCommerce using the PHP function. Hope this tutorial helps you create the order programmatically according to your needs.

If you have any questions, please just comment below.

WooCommerce Top Discount Plugins

We’re going to show you some top and free WooCommerce Discount Plugins. You can use these free plugins for your WooCommerce store.

Offering discounts is one of the most successful strategies in improving your store’s sales. In this section, we have listed the best WooCommerce discount plugin for your eCommerce store. Look through this section and choose the one that best suits your store’s needs.

All plugins will help you to create any type of bulk discounts, dynamic pricing, advanced discounts, percentage discounts, product-based discounts, and tiered discounts for your store products.

  1. Discount Rules for WooCommerce
  2. Discount Rules and Dynamic Pricing for WooCommerce
  3. Conditional Discounts for WooCommerce
  4. Dynamic Pricing With Discount Rules for WooCommerce

Wrap Up

In this article, we discussed how to create orders programmatically in WooCommerce and covered some of the best free discount plugins. Each of the plugins mentioned above is an amazing solution for offering discounts in your store, and they are all free.

You can choose the best one for your store. We hope this tutorial helps you find the best discount plugin and create orders programmatically in WooCommerce.

So, if you have any questions, please let us know!



WordPress development company specializing in custom plugins, WooCommerce solutions, Elementor websites, performance optimization, and ongoing maintenance & support.

Contact Us

Service

© Gernx IT – All rights reserved.