PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.4
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.4
6.0.4 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 All 35 releases
automatorwp / integrations / woocommerce / includes / triggers / anonymous-purchase-product.php

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

189 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 * Anonymous Purchase Product
4 *
5 * @package AutomatorWP\Integrations\WooCommerce\Triggers\Anonymous_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_Anonymous_Purchase_Product extends AutomatorWP_Integration_Trigger {
13
14 public $integration = 'woocommerce';
15 public $trigger = 'woocommerce_anonymous_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 'anonymous' => true,
27 'label' => __( 'Guest purchases a product', 'automatorwp' ),
28 'select_option' => __( 'Guest purchases <strong>a product</strong>', 'automatorwp' ),
29 /* translators: %1$s: Post title. %2$s: Number of times. */
30 'edit_label' => sprintf( __( 'Guest purchases %1$s %2$s time(s)', 'automatorwp' ), '{post}', '{times}' ),
31 /* translators: %1$s: Post title. */
32 'log_label' => sprintf( __( 'Guest purchases %1$s', 'automatorwp' ), '{post}' ),
33 'action' => 'woocommerce_order_status_completed',
34 'function' => array( $this, 'listener' ),
35 'priority' => 10,
36 'accepted_args' => 1,
37 'options' => array(
38 'post' => automatorwp_utilities_post_option( array(
39 'name' => __( 'Product:', 'automatorwp' ),
40 'option_none_label' => __( 'any product', 'automatorwp' ),
41 'post_type' => 'product'
42 ) ),
43 'times' => automatorwp_utilities_times_option(),
44 ),
45 'tags' => array_merge(
46 automatorwp_utilities_post_tags( __( 'Product', 'automatorwp' ) ),
47 automatorwp_woocommerce_order_tags(),
48 automatorwp_utilities_times_tag()
49 )
50 ) );
51
52 }
53
54 /**
55 * Trigger listener
56 *
57 * @since 1.0.0
58 *
59 * @param int $order_id The order ID
60 */
61 public function listener( $order_id ) {
62
63 $order = wc_get_order( $order_id );
64
65 // Bail if not a valid order
66 if( ! $order ) {
67 return;
68 }
69
70 // Bail if order is not marked as completed
71 if ( $order->get_status() !== 'completed' ) {
72 return;
73 }
74
75 $items = $order->get_items();
76
77 // Bail if no items purchased
78 if ( ! is_array( $items ) ) {
79 return;
80 }
81
82 $order_total = $order->get_total();
83 $user_id = $order->get_user_id();
84
85 // Bail if not is an anonymous purchase
86 if( $user_id !== 0 ) {
87 return;
88 }
89
90 // Loop all items to trigger events on each one purchased
91 foreach ( $items as $item ) {
92
93 $product_id = $item->get_product_id();
94 $quantity = $item->get_quantity();
95
96 // Skip items not assigned to a product
97 if( $product_id === 0 ) {
98 continue;
99 }
100
101
102 // Trigger events same times as item quantity
103 for ( $i = 0; $i < $quantity; $i++ ) {
104
105 // Trigger the product purchase
106 automatorwp_trigger_event( array(
107 'trigger' => $this->trigger,
108 'post_id' => $product_id,
109 'order_id' => $order_id,
110 'order_total' => $order_total,
111 ) );
112
113 } // End for of quantities
114
115 } // End foreach of items
116
117 }
118
119 /**
120 * Anonymous deserves check
121 *
122 * @since 1.0.0
123 *
124 * @param bool $deserves_trigger True if anonymous deserves trigger, false otherwise
125 * @param stdClass $trigger The trigger object
126 * @param array $event Event information
127 * @param array $trigger_options The trigger's stored options
128 * @param stdClass $automation The trigger's automation object
129 *
130 * @return bool True if anonymous deserves trigger, false otherwise
131 */
132 public function anonymous_deserves_trigger( $deserves_trigger, $trigger, $event, $trigger_options, $automation ) {
133
134 // Don't deserve if post is not received
135 if( ! isset( $event['post_id'] ) ) {
136 return false;
137 }
138
139 // Don't deserve if post doesn't match with the trigger option
140 if( ! automatorwp_posts_matches( $event['post_id'], $trigger_options['post'] ) ) {
141 return false;
142 }
143
144 return $deserves_trigger;
145
146 }
147
148 /**
149 * Register the required hooks
150 *
151 * @since 1.0.0
152 */
153 public function hooks() {
154
155 // Log meta data
156 add_filter( 'automatorwp_anonymous_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 5 );
157
158 parent::hooks();
159 }
160
161 /**
162 * Trigger custom log meta
163 *
164 * @since 1.0.0
165 *
166 * @param array $log_meta Log meta data
167 * @param stdClass $trigger The trigger object
168 * @param array $event Event information
169 * @param array $trigger_options The trigger's stored options
170 * @param stdClass $automation The trigger's automation object
171 *
172 * @return array
173 */
174 function log_meta( $log_meta, $trigger, $event, $trigger_options, $automation ) {
175
176 // Bail if action type don't match this action
177 if( $trigger->type !== $this->trigger ) {
178 return $log_meta;
179 }
180
181 $log_meta['order_id'] = ( isset( $event['order_id'] ) ? $event['order_id'] : 0 );
182
183 return $log_meta;
184
185 }
186
187 }
188
189 new AutomatorWP_WooCommerce_Anonymous_Purchase_Product();