Skip to content

Instantly share code, notes, and snippets.

@jmabbas
Created October 13, 2026 08:48
Show Gist options
  • Select an option

  • Save jmabbas/03db3e1135e1cdb9d5525d052715c2b3 to your computer and use it in GitHub Desktop.

Select an option

Save jmabbas/03db3e1135e1cdb9d5525d052715c2b3 to your computer and use it in GitHub Desktop.
Electro - Single product custom download button
/**
* 1. Add custom Download Link and Button Text fields on single product edit page.
*/
add_action( 'woocommerce_product_options_general_product_data', 'ec_child_add_download_button_fields' );
function ec_child_add_download_button_fields() {
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => '_custom_download_button_text',
'label' => __( 'Download Button Text', 'electro-child' ),
'placeholder' => __( 'Download', 'electro-child' ),
'desc_tip' => false,
'description' => __( 'Enter custom label for the download button.', 'electro-child' ),
)
);
woocommerce_wp_text_input(
array(
'id' => '_custom_download_button_url',
'label' => __( 'Download Button URL', 'electro-child' ),
'placeholder' => 'https://...',
'desc_tip' => false,
'description' => __( 'Enter the custom link/URL for the download button.', 'electro-child' ),
'type' => 'url',
)
);
echo '</div>';
}
/**
* Save custom fields when product is updated/saved.
*
* @param int $post_id Product post ID.
*/
add_action( 'woocommerce_process_product_meta', 'ec_child_save_download_button_fields' );
function ec_child_save_download_button_fields( $post_id ) {
if ( isset( $_POST['_custom_download_button_text'] ) ) {
update_post_meta( $post_id, '_custom_download_button_text', sanitize_text_field( wp_unslash( $_POST['_custom_download_button_text'] ) ) );
}
if ( isset( $_POST['_custom_download_button_url'] ) ) {
update_post_meta( $post_id, '_custom_download_button_url', esc_url_raw( wp_unslash( $_POST['_custom_download_button_url'] ) ) );
}
}
/**
* 2. Load button on single product frontend.
*/
add_action( 'init', 'ec_child_single_product_custom_button', 10 );
function ec_child_single_product_custom_button() {
add_action( 'woocommerce_single_product_summary', 'single_product_custom_button_after_add_to_cart', 31 );
}
function single_product_custom_button_after_add_to_cart() {
global $product;
if ( ! is_a( $product, 'WC_Product' ) ) {
$product = wc_get_product( get_the_ID() );
}
if ( ! $product ) {
return;
}
$button_url = get_post_meta( $product->get_id(), '_custom_download_button_url', true );
$button_text = get_post_meta( $product->get_id(), '_custom_download_button_text', true );
// Only display if a download link is provided.
if ( empty( $button_url ) ) {
return;
}
if ( empty( $button_text ) ) {
$button_text = __( 'Download', 'electro-child' );
}
?>
<div class="custom-button custom-download-button">
<a href="<?php echo esc_url( $button_url ); ?>" class="button" target="_blank" rel="noopener noreferrer">
<?php echo esc_html( $button_text ); ?>
</a>
</div>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment