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

178 lines 5.9 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\Upsell_Plugin\Triggers\Purchase_Product
6 * @author AutomatorWP <contact@automatorwp.com>, Ruben Garcia <rubengcdev@gmail.com>
7 * @since 1.0.0
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 class AutomatorWP_Upsell_Plugin_Purchase_Product extends AutomatorWP_Integration_Trigger {
13
14 public $integration = 'upsell_plugin';
15 public $trigger = 'upsell_plugin_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' => 'upsell_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' => 'upsell_product'
41 ) ),
42 'times' => automatorwp_utilities_times_option(),
43 ),
44 'tags' => array_merge(
45 automatorwp_utilities_post_tags( __( 'Product', 'automatorwp' ) ),
46 automatorwp_utilities_times_tag()
47 )
48 ) );
49
50 }
51
52 /**
53 * Trigger listener
54 *
55 * @since 1.0.0
56 *
57 * @param Upsell\Entities\Order $order The order
58 */
59 public function listener( $order ) {
60
61 // Bail if order is not marked as completed
62 if ( $order->getStatus() !== 'completed' ) {
63 return;
64 }
65
66 $items = $order->getItems();
67
68 // Bail if no items purchased
69 if ( ! is_array( $items ) ) {
70 return;
71 }
72
73 $order_id = $order->getId();
74 $order_total = $order->getTotal();
75 $user_id = $order->customer()->attribute('user_id');
76
77 // Loop all items to trigger events on each one purchased
78 foreach ( $items as $item ) {
79
80 $product_id = $item['id'];
81 $quantity = $item['quantity'];
82
83 // Skip items not assigned to a product
84 if( $product_id === 0 ) {
85 continue;
86 }
87
88 // Trigger events same times as item quantity
89 for ( $i = 0; $i < $quantity; $i++ ) {
90
91 // Trigger the product purchase
92 automatorwp_trigger_event( array(
93 'trigger' => $this->trigger,
94 'user_id' => $user_id,
95 'post_id' => $product_id,
96 'order_id' => $order_id,
97 'order_total' => $order_total,
98 ) );
99
100 } // End for of quantities
101
102 } // End foreach of items
103
104 }
105
106 /**
107 * User deserves check
108 *
109 * @since 1.0.0
110 *
111 * @param bool $deserves_trigger True if user deserves trigger, false otherwise
112 * @param stdClass $trigger The trigger object
113 * @param int $user_id The user ID
114 * @param array $event Event information
115 * @param array $trigger_options The trigger's stored options
116 * @param stdClass $automation The trigger's automation object
117 *
118 * @return bool True if user deserves trigger, false otherwise
119 */
120 public function user_deserves_trigger( $deserves_trigger, $trigger, $user_id, $event, $trigger_options, $automation ) {
121
122 // Don't deserve if post is not received
123 if( ! isset( $event['post_id'] ) ) {
124 return false;
125 }
126
127 // Don't deserve if post doesn't match with the trigger option
128 if( ! automatorwp_posts_matches( $event['post_id'], $trigger_options['post'] ) ) {
129 return false;
130 }
131
132 return $deserves_trigger;
133
134 }
135
136 /**
137 * Register the required hooks
138 *
139 * @since 1.0.0
140 */
141 public function hooks() {
142
143 // Log meta data
144 add_filter( 'automatorwp_user_completed_trigger_log_meta', array( $this, 'log_meta' ), 10, 6 );
145
146 parent::hooks();
147 }
148
149 /**
150 * Trigger custom log meta
151 *
152 * @since 1.0.0
153 *
154 * @param array $log_meta Log meta data
155 * @param stdClass $trigger The trigger object
156 * @param int $user_id The user ID
157 * @param array $event Event information
158 * @param array $trigger_options The trigger's stored options
159 * @param stdClass $automation The trigger's automation object
160 *
161 * @return array
162 */
163 function log_meta( $log_meta, $trigger, $user_id, $event, $trigger_options, $automation ) {
164
165 // Bail if action type don't match this action
166 if( $trigger->type !== $this->trigger ) {
167 return $log_meta;
168 }
169
170 $log_meta['order_id'] = ( isset( $event['order_id'] ) ? $event['order_id'] : 0 );
171
172 return $log_meta;
173
174 }
175
176 }
177
178 new AutomatorWP_Upsell_Plugin_Purchase_Product();