Skip to content

Instantly share code, notes, and snippets.

@RichardNesbitt
Created December 6, 2020 08:52
Show Gist options
  • Save RichardNesbitt/b26962b6ddfc2cc814b63aaf87adc597 to your computer and use it in GitHub Desktop.
Save RichardNesbitt/b26962b6ddfc2cc814b63aaf87adc597 to your computer and use it in GitHub Desktop.
Add user to LearnDash groups when they purchase products.
<?php
function jsforwp_add_user_to_group( $order_id ) {
//$group_rules contains settings in the format of product_id => array(groupid, groupid, groupid)
$group_rules = array(
26325 => array(26346, 26348, 26349),
26329 => array(26346, 26348, 26349),
26327 => array(26351, 26353, 26354),
23661 => array(26351, 26353, 26354),
26138 => array(26359),
26102 => array(26356)
);
$order = new WC_Order( $order_id );
$items = $order->get_items();
//for each item in the order
foreach ( $items as $item ) {
//if the items product_id is in the rules array
if ( array_key_exists( $item['product_id'], $group_rules ) ) {
//for each groupid in the array
foreach( $group_rules[$item['product_id']] as $ld_group_id ) {
//add the user to that group
ld_update_group_access( $order->customer_id, $ld_group_id, false );
}
}
}
} add_action( 'woocommerce_order_status_completed', 'jsforwp_add_user_to_group', 10, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment