Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Created January 8, 2020 08:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewlimaza/6384264008f01236ff2096bb1dd7fc5b to your computer and use it in GitHub Desktop.
Save andrewlimaza/6384264008f01236ff2096bb1dd7fc5b to your computer and use it in GitHub Desktop.
Give members a free WooCommerce downloadable Product [Paid Memberships Pro Snippet]
<?php
/**
* Give certain downloadable products to Paid Memberships Pro members for free.
* Adjust the code to your needs and to match your WooCommerce Product IDs and Membership Level IDs.
* Follow this guide to add the code to your WordPress website - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function free_download_product_for_members( $discount_price, $level_id, $original_price, $product ) {
$allowed_free_products = array( 58, 59, 100, 300, 501); // Adjust this with the WooCommerce Product ID's that are allowed to be discounted.
$allowed_levels = array( '1', '2' ); //Adjust this for membership levels that qualify for the discount.
// Check to see if product is in allowed product array.
if ( ! in_array( $product->get_id(), $allowed_free_products ) ) {
return $discount_price;
}
// If the product is set to downloadable and the user belongs to the right membership level, discount the price to free.
if ( $product->is_downloadable( 'yes' ) && pmpro_hasMembershipLevel( $allowed_levels ) ) {
$discount_price = 0;
}
return $discount_price;
}
add_filter( 'pmprowoo_get_membership_price', 'free_download_product_for_members', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment