Forked from andrewlimaza/exclude-discount-woocommerce-categories.php
Created
July 23, 2019 11:31
-
-
Save travislima/710cd81d0607f7f857ea805ea7d5cbd6 to your computer and use it in GitHub Desktop.
Exclude membership discount for products in a certain category for WooCommerce and Paid Memberships Pro.
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
<?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
This recipe is included in the blog post on "Exclude Certain WooCommerce Products from Membership Discount | Paid Memberships Pro" at Paid Memberships Pro here: https://www.paidmembershipspro.com/exclude-certain-woocommerce-products-from-membership-discount/