Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active May 1, 2021 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save damiencarbery/263e86a77d3429e0d3bcca03cbcefb36 to your computer and use it in GitHub Desktop.
Save damiencarbery/263e86a77d3429e0d3bcca03cbcefb36 to your computer and use it in GitHub Desktop.
Add free downloads to the WooCommerce My Account/Downloads page (a question from Facebook Advanced WooCommerce group - https://www.facebook.com/groups/advanced.woocommerce/permalink/1928688317145578/
<?php
/*
Plugin Name: Free Downloads for WooCommerce
Plugin URI: http://www.damiencarbery.com
Description: Add free downloads to the My Account/Downloads page.
Author: Damien Carbery
Author URI: http://www.damiencarbery.com
Version: 0.1
*/
add_filter( 'woocommerce_customer_get_downloadable_products', 'wc_free_downloads' );
function wc_free_downloads( $downloads ) {
// Use this to find out the
//error_log( 'Downloads: ' . var_export( $downloads, true ) );
$item =
array (
'download_url' => 'http://localhost/wp-content/uploads/2017/12/an-image-file.jpg',
'download_name' => 'Free Download Picture',
'downloads_remaining' => '',
'access_expires' => NULL,
//'download_id' => '7f7e4923ff94b7928c0b8b5e93fb4f89',
//'product_id' => 17, // was 17
//'order_id' => 20,
//'order_key' => 'wc_order_5a382bef811e6',
/*'file' =>
array (
'name' => 'A Downloadable Image',
'file' => 'http://localhost/wp-content/uploads/woocommerce_uploads/2017/12/a-downloadable-image-file.png',
),*/
);
$downloads[] = $item;
return $downloads;
}
<?php
function create_order_for_free_downloads() {
$user_id = 1;
$user = get_user_by( 'id', $user_id );
if ( empty( $user ) ) {
return '<p>ERROR: Could not retrieve user information.</p>';
}
$user_meta = get_user_meta( $user_id );
// TODO: Verify that $user_meta has the necessary fields - or test what fields are required to create an order.
$address = array(
'first_name' => $user_meta['billing_first_name'][0],
'last_name' => $user_meta['billing_last_name'][0],
'email' => $user_meta['billing_email'][0],
'phone' => $user_meta['billing_phone'][0],
'address_1' => $user_meta['billing_address_1'][0],
'address_2' => $user_meta['billing_address_1'][0],
'city' => $user_meta['billing_city'][0],
'state' => $user_meta['billing_state'][0],
'postcode' => $user_meta['billing_postcode'][0],
'country' => $user_meta['billing_country'][0],
);
// Now we create the order
$args = array( 'customer_note' => $user_id );
$order = wc_create_order( $args );
// The add_product() function below is located in /plugins/woocommerce/includes/abstracts/abstract_wc_order.php
$order->add_product( get_product('17'), 1);
$order->set_address( $address, 'billing' );
$order->calculate_totals();
$order->update_status("Completed", 'Imported order', TRUE);
// Create the entry in '{prefix}woocommerce_downloadable_product_permissions' table
wc_downloadable_product_permissions( $order->get_order_number() );
// Then change the user id from 0 to the required user.
global $wpdb;
$wpdb->update( $wpdb->prefix . 'woocommerce_downloadable_product_permissions', array('user_id'=> $user_id ), array( 'order_id' => $order->get_order_number() ), array( '%d' ), array( '%d' ) );
return '<p>Order created: '. $order->get_order_number(). '</p>';
}
array (
0 =>
array (
'download_url' => 'http://localhost/?download_file=17&order=wc_order_5a382bef811e6&email=damien@damiencarbery.com&key=7f7e4923ff94b7928c0b8b5e93fb4f89',
'download_id' => '7f7e4923ff94b7928c0b8b5e93fb4f89',
'product_id' => 17,
'download_name' => 'Download Product &ndash; RRA Water Outage',
'order_id' => 20,
'order_key' => 'wc_order_5a382bef811e6',
'downloads_remaining' => '',
'access_expires' => NULL,
'file' =>
array (
'name' => 'RRA Water Outage',
'file' => 'http://localhost/wp-content/uploads/woocommerce_uploads/2017/12/rra-water-outage-2017-12-19.png',
),
),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment