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 / studiocart / includes / triggers / purchase-product.php

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

169 lines 5.8 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\Studiocart\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_Studiocart_Purchase_Product extends AutomatorWP_Integration_Trigger {
13
14 public $integration = 'studiocart';
15 public $trigger = 'studiocart_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' => 'sc_order_complete',
33 'function' => array( $this, 'listener' ),
34 'priority' => 10,
35 'accepted_args' => 3,
36 'options' => array(
37 'post' => automatorwp_utilities_post_option( array(
38 'name' => __( 'Product:', 'automatorwp' ),
39 'option_none_label' => __( 'any product', 'automatorwp' ),
40 'post_type' => 'sc_product'
41 ) ),
42 'times' => automatorwp_utilities_times_option(),
43 ),
44 'tags' => array_merge(
45 automatorwp_utilities_post_tags( __( 'Product', 'automatorwp' ) ),
46 automatorwp_studiocart_order_tags(),
47 automatorwp_studiocart_product_tags(),
48 automatorwp_utilities_times_tag()
49 )
50 ) );
51
52 }
53
54 /**
55 * Trigger listener
56 *
57 * @since 1.0.0
58 *
59 * @param string $status
60 * @param array $order_data
61 * @param string $order_type
62 *
63 */
64 public function listener( $status, $order_data, $order_type ) {
65
66 // Bail if can not find any data
67 if ( $status !== 'paid' ) {
68 return;
69 }
70
71 $user_id = get_current_user_id();
72
73 // Bail if user is not logged
74 if ($user_id === 0) {
75 return;
76 }
77
78 // Trigger user product purchased
79 automatorwp_trigger_event( array(
80 'trigger' => $this->trigger,
81 'user_id' => $user_id,
82 'post_id' => $order_data['product_id'],
83 'order_id' => $order_data['ID'],
84 'order_amount' => $order_data['amount'],
85 'product_id' => $order_data['product_id'],
86 'product_name' => $order_data['product_name'],
87 ) );
88
89 }
90
91 /**
92 * User deserves check
93 *
94 * @since 1.0.0
95 *
96 * @param bool $deserves_trigger True if user deserves trigger, false otherwise
97 * @param stdClass $trigger The trigger object
98 * @param int $user_id The user ID
99 * @param array $event Event information
100 * @param array $trigger_options The trigger's stored options
101 * @param stdClass $automation The trigger's automation object
102 *
103 * @return bool True if user deserves trigger, false otherwise
104 */
105
106
107 public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) {
108
109 // Don't deserve if post is not received
110 if( ! isset( $event['post_id'] ) ) {
111 return false;
112 }
113
114 // Don't deserve if post doesn't match with the trigger option
115 if( ! automatorwp_posts_matches( $event['post_id'], $trigger_options['post'] ) ) {
116 return false;
117 }
118
119 return $deserves_trigger;
120
121 }
122
123 /**
124 * Register the required hooks
125 *
126 * @since 1.0.0
127 */
128 public function hooks() {
129
130 // Log meta data
131 add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 );
132
133 parent::hooks();
134 }
135
136 /**
137 * Trigger custom log meta
138 *
139 * @since 1.0.0
140 *
141 * @param array $log_meta Log meta data
142 * @param stdClass $trigger The trigger object
143 * @param int $user_id The user ID
144 * @param array $event Event information
145 * @param array $trigger_options The trigger's stored options
146 * @param stdClass $automation The trigger's automation object
147 *
148 * @return array
149 */
150 function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) {
151
152 // Bail if action type don't match this action
153 if( $trigger->type !== $this->trigger ) {
154 return $log_meta;
155 }
156
157 $log_meta['order_id'] = ( isset( $event['order_id'] ) ? $event['order_id'] : 0 );
158 $log_meta['order_amount'] = ( isset( $event['order_amount'] ) ? $event['order_amount'] : 0.00 );
159 $log_meta['product_id'] = ( isset( $event['product_id'] ) ? $event['product_id'] : 0 );
160 $log_meta['product_name'] = ( isset( $event['product_name'] ) ? $event['product_name'] : '' );
161
162 return $log_meta;
163
164 }
165
166 }
167
168
169 new AutomatorWP_Studiocart_Purchase_Product();