Last active
July 24, 2022 19:12
-
-
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
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
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' ); | |
} | |
} |
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
Hi where should i insert this code ?