PluginProbe
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services / 8.8.0
WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services v8.8.0
8.8.0 8.7.9 8.7.8 8.7.7 8.7.6 8.7.5 8.7.4 8.7.3 8.7.2 8.7.1 8.7.0 8.6.9 8.6.8 8.6.7 8.6.6 8.6.5 8.6.4 8.6.2 8.6.1 8.6.0 8.5.9 8.5.8 8.5.7 8.5.6 8.5.5 All 536 releases
chatbot / addons / automator / includes / triggers / woocommerce-triggers.php

woocommerce-triggers.php in WPBot – AI ChatBot for Live Support, Lead Generation, WordPress Automation, AI Services 8.8.0, at addons/automator/includes/triggers/woocommerce-triggers.php

186 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WooCommerce Triggers
4 *
5 * @package WPbot_Automator
6 */
7
8 namespace WPbot_Automator\Triggers;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit;
12 }
13
14 /**
15 * Class WooCommerce_Triggers
16 */
17 class WooCommerce_Triggers extends Trigger {
18
19 /**
20 * Constructor
21 */
22 public function __construct() {
23 $this->id = 'woocommerce';
24 $this->group = __( 'WooCommerce', 'wpbot-automator' );
25 }
26
27 /**
28 * Get available sub-triggers
29 */
30 public static function get_sub_triggers() {
31 return array(
32 'wc_order_created' => array(
33 'title' => __( 'Order Created', 'wpbot-automator' ),
34 'hook' => 'woocommerce_new_order',
35 ),
36 'wc_order_status_change' => array(
37 'title' => __( 'Order Status Change', 'wpbot-automator' ),
38 'hook' => 'woocommerce_order_status_changed',
39 ),
40 'wc_payment_completed' => array(
41 'title' => __( 'Payment Completed', 'wpbot-automator' ),
42 'hook' => 'woocommerce_payment_complete',
43 ),
44 'wc_product_added' => array(
45 'title' => __( 'Product Added', 'wpbot-automator' ),
46 'hook' => 'wp_insert_post', // Filtered for product post type
47 ),
48 );
49 }
50
51 /**
52 * Register the triggers
53 */
54 public function register() {
55 if ( ! class_exists( 'WooCommerce' ) ) {
56 return;
57 }
58
59 $triggers = self::get_sub_triggers();
60
61 add_action( 'woocommerce_new_order', array( $this, 'handle_new_order' ), 10, 2 );
62 add_action( 'woocommerce_order_status_changed', array( $this, 'handle_order_status_change' ), 10, 4 );
63 add_action( 'woocommerce_payment_complete', array( $this, 'handle_payment_complete' ), 10, 1 );
64 add_action( 'wp_insert_post', array( $this, 'handle_new_product' ), 10, 3 );
65 }
66
67 /**
68 * Handle new order
69 */
70 public function handle_new_order( $order_id, $order ) {
71 $product_ids = array();
72 $items = array();
73 foreach ( $order->get_items() as $item ) {
74 $product_ids[] = $item->get_product_id();
75 $items[] = array(
76 'name' => $item->get_name(),
77 'sku' => ( $item->get_product() ? $item->get_product()->get_sku() : '' ),
78 'quantity' => $item->get_quantity(),
79 'total' => $item->get_total(),
80 );
81 }
82
83 $this->run( array(
84 'sub_id' => 'wc_order_created',
85 'data' => array(
86 'order_id' => $order_id,
87 'total' => $order->get_total(),
88 'currency' => $order->get_currency(),
89 'status' => $order->get_status(),
90 'billing_email' => $order->get_billing_email(),
91 'billing_phone' => $order->get_billing_phone(),
92 'billing_name' => $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(),
93 'payment_method' => $order->get_payment_method_title(),
94 'product_ids' => $product_ids,
95 'items' => $items,
96 ),
97 ) );
98 }
99
100 /**
101 * Handle order status change
102 */
103 public function handle_order_status_change( $order_id, $old_status, $new_status, $order ) {
104 $product_ids = array();
105 $items = array();
106 foreach ( $order->get_items() as $item ) {
107 $product_ids[] = $item->get_product_id();
108 $items[] = array(
109 'name' => $item->get_name(),
110 'sku' => ( $item->get_product() ? $item->get_product()->get_sku() : '' ),
111 'quantity' => $item->get_quantity(),
112 'total' => $item->get_total(),
113 );
114 }
115
116 $this->run( array(
117 'sub_id' => 'wc_order_status_change',
118 'data' => array(
119 'order_id' => $order_id,
120 'old_status' => $old_status,
121 'new_status' => $new_status,
122 'total' => $order->get_total(),
123 'currency' => $order->get_currency(),
124 'billing_email' => $order->get_billing_email(),
125 'billing_phone' => $order->get_billing_phone(),
126 'billing_name' => $order->get_billing_first_name() . ' ' . $order->get_billing_last_name(),
127 'payment_method' => $order->get_payment_method_title(),
128 'product_ids' => $product_ids,
129 'items' => $items,
130 ),
131 ) );
132 }
133
134 /**
135 * Handle payment complete
136 */
137 public function handle_payment_complete( $order_id ) {
138 $order = \wc_get_order( $order_id );
139 $product_ids = array();
140 $items = array();
141 if ( $order ) {
142 foreach ( $order->get_items() as $item ) {
143 $product_ids[] = $item->get_product_id();
144 $items[] = array(
145 'name' => $item->get_name(),
146 'sku' => ( $item->get_product() ? $item->get_product()->get_sku() : '' ),
147 'quantity' => $item->get_quantity(),
148 'total' => $item->get_total(),
149 );
150 }
151 }
152
153 $this->run( array(
154 'sub_id' => 'wc_payment_completed',
155 'data' => array(
156 'order_id' => $order_id,
157 'total' => $order ? $order->get_total() : 0,
158 'currency' => $order ? $order->get_currency() : '',
159 'billing_email' => $order ? $order->get_billing_email() : '',
160 'billing_phone' => $order ? $order->get_billing_phone() : '',
161 'billing_name' => $order ? ( $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() ) : '',
162 'payment_method' => $order ? $order->get_payment_method_title() : '',
163 'product_ids' => $product_ids,
164 'items' => $items,
165 ),
166 ) );
167 }
168
169 /**
170 * Handle new product
171 */
172 public function handle_new_product( $post_id, $post, $update ) {
173 if ( $update || 'product' !== $post->post_type ) {
174 return;
175 }
176
177 $this->run( array(
178 'sub_id' => 'wc_product_added',
179 'data' => array(
180 'product_id' => $post_id,
181 'title' => $post->post_title,
182 ),
183 ) );
184 }
185 }
186