Skip to content

Instantly share code, notes, and snippets.

@mahdi-alavi
Last active July 24, 2022 19:12
Show Gist options
  • Save mahdi-alavi/9d7752572fed9cedc8b10c6876d1e86a to your computer and use it in GitHub Desktop.
Save mahdi-alavi/9d7752572fed9cedc8b10c6876d1e86a to your computer and use it in GitHub Desktop.
replace WooCommerce add-to-cart button with download link when product is downloadable & free
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'itl_woocommerce_template_single_add_to_cart', 30 );
/*
* replace WooCommerce add-to-cart button with download link when product is downloadable & free
*/
function itl_woocommerce_template_single_add_to_cart() {
global $product;
if ( $product->is_downloadable('yes') ) {
if ( $product->get_price() > 0 ) {
do_action( 'woocommerce_' . $product->product_type . '_add_to_cart' );
} else {
$downloads = $product->get_files();
foreach( $downloads as $key => $download ) {
echo '<p class="download-link"><a href="' . esc_url( $download['file'] ) . '">' . $download['name'] . '</a></p>';
}
}
} else {
do_action( 'woocommerce_' . $product->product_type . '_add_to_cart' );
}
}
@Covid-the-21
Copy link

what if we want the "add the cart" button turn to download button for the paid membership.

and how can we limit the download per day for all membership plans, like 50 per day.

i am working on woocommerce by the way.

@beatrixsloan
Copy link

Hi where should i insert this code ?

@mauvisit
Copy link

Hi where should i insert this code ?

it should be function.php in your child theme

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