Skip to content

Instantly share code, notes, and snippets.

@JarrydLong
Created February 7, 2023 08:41
Show Gist options
  • Save JarrydLong/efd719365b7be20b2e66817e24a276bc to your computer and use it in GitHub Desktop.
Save JarrydLong/efd719365b7be20b2e66817e24a276bc to your computer and use it in GitHub Desktop.
<?php //do not copy
/**
* This recipe will either add a fee or discount to specific variations of a Woo
* product based on the level that the user holds.
*
* In the below example, variation ID 107 should have level 1 and a fee of 1500 will be added.
* Variation ID 108 requires a level 2 and applies a discount of 8
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function mypmpro_adjusted_woo_variation_discounts( $discount_price, $lowest_price_level, $price, $product ) {
$pmpro_variation_levels = array(
//Set 107 to the variation ID, level to the level ID required, and the amount to ADD
107 => array( 'level' => 1, 'amount' => 1500 ),
//Set 108 to the variation ID, level to the level ID required, and the discount to DEDUCT
108 => array( 'level' => 2, 'discount' => 8 ),
);
if( $product->is_type( 'variation' ) ) {
$variation_id = $product->get_id();
if( isset( $pmpro_variation_levels[$variation_id] ) ) {
if( pmpro_hasMembershipLevel( $pmpro_variation_levels[$variation_id]['level'] ) ) {
if( isset( $pmpro_variation_levels[$variation_id]['amount'] ) ) {
return $price + $pmpro_variation_levels[$variation_id]['amount'];
} else if( isset( $pmpro_variation_levels[$variation_id]['discount'] ) ) {
return $price - $pmpro_variation_levels[$variation_id]['discount'];
}
}
}
}
return $discount_price;
}
add_filter( 'pmprowoo_get_membership_price', 'mypmpro_adjusted_woo_variation_discounts', 10, 4 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment