Last active
June 23, 2023 07:19
-
-
Save JarrydLong/2f3139ce45b46109852f3ce80d69634b to your computer and use it in GitHub Desktop.
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 //do not copy | |
/** | |
* This recipe associates a membership level with a Variable product's variation. | |
* You can specify that Variation A should get Level 1 and Variation B should get Level 2. | |
* | |
* Add variations/levels to the array in Line 31 | |
* | |
* Use of this code recipe requires that the PMPro WooCommerce Add On is active. | |
* | |
* 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/ | |
*/ | |
//Remove the default functionality. | |
function mypmpro_remove_default_add_membership_from_order(){ | |
remove_action( 'woocommerce_order_status_completed', 'pmprowoo_add_membership_from_order' ); | |
} | |
add_action( 'init','mypmpro_remove_default_add_membership_from_order' ); | |
/** | |
* Adds the membership based on the variation and level ID set out | |
* in $pmprowoo_variation_levels | |
*/ | |
function mypmprowoo_add_membership_from_order( $order_id ) { | |
global $wpdb, $pmprowoo_product_levels; | |
$variation_products = array( | |
3580 => 13 //3580 - Variation ID. 13 - Level ID | |
); | |
$pmprowoo_product_levels = array_merge( $variation_products, $pmprowoo_product_levels ); | |
// quitely exit if PMPro isn't active | |
if ( ! defined( 'PMPRO_DIR' ) && ! function_exists( 'pmpro_init' ) ) { | |
return; | |
} | |
//don't bother if array is empty | |
if ( empty( $pmprowoo_product_levels ) ) { | |
return; | |
} | |
/* | |
does this order contain a membership product? | |
*/ | |
//get order | |
$order = new WC_Order( $order_id ); | |
//does the order have a user id and some products? | |
$user_id = $order->get_user_id(); | |
if ( ! empty( $user_id ) && sizeof( $order->get_items() ) > 0 ) { | |
foreach ( $order->get_items() as $item ) { | |
$_product = $item->get_product(); | |
$product_id = $_product->get_id(); | |
if ( ! empty( $product_id ) && ! empty( $pmprowoo_product_levels[ $product_id ] ) ) { | |
//is there a membership level for this product? | |
//get user id and level | |
$pmpro_level = pmpro_getLevel( $pmprowoo_product_levels[ $product_id ] ); | |
//if checking out for the same level they have, keep their old start date | |
$sqlQuery = $wpdb->prepare( | |
"SELECT startdate | |
FROM {$wpdb->pmpro_memberships_users} | |
WHERE user_id = %d | |
AND membership_id = %d | |
AND status = 'active' | |
ORDER BY id DESC | |
LIMIT 1", | |
$user_id, | |
$pmpro_level->id | |
); | |
$old_startdate = $wpdb->get_var( $sqlQuery ); | |
if ( ! empty( $old_startdate ) ) { | |
$startdate = "'" . $old_startdate . "'"; | |
} else { | |
$startdate = "'" . current_time( 'mysql' ) . "'"; | |
} | |
//create custom level to mimic PMPro checkout | |
$custom_level = array( | |
'user_id' => $user_id, | |
'membership_id' => $pmpro_level->id, | |
'code_id' => '', //will support PMPro discount codes later | |
'initial_payment' => $item['line_total'], | |
'billing_amount' => '', | |
'cycle_number' => '', | |
'cycle_period' => '', | |
'billing_limit' => '', | |
'trial_amount' => '', | |
'trial_limit' => '', | |
'startdate' => $startdate, | |
'enddate' => '0000-00-00 00:00:00', | |
); | |
//set enddate | |
if ( ! empty( $pmpro_level->expiration_number ) ) { | |
$custom_level['enddate'] = date( "Y-m-d H:i:00", strtotime( "+ " . $pmpro_level->expiration_number . " " . $pmpro_level->expiration_period, current_time( 'timestamp' ) ) ); | |
} | |
/** | |
* Filter for the level object. | |
*/ | |
$custom_level = apply_filters( 'pmprowoo_checkout_level', $custom_level ); | |
// Is MMPU activated? | |
if ( function_exists( 'pmprommpu_addMembershipLevel' ) ) { | |
// Allow filter to force add levels (ignore MMPU group level settings). | |
$mmpu_force_add_level = apply_filters( 'pmprowoo_mmpu_force_add_level', false ); | |
pmprommpu_addMembershipLevel( $custom_level, $user_id, $mmpu_force_add_level ); | |
} else { | |
// Only add the first membership level found. | |
pmpro_changeMembershipLevel( $custom_level, $user_id ); | |
break; | |
} | |
} | |
} | |
} | |
} | |
add_action( 'woocommerce_order_status_completed', 'mypmprowoo_add_membership_from_order' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment