Programmatically creating new order in Woocommerce

Cover Image for Programmatically creating new order in Woocommerce
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

🛒 Creating a New Order in WooCommerce Programmatically

Having trouble programmatically creating an order in WooCommerce? Don't worry, we're here to help you out! 🤓💪

🔍 The Problem

You've tried using the code below to create an order, but you're encountering some issues. The order is created successfully, but there's no customer information or product line items added to it. The order seems to be created as a guest without any details.

function create_vip_order() {
    global $woocommerce;

    $address = array(
        'first_name'  => '111Joe',
        'last_name'   => 'Conlin',
        'company'     => 'Speed Society',
        'email'       => 'joe@testing.com',
        'phone'       => '760-555-1212',
        'address_1'   => '123 Main st.',
        'address_2'   => '104',
        'city'        => 'San Diego',
        '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( '275962' ), 1 ); // This is an existing SIMPLE product
    $order->set_address( $address, 'billing' );

    $order->calculate_totals();
    $order->update_status("Completed", 'Imported order', TRUE);
}

add_action( 'woocommerce_init', 'create_vip_order' );

🐞 The Error

Here's the error you're getting in your logs:

[19-Apr-2016 21:16:38 UTC] PHP Fatal error:  Uncaught Error: Call to a member function add_product() on boolean in /Users/joe/Sites/speedsociety-2/wp-content/themes/ss/lib/contests/order.php:107
Stack trace:
#0 /Users/joe/Sites/speedsociety-2/wp-includes/plugin.php(525): create_vip_order('')
#1 /Users/joe/Sites/speedsociety-2/wp-content/plugins/woocommerce/woocommerce.php(330): do_action('woocommerce_ini...')
#2 /Users/joe/Sites/speedsociety-2/wp-includes/plugin.php(525): WooCommerce->init('')
#3 /Users/joe/Sites/speedsociety-2/wp-settings.php(392): do_action('init')
#4 /Users/joe/Sites/speedsociety-2/wp-config.php(67): require_once('/Users/joe/Site...')
#5 /Users/joe/Sites/speedsociety-2/wp-load.php(37): require_once('/Users/joe/Site...')
#6 /Users/joe/Sites/speedsociety-2/wp-admin/admin.php(31): require_once('/Users/joe/Site...')
#7 /Users/joe/Sites/speedsociety-2/wp-admin/edit.php(10): require_once('/Users/joe/Site...')
#8 {main}
  thrown in /Users/joe/Sites/speedsociety-2/wp-content/themes/ss/lib/contests/order.php on line 107

💡 The Solution

To fix this issue, you need to make sure that you're using the correct hook and the right place to create the order. Instead of using the woocommerce_init hook, you should use the woocommerce_new_order hook, which is fired after a new order is created.

function create_vip_order( $order_id ) {
    $order = wc_get_order( $order_id );

    $address = array(
        'first_name'  => '111Joe',
        'last_name'   => 'Conlin',
        'company'     => 'Speed Society',
        'email'       => 'joe@testing.com',
        'phone'       => '760-555-1212',
        'address_1'   => '123 Main st.',
        'address_2'   => '104',
        'city'        => 'San Diego',
        'state'       => 'Ca',
        'postcode'    => '92121',
        'country'     => 'US'
    );

    $order->add_product( get_product( '275962' ), 1 ); // This is an existing SIMPLE product
    $order->set_address( $address, 'billing' );

    $order->calculate_totals();
    $order->update_status( 'completed', 'Imported order', true );
}

add_action( 'woocommerce_new_order', 'create_vip_order', 10, 1 );

By using this hook, you will have access to the $order_id parameter, which allows you to retrieve the order object using the wc_get_order() function.

🎉 Now you should be able to create new orders programmatically and have customer information and product line items properly added to the order!

📣 Get Involved!

We hope this guide helped you solve your issue and get your WooCommerce orders flowing smoothly. If you have any other questions or need further assistance, feel free to leave a comment below. We'd love to hear from you! 😊

Don't forget to share this guide with your fellow developers or WooCommerce users who might find it helpful. Let's spread the knowledge and help everyone build awesome digital stores! 🚀🙌


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello