PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 5.8.5
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v5.8.5
6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 All 34 releases
automatorwp / integrations / woocommerce / includes / triggers / purchase-subscription.php

purchase-subscription.php in AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI 5.8.5, at integrations/woocommerce/includes/triggers/purchase-subscription.php

175 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Purchase Subscription
4 *
5 * @package AutomatorWP\Integrations\WooCommerce\Triggers\Purchase_Subscription
6 * @author AutomatorWP <[email protected]>, Ruben Garcia <[email protected]>
7 * @since 1.0.0
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 class AutomatorWP_WooCommerce_Purchase_Subscription extends AutomatorWP_Integration_Trigger {
13
14 public $integration = 'woocommerce';
15 public $trigger = 'woocommerce_purchase_subscription';
16
17 /**
18 * Register the trigger
19 *
20 * @since 1.0.0
21 */
22 public function register() {
23
24 automatorwp_register_trigger( $this->trigger, array(
25 'integration' => $this->integration,
26 'label' => __( 'User purchases subscription of a product', 'automatorwp' ),
27 'select_option' => __( 'User <strong>purchases</strong> subscription of a product', 'automatorwp' ),
28 /* translators: %1$s: Post title. %2$s: Number of times. */
29 'edit_label' => sprintf( __( 'User purchases subscription of %1$s %2$s time(s)', 'automatorwp' ), '{post}', '{times}' ),
30 /* translators: %1$s: Post title. */
31 'log_label' => sprintf( __( 'User purchases subscription of %1$s', 'automatorwp' ), '{post}' ),
32 'action' => 'woocommerce_subscription_payment_complete',
33 'function' => array( $this, 'listener' ),
34 'priority' => 10,
35 'accepted_args' => 1,
36 'options' => array(
37 'post' => automatorwp_utilities_post_option( array(
38 'name' => __( 'Product:', 'automatorwp' ),
39 'option_none_label' => __( 'any product', 'automatorwp' ),
40 'post_type' => 'product'
41 ) ),
42 'times' => automatorwp_utilities_times_option(),
43 ),
44 'tags' => array_merge(
45 automatorwp_utilities_post_tags( __( 'Product', 'automatorwp' ) ),
46 automatorwp_woocommerce_order_tags(),
47 automatorwp_utilities_times_tag()
48 )
49 ) );
50
51 }
52
53 /**
54 * Trigger listener
55 *
56 * @since 1.0.0
57 *
58 * @param WC_Subscription $subscription
59 */
60 public function listener( $subscription ) {
61
62 $items = $subscription->get_items();
63
64 // Bail if no items purchased
65 if ( ! is_array( $items ) ) {
66 return;
67 }
68
69 $order_total = $subscription->get_total();
70 $user_id = $subscription->get_user_id();
71
72 // Loop all items to trigger events on each one purchased
73 foreach ( $items as $item ) {
74
75 $product_id = $item->get_product_id();
76 $quantity = $item->get_quantity();
77
78 // Skip items not assigned to a product
79 if( $product_id === 0 ) {
80 continue;
81 }
82
83 // Trigger events same times as item quantity
84 for ( $i = 0; $i < $quantity; $i++ ) {
85
86 // Trigger the product subscription purchase
87 automatorwp_trigger_event( array(
88 'trigger' => $this->trigger,
89 'user_id' => $user_id,
90 'post_id' => $product_id,
91 'order_id' => $subscription->get_id(),
92 'subscription_id' => $subscription->get_id(),
93 'order_total' => $order_total,
94 ) );
95
96 } // End for of quantities
97
98 } // End foreach of items
99
100 }
101
102 /**
103 * User deserves check
104 *
105 * @since 1.0.0
106 *
107 * @param bool $deserves_trigger True if user deserves trigger, false otherwise
108 * @param stdClass $trigger The trigger object
109 * @param int $user_id The user ID
110 * @param array $event Event information
111 * @param array $trigger_options The trigger's stored options
112 * @param stdClass $automation The trigger's automation object
113 *
114 * @return bool True if user deserves trigger, false otherwise
115 */
116 public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) {
117
118 // Don't deserve if post is not received
119 if( ! isset( $event['post_id'] ) ) {
120 return false;
121 }
122
123 // Don't deserve if post doesn't match with the trigger option
124 if( ! automatorwp_posts_matches( $event['post_id'], $trigger_options['post'] ) ) {
125 return false;
126 }
127
128 return $deserves_trigger;
129
130 }
131
132 /**
133 * Register the required hooks
134 *
135 * @since 1.0.0
136 */
137 public function hooks() {
138
139 // Log meta data
140 add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 );
141
142 parent::hooks();
143 }
144
145 /**
146 * Trigger custom log meta
147 *
148 * @since 1.0.0
149 *
150 * @param array $log_meta Log meta data
151 * @param stdClass $trigger The trigger object
152 * @param int $user_id The user ID
153 * @param array $event Event information
154 * @param array $trigger_options The trigger's stored options
155 * @param stdClass $automation The trigger's automation object
156 *
157 * @return array
158 */
159 function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) {
160
161 // Bail if action type don't match this action
162 if( $trigger->type !== $this->trigger ) {
163 return $log_meta;
164 }
165
166 $log_meta['order_id'] = ( isset( $event['order_id'] ) ? $event['order_id'] : 0 );
167 $log_meta['subscription_id'] = ( isset( $event['subscription_id'] ) ? $event['subscription_id'] : 0 );
168
169 return $log_meta;
170
171 }
172
173 }
174
175 new AutomatorWP_WooCommerce_Purchase_Subscription();