class-aw-subscriptions-integration.php
5.21 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
*
*
* @package WC_Active Woo Integration
* @category Integration
* @author Jason Kadlec http://crazyeffective.com
*/
if ( ! class_exists( 'AW_WC_Subscription_Integration' ) ) :
class AW_WC_Subscription_Integration{
/**
* Set up the class, including it's hooks & filters, when the file is loaded.
*
**/
public static function init() {
$subsSettings = WC_Admin_Settings::get_option( 'woocommerce_active-woo_settings' );
if (isset($subsSettings['wc_subs_sync'])) {
$subsSettings = $subsSettings['wc_subs_sync'];
}
$subscription_version = (int) WC_Subscriptions::$version;
// exit if Woo Subscription Sync is disabled
if ( ! 'yes' == $subsSettings ) {
return;
}
/**
* Show notice if Subscriptions version is lower than 2
*/
if ( $subscription_version < 2 ) {
add_action('admin_notices', __CLASS__ . '::version_notice' );
return;
}
// add skuCANC tag
add_action( 'woocommerce_subscription_status_cancelled', __CLASS__ . '::add_tag_skuCANC' );
// add skuCOMPLETE tag
add_action( 'woocommerce_subscription_status_expired', __CLASS__ . '::add_tag_skuCOMPLETE' );
// add skuSUSP tag
add_action( 'woocommerce_subscription_status_on-hold', __CLASS__ . '::add_tag_skuSUSP' );
// add skuTrialEnd tag
add_action( 'woocommerce_scheduled_subscription_trial_end', __CLASS__ . '::add_tag_skuTrialEnd' );
// add skuPAYF tag
add_action( 'woocommerce_subscription_payment_failed', __CLASS__ . '::add_tag_skuPAYF' );
// add skuCANC tag
add_action( 'subscriptions_cancelled_for_order', __CLASS__ . '::add_tag_skuCANC' );
// // add skuCANC tag
add_action( 'subscriptions_expired_for_order', __CLASS__ . '::add_tag_skuCANC' );
}
public static function version_notice(){
$notice = '';
$notice .= '<div class="notice notice-error is-dismissible"> ';
$notice .= '<p><strong>You must update Subscriptions to use the ActiveWoo Subscription sync feature.</strong></p>';
$notice .= '<button type="button" class="notice-dismiss">';
$notice .= '<span class="screen-reader-text">Dismiss this notice.</span>';
$notice .= '</button>';
$notice .= '</div>';
echo $notice;
}
/**
* Saves required tags to ActiveCampaign Contact
* @param WC_Order|int $order The order or ID of the order for which subscriptions should be marked as cancelled.
* @param String tag suffix to add before SKU of Products
*/
public static function save_tags( $order, $suffix ) {
// Get WC_Order if $order param isn't a object of WC_Order
if ( ! $order instanceof WC_Order ) {
$order = new WC_Order( $order );
}
$items = $order->get_items();
$email = $order->billing_email;
$tags = array();
foreach ($items as $item) { //loop through the items in the purchase
$product = new WC_Product($item['product_id']);
$tags[] = $product->get_sku() . $suffix ;
}
$awc = new WC_Integration_Active_Woo();
$ac = new aw_ActiveCampaign( $awc->api_url, $awc->api_key );
$data = array( 'email' => $email, 'tags' => $tags );
$response = $ac->api("contact/tag/add", $data);
if ( $response->success ) {
// save the tags to post meta
update_post_meta ( $order->id, 'aw_subs_tags', $tags );
}
}
/**
* To add tag with CANC suffix
* @param WC_Order|WC_subscription $subscription depends based on triggered by which Action
*/
public static function add_tag_skuCANC( $subscription ) {
// check if $subscription a WC_Order object
// may triggered by subscriptions_expired_for_order and
// subscriptions_expired_for_order action
if ( $subscription instanceof WC_Order ) {
// WC_Order
self::save_tags( $subscription, "CANC" );
}elseif( $subscription instanceof WC_Subscription ){
// WC_Subscription
self::save_tags( $subscription->order->id, "CANC" );
}else{
// order id
self::save_tags( $subscription, "CANC" );
}
}
/**
* To add tag with COMPLETE suffix
* @param WC_subscription $subscription
*/
public static function add_tag_skuCOMPLETE( $subscription ) {
self::save_tags( $subscription->order->id, "COMPLETE" );
}
/**
* To add tag with SUSP suffix
* @param WC_subscription $subscription
*/
public static function add_tag_skuSUSP( $subscription ) {
self::save_tags( $subscription->order->id, "SUSP" );
}
/**
* To add tag with TrialEnd suffix
* @param WC_subscription $subscription
*/
public static function add_tag_skuTrialEnd( $subscription ) {
self::save_tags( $subscription->order->id, "TrialEnd" );
}
/**
* To add tag with PAYF suffix
* @param WC_subscription $subscription
*/
public static function add_tag_skuPAYF( $subscription ) {
self::save_tags( $subscription->order->id, "PAYF" );
}
} // endif
endif;