PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.33
OttoKit: All-in-One Automation Platform v1.1.33
1.1.33 1.1.32 1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.47 1.0.48 1.0.49 1.0.50 1.0.51 1.0.52 1.0.53 1.0.54 1.0.55 1.0.56 1.0.57 1.0.58 1.0.59 1.0.60 1.0.61 1.0.62 1.0.63 1.0.64 1.0.65 1.0.66 1.0.67 1.0.68 1.0.69 1.0.7 1.0.70 1.0.71 1.0.72 1.0.73 1.0.74 1.0.75 1.0.76 1.0.77 1.0.78 1.0.79 1.0.8 1.0.80 1.0.81 1.0.82 1.0.83 1.0.84 1.0.85 1.0.86 1.0.87 1.0.88 1.0.89 1.0.9 1.0.90 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
suretriggers / src / Integrations / advanced-coupons / actions / create-coupon.php
suretriggers / src / Integrations / advanced-coupons / actions Last commit date
add-store-credit-to-user.php 10 months ago create-coupon.php 5 months ago remove-store-credit-from-user.php 10 months ago
create-coupon.php
267 lines
1 <?php
2 /**
3 * CreateCoupon.
4 * php version 5.6
5 *
6 * @category CreateCoupon
7 * @package SureTriggers
8 * @author BSF <username@example.com>
9 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
10 * @link https://www.brainstormforce.com/
11 * @since 1.0.0
12 */
13
14 namespace SureTriggers\Integrations\AdvancedCoupons\Actions;
15
16 use SureTriggers\Integrations\AutomateAction;
17 use SureTriggers\Integrations\WordPress\WordPress;
18 use SureTriggers\Traits\SingletonLoader;
19
20 /**
21 * CreateCoupon
22 *
23 * @category CreateCoupon
24 * @package SureTriggers
25 * @author BSF <username@example.com>
26 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
27 * @link https://www.brainstormforce.com/
28 * @since 1.0.0
29 */
30 class CreateCoupon extends AutomateAction {
31
32 /**
33 * Integration type.
34 *
35 * @var string
36 */
37 public $integration = 'AdvancedCoupons';
38
39 /**
40 * Action name.
41 *
42 * @var string
43 */
44 public $action = 'acfw_create_coupon';
45
46 use SingletonLoader;
47
48 /**
49 * Register a action.
50 *
51 * @param array $actions actions.
52 * @return array
53 */
54 public function register( $actions ) {
55 $actions[ $this->integration ][ $this->action ] = [
56 'label' => __( 'Create Coupon', 'suretriggers' ),
57 'action' => $this->action,
58 'function' => [ $this, 'action_listener' ],
59 ];
60 return $actions;
61 }
62
63 /**
64 * Action listener.
65 *
66 * @param int $user_id user_id.
67 * @param int $automation_id automation_id.
68 * @param array $fields fields.
69 * @param array $selected_options selectedOptions.
70 *
71 * @return void|array|bool
72 */
73 public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) {
74 $coupon_code = ! empty( $selected_options['coupon_code'] ) ? sanitize_text_field( $selected_options['coupon_code'] ) : $this->generate_coupon_code();
75 $description = isset( $selected_options['description'] ) ? sanitize_textarea_field( $selected_options['description'] ) : '';
76 $discount_type = isset( $selected_options['discount_type'] ) ? sanitize_text_field( $selected_options['discount_type'] ) : 'percent';
77 $coupon_amount = isset( $selected_options['coupon_amount'] ) ? floatval( $selected_options['coupon_amount'] ) : 0;
78 $is_free_shipping = ! empty( $selected_options['is_free_shipping'] ) ? 'yes' : 'no';
79 $start_date = isset( $selected_options['start_date'] ) ? sanitize_text_field( $selected_options['start_date'] ) : '';
80 $expiry_date = isset( $selected_options['expiry_date'] ) ? sanitize_text_field( $selected_options['expiry_date'] ) : '';
81 $min_spend = isset( $selected_options['min_spend'] ) ? floatval( $selected_options['min_spend'] ) : '';
82 $max_spend = isset( $selected_options['max_spend'] ) ? floatval( $selected_options['max_spend'] ) : '';
83 $is_individual = ! empty( $selected_options['is_individual'] ) ? 'yes' : 'no';
84 $exclude_sale_items = ! empty( $selected_options['exclude_sale_items'] ) ? 'yes' : 'no';
85 $allowed_emails = ! empty( $selected_options['allowed_emails'] ) ? array_filter( array_map( 'sanitize_email', explode( ',', $selected_options['allowed_emails'] ) ) ) : '';
86 $usage_limit_per_coupon = isset( $selected_options['usage_limit_per_coupon'] ) ? absint( $selected_options['usage_limit_per_coupon'] ) : '';
87 $limit_items = isset( $selected_options['limit_items'] ) ? absint( $selected_options['limit_items'] ) : '';
88 $usage_limit_per_user = isset( $selected_options['usage_limit_per_user'] ) ? absint( $selected_options['usage_limit_per_user'] ) : '';
89 $product_cat_ids = [];
90 $product_cat_names = [];
91 $product_cat_exclude_ids = [];
92 $product_cat_exclude_names = [];
93 $product_ids = [];
94 $product_names = [];
95 $exclude_product_ids = [];
96 $exclude_product_names = [];
97
98 if ( isset( $selected_options['product_cat'] ) && is_array( $selected_options['product_cat'] ) ) {
99 foreach ( $selected_options['product_cat'] as $product_cat ) {
100 $product_cat_ids[] = $product_cat['value'];
101 $product_cat_names[] = $product_cat['label'];
102 }
103 }
104
105 if ( isset( $selected_options['exclude_product_cat'] ) && is_array( $selected_options['exclude_product_cat'] ) ) {
106 foreach ( $selected_options['exclude_product_cat'] as $exclude_product_cat ) {
107 $product_cat_exclude_ids[] = $exclude_product_cat['value'];
108 $product_cat_exclude_names[] = $exclude_product_cat['label'];
109 }
110 }
111
112 if ( isset( $selected_options['product'] ) && is_array( $selected_options['product'] ) ) {
113 foreach ( $selected_options['product'] as $product ) {
114 $product_ids[] = $product['value'];
115 $product_names[] = $product['label'];
116 }
117 }
118
119 if ( isset( $selected_options['exclude_product'] ) && is_array( $selected_options['exclude_product'] ) ) {
120 foreach ( $selected_options['exclude_product'] as $exclude_product ) {
121 $exclude_product_ids[] = $exclude_product['value'];
122 $exclude_product_names[] = $exclude_product['label'];
123 }
124 }
125
126 $products = ! empty( $product_ids ) ? implode( ',', array_map( 'intval', $product_ids ) ) : '';
127 $exclude_products = ! empty( $exclude_product_ids ) ? implode( ',', array_map( 'intval', $exclude_product_ids ) ) : '';
128
129 // Check if coupon code already exists using WooCommerce function.
130 if ( function_exists( 'wc_get_coupon_id_by_code' ) ) {
131 $existing_coupon_id = wc_get_coupon_id_by_code( $coupon_code );
132 if ( $existing_coupon_id ) {
133 return [
134 'status' => 'error',
135 'message' => __( 'A coupon with this code already exists.', 'suretriggers' ),
136 ];
137 }
138 }
139
140 $args = [
141 'post_title' => $coupon_code,
142 'post_content' => '',
143 'post_status' => 'publish',
144 'post_author' => $user_id,
145 'post_type' => 'shop_coupon',
146 'post_excerpt' => $description,
147 ];
148
149 $coupon_id = wp_insert_post( $args, true );
150
151 if ( is_wp_error( $coupon_id ) ) {
152 return [
153 'status' => 'error',
154 'message' => $coupon_id->get_error_message(),
155 ];
156 }
157
158 if ( ! $coupon_id ) {
159 return [
160 'status' => 'error',
161 'message' => __( 'Failed to create coupon.', 'suretriggers' ),
162 ];
163 }
164
165 $coupon_meta = [
166 'discount_type' => $discount_type,
167 'coupon_amount' => $coupon_amount,
168 'free_shipping' => $is_free_shipping,
169 'expiry_date' => $expiry_date,
170 'date_expires' => ! empty( $expiry_date ) ? strtotime( $expiry_date ) : '',
171 'minimum_amount' => $min_spend,
172 'maximum_amount' => $max_spend,
173 'individual_use' => $is_individual,
174 'exclude_sale_items' => $exclude_sale_items,
175 'product_ids' => $products,
176 'exclude_product_ids' => $exclude_products,
177 'product_categories' => $product_cat_ids,
178 'exclude_product_categories' => $product_cat_exclude_ids,
179 'customer_email' => $allowed_emails,
180 'usage_limit' => $usage_limit_per_coupon,
181 'limit_usage_to_x_items' => $limit_items,
182 'usage_limit_per_user' => $usage_limit_per_user,
183 ];
184
185 foreach ( $coupon_meta as $key => $value ) {
186 update_post_meta( $coupon_id, $key, $value );
187 }
188
189 // Advanced Coupons specific features.
190 $enable_coupon_url = isset( $selected_options['enable_coupon_url'] ) && $selected_options['enable_coupon_url'] ? 'yes' : 'no';
191 $coupon_url_code = isset( $selected_options['coupon_url_code'] ) ? sanitize_title( $selected_options['coupon_url_code'] ) : '';
192 $success_message = isset( $selected_options['success_message'] ) ? sanitize_textarea_field( $selected_options['success_message'] ) : '';
193 $redirect_url = isset( $selected_options['redirect_url'] ) ? esc_url_raw( $selected_options['redirect_url'] ) : '';
194
195 $acfw_meta = [
196 '_acfw_enable_coupon_url' => $enable_coupon_url,
197 '_acfw_coupon_url_code' => $coupon_url_code,
198 '_acfw_success_message' => $success_message,
199 '_acfw_after_redirect_url' => $redirect_url,
200 '_acfw_schedule_start' => $start_date,
201 '_acfw_schedule_end' => $expiry_date,
202 ];
203
204 foreach ( $acfw_meta as $key => $value ) {
205 if ( '' !== $value ) {
206 update_post_meta( $coupon_id, $key, $value );
207 }
208 }
209
210 // Prepare return data.
211 $return_data = [
212 'coupon_id' => $coupon_id,
213 'coupon_code' => $coupon_code,
214 'coupon_description' => $description,
215 'discount_type' => $discount_type,
216 'coupon_amount' => $coupon_amount,
217 'free_shipping' => $is_free_shipping,
218 'start_date' => $start_date,
219 'expiry_date' => $expiry_date,
220 'minimum_amount' => $min_spend,
221 'maximum_amount' => $max_spend,
222 'individual_use' => $is_individual,
223 'exclude_sale_items' => $exclude_sale_items,
224 'product_ids' => $products,
225 'product_ids_list' => implode( ',', $product_ids ),
226 'product_names' => $product_names,
227 'product_names_list' => implode( ',', $product_names ),
228 'exclude_product_ids' => $exclude_products,
229 'exclude_product_ids_list' => implode( ',', $exclude_product_ids ),
230 'exclude_product_names' => $exclude_product_names,
231 'exclude_product_names_list' => implode( ',', $exclude_product_names ),
232 'product_categories' => $product_cat_ids,
233 'product_categories_list' => implode( ',', $product_cat_ids ),
234 'product_categories_names' => $product_cat_names,
235 'product_categories_names_list' => implode( ',', $product_cat_names ),
236 'exclude_product_categories' => $product_cat_exclude_ids,
237 'exclude_product_categories_list' => implode( ',', $product_cat_exclude_ids ),
238 'exclude_product_categories_names' => $product_cat_exclude_names,
239 'exclude_product_categories_names_list' => implode( ',', $product_cat_exclude_names ),
240 'customer_email' => is_array( $allowed_emails ) ? implode( ',', $allowed_emails ) : $allowed_emails,
241 'usage_limit' => $usage_limit_per_coupon,
242 'limit_usage_to_x_items' => $limit_items,
243 'usage_limit_per_user' => $usage_limit_per_user,
244 'enable_coupon_url' => $enable_coupon_url,
245 'coupon_url_code' => $coupon_url_code,
246 'success_message' => $success_message,
247 'redirect_url' => $redirect_url,
248 ];
249
250 return array_merge(
251 $return_data,
252 WordPress::get_user_context( $user_id )
253 );
254 }
255
256 /**
257 * Generate random coupon code.
258 *
259 * @return string
260 */
261 private function generate_coupon_code() {
262 return substr( str_shuffle( 'ABCDEFGHJKMNPQRSTUVWXYZ23456789' ), 0, 8 );
263 }
264 }
265
266 CreateCoupon::get_instance();
267