Skip to content

Instantly share code, notes, and snippets.

@bekarice
Last active August 21, 2021 04:54
Show Gist options
  • Save bekarice/d8c3b7dc75b06b0d94e4 to your computer and use it in GitHub Desktop.
Save bekarice/d8c3b7dc75b06b0d94e4 to your computer and use it in GitHub Desktop.
WooCommerce: Add a list of custom files for an order (uses Download Monitor shortcode)
<?php // only copy this line if needed!
/**
* Adds a section with custom downloadable files to the "View Order Screen"
* if the order has a "custom_file" custom field with a download ID
* Requires Download Monitor
*
* SEE https://jilt.com/blog/selling-custom-files-woocommerce/
*
* @param int $order_id the ID of the order being viewed
*/
function sww_add_custom_file_view_order( $order_id ) {
$order = wc_get_order( $order_id );
// only add our section if the order is paid for
if ( ! $order->is_paid() ) {
return;
}
$order_id = is_callable( array( $order, 'get_id' ) ) ? $order->get_id() : $order->id;
// add our section to shown download links if the order has the custom_file field set
if ( $file_id = (int) get_post_meta( $order_id, 'custom_file', true ) ) {
$content = '<h3>Custom Files</h3>';
$content .= do_shortcode( '[download id="' . $file_id . '"]' );
echo $content;
}
}
add_action( 'woocommerce_view_order', 'sww_add_custom_file_view_order', 9 );
@bekarice
Copy link
Author

@alexlii1971 this snippet isn't functional solely on its own — check out this blog post, which is what it was written for!

@ermkmandawar
Copy link

@bekarice This code allows adding the custom file in Order tab. It work successfully. But what should I do for showing the same #file in Download tab instead of Order. Please share your answer. I'll be thankful to you. #

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment