Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WooCommerce - add downloadable files to existing orders WordPress

  • SOLVED

Hey guys,

I recently ran a pre-order with WooCommerce Pre-Orders that has now completed, and customers' order statuses have been automatically changed from "pre-ordered" to "processing".

The pre-ordered products are physical and digital, but previously when I added a digital file to pre-orders, customers were able to download it immediately rather than on the pre-order completion date. I intended to add the digital files upon the pre-order completing, but updating the products that were available in the pre-order does not grant the customers access to the files in their order.

Therefore, I need a way of updating all of the pre-orders so that the customers have access to the files. I'm not sure how to do this

Answers (2)

2018-11-02

Reigel Gallarde answers:

are you using this one?
https://woocommerce.com/products/woocommerce-pre-orders/


Dan Davies comments:

Yup!


Reigel Gallarde comments:

Does the products on an order already have a set downloadable items?
If so, you have to click the option under "Order Actions" and choose "Regenerate download permissions" then click the arrow button next to it.


Reigel Gallarde comments:

If that will work, you can then get all the order id, loop through each, ang regenerate permission...

$data_store = WC_Data_Store::load( 'customer-download' );
$data_store->delete_by_order_id( $order_id );
wc_downloadable_product_permissions( $order_id , true );


Dan Davies comments:

There are 400+ orders to do this for. Can this be done in bulk?


Reigel Gallarde comments:

you can try something like this...
This will loop through all orders that as pre-ordered status and regenerates the download permission.

Please have a backup of your database or use this code on a test website first.
You can also test this against a dummy user using customer_id

$args = array(
'status' => 'pre-ordered', // change this if needed.
'customer_id' => 123, // only use this to test against a customer
'return' => 'ids'
);
$orders = wc_get_orders( $args );

$data_store = WC_Data_Store::load( 'customer-download' );
foreach ( $orders as $order_id ) {
$data_store->delete_by_order_id( $order_id );
wc_downloadable_product_permissions( $order_id , true );
}

2018-11-02

Rempty answers:

Hello
Maybe this code can help

function add_onhold_status_to_download_permission($data, $order) {
if ( $order->has_status( 'on-hold' ) ) { return true; }
return $data;
}
add_filter('woocommerce_order_is_download_permitted', 'add_onhold_status_to_download_permission', 10, 2);


where you must change on-hold to the status you want.

code from here
https://stackoverflow.com/questions/37938239/woocommerce-give-user-access-to-downloadable-files-when-the-status-is-on-hold