Skip to content

Instantly share code, notes, and snippets.

@zgordon
Last active August 21, 2019 00:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zgordon/adecb8351f20c68a55c2ee55dbc3db22 to your computer and use it in GitHub Desktop.
Save zgordon/adecb8351f20c68a55c2ee55dbc3db22 to your computer and use it in GitHub Desktop.
Assign users to a LearnDash Group when they purchase a specific WooCommerce product
<?php
// Add user to LearnDash Group if purchases specific WooCommerce product
function jsforwp_add_user_to_group( $order_id ) {
// Add group ID from LearnDash here
$ld_group_id = XXXX;
// Add product ID from WooCommerce here
$wc_product_id = XXXX;
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
if ( $wc_product_id == $item['product_id'] ) {
ld_update_group_access( $order->customer_id, $ld_group_id, false );
}
}
}
add_action( 'woocommerce_order_status_completed', 'jsforwp_add_user_to_group', 10, 1 );
?>
@rkm82
Copy link

rkm82 commented Aug 20, 2019

excatly what I was looking for - but now how do I use this file? :-) Thanks for clarifying!

@rkm82
Copy link

rkm82 commented Aug 21, 2019

Oh - nevermind - found it in your post here:
https://wp.zacgordon.com/2017/05/10/add-user-to-learndash-group-when-orders-a-woocommerce-product/

:-) Thanks so much for creating this!
Cheers,
Rob

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment