PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.2
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.2
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 5.6.2 All 33 releases
automatorwp / integrations / fluentcart / includes / triggers / purchase-product.php

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

174 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Purchase Product
4 *
5 * @package AutomatorWP\Integrations\FluentCart\Triggers\Purchase_Product
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_FluentCart_Purchase_Product extends AutomatorWP_Integration_Trigger {
13
14 public $integration = 'fluentcart';
15 public $trigger = 'fluentcart_purchase_product';
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 a product', 'automatorwp' ),
27 'select_option' => __( 'User purchases <strong>a product</strong>', 'automatorwp' ),
28 /* translators: %1$s: Product title. %2$s: Number of times. */
29 'edit_label' => sprintf( __( 'User purchases %1$s %2$s time(s)', 'automatorwp' ), '{post}', '{times}' ),
30 /* translators: %1$s: Product title. */
31 'log_label' => sprintf( __( 'User purchases %1$s', 'automatorwp' ), '{post}' ),
32 'action' => 'fluent_cart/order_status_changed_to_completed',
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' => 'fluent-products'
41 ) ),
42 'times' => automatorwp_utilities_times_option(),
43 ),
44 'tags' => array_merge(
45 automatorwp_fluentcart_get_order_tags(),
46 automatorwp_utilities_post_tags( __( 'Product', 'automatorwp' ) ),
47 automatorwp_utilities_times_tag()
48 )
49 ) );
50
51 }
52
53 /**
54 * Trigger listener
55 *
56 * @since 1.0.0
57 *
58 * @param array $data
59 *
60 */
61 public function listener( $data ) {
62
63 $user_id = get_current_user_id();
64
65 // Bail if user is not logged
66 if ($user_id === 0) {
67 return;
68 }
69
70 $order = $data['order'];
71 $order_id = $order->id;
72
73 $items = $order->order_items;
74
75 // Loop all items to trigger events on each one purchased
76 foreach ( $items as $item ) {
77
78 $product_id = $item->post_id;
79 $quantity = $item->quantity;
80
81 // Skip items not assigned to a product
82 if( $product_id === 0 ) {
83 continue;
84 }
85
86 // Trigger events same times as item quantity
87 for ( $i = 0; $i < $quantity; $i++ ) {
88 // Trigger user product purchased
89 automatorwp_trigger_event( array(
90 'trigger' => $this->trigger,
91 'user_id' => $user_id,
92 'post_id' => $product_id,
93 'order_id' => $order_id,
94 'order_data' => $order,
95 ) );
96 }
97
98 }
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
168 return $log_meta;
169
170 }
171
172 }
173
174 new AutomatorWP_FluentCart_Purchase_Product();