-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoo-lms.php
79 lines (73 loc) · 2.38 KB
/
woo-lms.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/*
Plugin Name: WooCommerce to Learn Dash Groups
Plugin URI:
Description: Add user to LearnDash Group if purchases specific WooCommerce product.
Version: 1.1.0
Author: TecGuru
Author URI: https://tecguru.co
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
//Triggered when a payment is made on a subscription. This can be payment for the initial order.
add_action( 'woocommerce_order_status_completed', 'woolms_add_user_to_group', 10, 1 );
/*
@woolms_add_user_to_group
*/
function woolms_add_user_to_group( $order_id ) {
// Add group ID from LearnDash here
$ld_group_id = 116113;
// Add product ID from WooCommerce here
$wc_product_id = 116115;
$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 );
}
}
}
/*
*@woocommerce_subscription_status_active
*/
add_action('woocommerce_subscription_status_active', 'woolms_add_user_to_groupa', 10, 1 );
function woolms_add_user_to_groupa($subscription){
$sub = $subscription->get_data();
$order_id = $sub['parent_id'];
// Add group ID from LearnDash here
$ld_group_id = 116113;
// Add product ID from WooCommerce here
$wc_product_id = 116115;
$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 );
}
}
}
/*
*@woocommerce_subscription_status_on-hold
*@woocommerce_subscription_status_cancelled
*@woocommerce_subscription_status_expired
*/
add_action('woocommerce_subscription_status_on-hold', 'woolms_remove_user_to_group', 10, 1 );
add_action('woocommerce_subscription_status_cancelled', 'woolms_remove_user_to_group', 10, 1 );
add_action('woocommerce_subscription_status_expired', 'woolms_remove_user_to_group', 10, 1 );
function woolms_remove_user_to_group($subscription){
$sub = $subscription->get_data();
$order_id = $sub['parent_id'];
// Add group ID from LearnDash here
$ld_group_id = 116113;
// Add product ID from WooCommerce here
$wc_product_id = 116115;
$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, true );
}
}
}
?>