Created
July 11, 2021 17:25
-
-
Save jorpdesigns/1fdfbe11f3e323b30b27944d8339dc02 to your computer and use it in GitHub Desktop.
Function that returns upsells of products that grants access to a user's WooCommerce Membership plan
This file contains hidden or 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 | |
function get_year_group_upsells() { | |
$user_id = get_current_user_id(); | |
$args = array( | |
'status' => array( 'active', 'pending-cancellation' ), // Add or remove other statuses if you want | |
); | |
// Get user memberships | |
$active_memberships = wc_memberships_get_user_memberships( $user_id, $args ); | |
if ( ! empty( $active_memberships ) ) { | |
$membershipPlansArray = array(); | |
$upsellIDsArray = array(); | |
// Get user membership plans | |
foreach( $active_memberships as $membership ) { | |
$membershipPlansArray[] = $membership->plan; | |
} | |
// Get user membership plan product ids | |
foreach( $membershipPlansArray as $membershipPlan ) { | |
$productIDsArray = get_post_meta( $membershipPlan->id, '_product_ids', true); | |
if ( $productIDsArray ) { | |
// Get product upsells | |
foreach( $productIDsArray as $productID ) { | |
$productUpsellsArray = get_post_meta( $productID, '_upsell_ids', true ); | |
if ( $productUpsellsArray ) { | |
$upsellIDsArray = array_unique( array_merge( $productUpsellsArray, $upsellIDsArray )); | |
} | |
} | |
} | |
} | |
} else { | |
$upsellIDsArray = false; | |
} | |
return $upsellIDsArray; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment