Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active June 6, 2020 23:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/003ace603d731679458d0f9993bc3f7c to your computer and use it in GitHub Desktop.
Save damiencarbery/003ace603d731679458d0f9993bc3f7c to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Create WooCommerce Order for new accounts
Plugin URI: http://www.damiencarbery.com/2018/01/create-woocommerce-order-for-new-customers/
Description: Create an order with a downloadable product when a new account is created.
Author: Damien Carbery
Author URI: http://www.damiencarbery.com
Version: 0.1
*/
class FreeOrderForNewAccounts {
private $debug_mode;
private $user_id;
private $free_product_id;
public function __construct() {
// Allow debug mode to be enabled when WordPress debug mode is enabled.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
$this->debug_mode = true;
}
// Or set it manually.
//$this->debug_mode = false;
$this->user_id = null;
$this->free_product_id = null;
}
public function init() {
add_action( 'woocommerce_checkout_update_user_meta', array( $this, 'post_checkout_create_new_customer_order' ), 10, 2 );
}
// Runs after a new customer account is created (and meta info, like billing address, saved).
public function post_checkout_create_new_customer_order( $customer_id, $data ) {
$this->user_id = $customer_id;
// Users of this plugin must use this filter to specify the ID of the free product.
// Usage: add_filter( 'free_order_for_new_accounts_set_product_id',function( $id ){ return 12; });
$this->free_product_id = apply_filters( 'free_order_for_new_accounts_set_product_id', $this->free_product_id );
if ( !is_int( $this->free_product_id ) ) {
if ( $this->debug_mode ) {
error_log( 'woocommerce_checkout_update_user_meta: Product ID not set.' );
}
return;
}
if ( $this->debug_mode ) {
error_log( 'woocommerce_checkout_update_user_meta: Customer ID: ' . $this->user_id );
error_log( 'woocommerce_checkout_update_user_meta: Data: ' . var_export( $data, true ) );
}
$customer_meta = get_user_meta( $this->user_id );
if ( $this->debug_mode ) {
error_log( 'woocommerce_checkout_update_user_meta: Customer Meta: ' . var_export( $customer_meta, true ) );
}
$this->create_order_for_free_downloads();
}
public function create_order_for_free_downloads() {
$user = get_user_by( 'id', $this->user_id );
if ( empty( $user ) ) {
return '<p>ERROR: Could not retrieve user information.</p>';
}
$user_meta = get_user_meta( $this->user_id );
$address_fields = array( 'first_name', 'last_name', 'email', 'phone', 'address_1', 'address_2', 'city', 'state', 'postcode', 'country' );
$address = array();
// Verify that billing address fields exist. Set to empty as default.
foreach ( $address_fields as $field ) {
$address[ $field ] = isset( $user_meta[ 'billing_'. $field ][ 0 ] ) ? $user_meta[ 'billing_'. $field ][ 0 ] : '';
}
if ( $this->debug_mode ) {
error_log( 'Order address fields: '. var_export( $address, true ) );
}
// Now we create the order
$order = wc_create_order();
if ( $this->debug_mode ) {
error_log( 'Order created for customer ID: '. $this->user_id . ' with name: ' . $address['first_name'] );
}
$order->add_product( wc_get_product( $this->free_product_id ), 1);
$order->set_address( $address, 'billing' );
$order->calculate_totals();
$order->update_status( 'wc-completed', 'Free Download', TRUE );
// Setting the status to completed creates an entry in '{prefix}woocommerce_downloadable_product_permissions' table.
// We need to change the user id from 0 to the required user. I don't know why it is not done by WooCommerce.
global $wpdb;
$wpdb->update( $wpdb->prefix . 'woocommerce_downloadable_product_permissions', array('user_id'=> $this->user_id ), array( 'order_id' => $order->get_order_number() ), array( '%d' ), array( '%d' ) );
if ( $this->debug_mode ) {
error_log( 'Order created: '. $order->get_order_number() );
}
}
}
$FreeOrderForNewAccounts = new FreeOrderForNewAccounts();
$FreeOrderForNewAccounts->init();
<?php
<?php
/*
Plugin Name: Set product ID for free download
Plugin URI: http://www.damiencarbery.com/2018/01/create-woocommerce-order-for-new-customers/
Description: Set the product ID for the order that is automatically created when a new account is created.
Author: Damien Carbery
Author URI: http://www.damiencarbery.com
Version: 0.1
*/
// Change the number 72 to the ID of the downloadable product.
add_filter( 'free_order_for_new_accounts_set_product_id',function( $id ){ return 72; });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment