Skip to content

Instantly share code, notes, and snippets.

@chellestein
Created July 22, 2022 17:05
Show Gist options
  • Save chellestein/ec6461c38d6b3c85cdcd1915b6a0fa13 to your computer and use it in GitHub Desktop.
Save chellestein/ec6461c38d6b3c85cdcd1915b6a0fa13 to your computer and use it in GitHub Desktop.
Replace Woocommerce Add to Cart With Download Link for Paid Membership Pro Users
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', 'cs_woocommerce_template_single_add_to_cart', 30 );
/*
* replace WooCommerce add-to-cart button with download link when product is downloadable & free for PaidMembershipPro
*/
function cs_woocommerce_template_single_add_to_cart() {
global $product;
if ( pmpro_hasMembershipLevel(array('1','2'))) {
$downloads = $product->get_files();
foreach( $downloads as $key => $download ) {
echo '<div class="dlbox"><div class="download-link"><a class="dlbutton" href="' . esc_url( $download['file'] ) . '">Download File</a></div></div>';
}
} else {
do_action( 'woocommerce_' . $product->product_type . '_add_to_cart' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment