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-product.php

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

186 lines 6.0 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\WooCommerce\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_WooCommerce_Purchase_Product extends AutomatorWP_Integration_Trigger {
13
14 public $integration = 'woocommerce';
15 public $trigger = 'woocommerce_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: Post 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: Post title. */
31 'log_label' => sprintf( __( 'User purchases %1$s', 'automatorwp' ), '{post}' ),
32 'action' => 'woocommerce_order_status_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' => '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 int $order_id The order ID
59 */
60 public function listener( $order_id ) {
61
62 $order = wc_get_order( $order_id );
63
64 // Bail if not a valid order
65 if( ! $order ) {
66 return;
67 }
68
69 // Bail if order is not marked as completed
70 if ( $order->get_status() !== 'completed' ) {
71 return;
72 }
73
74 $items = $order->get_items();
75
76 // Bail if no items purchased
77 if ( ! is_array( $items ) ) {
78 return;
79 }
80
81 $order_total = $order->get_total();
82 $user_id = $order->get_user_id();
83
84 // Loop all items to trigger events on each one purchased
85 foreach ( $items as $item ) {
86
87 $product_id = $item->get_product_id();
88 $quantity = $item->get_quantity();
89
90 // Skip items not assigned to a product
91 if( $product_id === 0 ) {
92 continue;
93 }
94
95
96 // Trigger events same times as item quantity
97 for ( $i = 0; $i < $quantity; $i++ ) {
98
99 // Trigger the product purchase
100 automatorwp_trigger_event( array(
101 'trigger' => $this->trigger,
102 'user_id' => $user_id,
103 'post_id' => $product_id,
104 'order_id' => $order_id,
105 'order_total' => $order_total,
106 ) );
107
108 } // End for of quantities
109
110 } // End foreach of items
111
112 }
113
114 /**
115 * User deserves check
116 *
117 * @since 1.0.0
118 *
119 * @param bool $deserves_trigger True if user deserves trigger, false otherwise
120 * @param stdClass $trigger The trigger object
121 * @param int $user_id The user ID
122 * @param array $event Event information
123 * @param array $trigger_options The trigger's stored options
124 * @param stdClass $automation The trigger's automation object
125 *
126 * @return bool True if user deserves trigger, false otherwise
127 */
128 public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) {
129
130 // Don't deserve if post is not received
131 if( ! isset( $event['post_id'] ) ) {
132 return false;
133 }
134
135 // Don't deserve if post doesn't match with the trigger option
136 if( ! automatorwp_posts_matches( $event['post_id'], $trigger_options['post'] ) ) {
137 return false;
138 }
139
140 return $deserves_trigger;
141
142 }
143
144 /**
145 * Register the required hooks
146 *
147 * @since 1.0.0
148 */
149 public function hooks() {
150
151 // Log meta data
152 add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 );
153
154 parent::hooks();
155 }
156
157 /**
158 * Trigger custom log meta
159 *
160 * @since 1.0.0
161 *
162 * @param array $log_meta Log meta data
163 * @param stdClass $trigger The trigger object
164 * @param int $user_id The user ID
165 * @param array $event Event information
166 * @param array $trigger_options The trigger's stored options
167 * @param stdClass $automation The trigger's automation object
168 *
169 * @return array
170 */
171 function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) {
172
173 // Bail if action type don't match this action
174 if( $trigger->type !== $this->trigger ) {
175 return $log_meta;
176 }
177
178 $log_meta['order_id'] = ( isset( $event['order_id'] ) ? $event['order_id'] : 0 );
179
180 return $log_meta;
181
182 }
183
184 }
185
186 new AutomatorWP_WooCommerce_Purchase_Product();