Last active
July 12, 2019 11:35
-
-
Save bekarice/4c97a60e6d70611be8f2 to your computer and use it in GitHub Desktop.
WooCommerce: Add an order action to download custom order files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // only copy this line if needed! | |
/** | |
* Adds a custom order action in the "Recent Orders" table of the WooCommerce account | |
* if a download ID is entered as a "custom_file" order custom field | |
* Button downloads custom files for the order | |
* Requires Download Monitor | |
* | |
* @param array $actions the actions available for the order | |
* @param \WC_Order $order the order object for this row | |
* @return array the updated order actions | |
*/ | |
function sww_add_custom_file_order_action( $actions, $order ) { | |
// only add our button if the order is paid for\ | |
if ( ! $order->is_paid() ) { | |
return $actions; | |
} | |
$order_id = is_callable( array( $order, 'get_id' ) ) ? $order->get_id() : $order->id; | |
// add our action if the order has the custom_file field set | |
if ( $file_id = (int) get_post_meta( $order_id, 'custom_file', true ) ) { | |
$actions['files'] = array( | |
'url' => trailingslashit( get_site_url() ) . trailingslashit( get_option( 'dlm_download_endpoint' ) ) . $file_id, | |
'name' => __( 'Get Files', 'my-textdomain' ), | |
); | |
} | |
return $actions; | |
} | |
add_filter( 'woocommerce_my_account_my_orders_actions', 'sww_add_custom_file_order_action', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Bekarice,
is your code still working ? Because I've tried it at so many positions in the templates and it does not work :(
greetings from germany, michelle