Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save andrewlimaza/b71bf6db01bd42dcddadc3e78cf3b62d to your computer and use it in GitHub Desktop.
Save andrewlimaza/b71bf6db01bd42dcddadc3e78cf3b62d to your computer and use it in GitHub Desktop.
Exclude membership discount for products in a certain category for WooCommerce and Paid Memberships Pro.
<?php
/**
* This will exclude products that belong to a specific category from the membership discount.
* Add the below code to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_exclude_woocommerce_discounts_for_categories( $price, $level_id, $original_price, $product ) {
// Array of categories to exclude, uses category slug.
$exclude_categories = array( 'category-1', 'category-2', 'category-3' );
// Figure out if product is a variation or simple product.
if ( $product->get_type() === 'variation' ) {
$product_id = $product->get_parent_id();
} else {
$product_id = $product->get_id();
}
$category_data = wp_get_post_terms( $product_id, 'product_cat' );
// Check if product belongs to a category and remove discounted pricing.
foreach( $category_data as $key => $value ) {
if ( in_array( $value->slug, $exclude_categories ) ) {
$price = $original_price;
}
}
return $price;
}
add_filter( 'pmprowoo_get_membership_price', 'my_pmpro_exclude_woocommerce_discounts_for_categories', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment