Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* "WooCommerce Product" ACFE Form Action Class.
*
* Handles the "WooCommerce Product" ACFE Form Action.
*
* @package Conditional_Form_Actions_For_ACFE
* @since 0.1
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
/**
* CiviCRM Profile Sync "WooCommerce Product" ACFE Form Action Class.
*
* A class that handles the "WooCommerce Product" ACFE Form Action.
*
* @since 0.1
*/
class CFAFA_Form_Action_Product extends CFAFA_Form_Action_Base {
/**
* Plugin object.
*
* @since 0.1
* @access public
* @var object $plugin The plugin object.
*/
public $plugin;
/**
* Parent (calling) object.
*
* @since 0.1
* @access public
* @var object $acf The parent object.
*/
public $acfe;
/**
* Form Action Name.
*
* @since 0.1
* @access public
* @var string $action_name The unique name of the Form Action.
*/
public $action_name = 'woo_cfafa_product';
/**
* Field Key Prefix.
*
* @since 0.1
* @access public
* @var string $field_key The prefix for the Field Key.
*/
public $field_key = 'field_cfafa_woo_product_';
/**
* Field Name Prefix.
*
* @since 0.1
* @access public
* @var string $field_name The prefix for the Field Name.
*/
public $field_name = 'cfafa_woo_product_';
/**
* Constructor.
*
* @since 0.1
*
* @param object $parent The parent object reference.
*/
public function __construct( $parent ) {
// Store references to objects.
$this->plugin = $parent->plugin;
$this->acfe = $parent;
// Label this Form Action.
$this->action_label = __( 'WooCommerce Product action', 'conditional-form-actions-for-acfe' );
// Alias Placeholder for this Form Action.
$this->alias_placeholder = __( 'WooCommerce Product', 'conditional-form-actions-for-acfe' );
// Init parent.
parent::__construct();
}
/**
* Configure this object.
*
* @since 0.1
*/
public function configure() {
// WooCommerce Product Conditional Field.
$this->mapping_field_filters_add( 'product_conditional' );
}
/**
* Performs the action when the Form the Action is attached to is submitted.
*
* @since 0.1
*
* @param array $form The array of Form data.
* @param integer $current_post_id The ID of the Post from which the Form has been submitted.
* @param string $action The customised name of the action.
*/
public function make( $form, $current_post_id, $action ) {
// Bail if a filter has overridden the action.
if ( false === $this->make_skip( $form, $current_post_id, $action ) ) {
return;
}
// Get some Form details.
$form_name = acf_maybe_get( $form, 'name' );
$form_id = acf_maybe_get( $form, 'ID' );
// Populate Product data array.
$product_data = $this->form_product_data( $form, $current_post_id, $action );
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Act with the data from the Form.
$product_data = $this->form_product_save( $product_data );
// Save the results of this Action for later use.
$this->make_action_save( $action, $product_data );
}
/**
* Defines additional Fields for the "Action" Tab.
*
* @since 0.1
*
* @return array $fields The array of Fields for this section.
*/
public function tab_action_append() {
// Init Fields.
$fields = [];
$fields[] = [
'key' => $this->field_key . 'product_id',
'label' => __( 'WooCommerce Product', 'conditional-form-actions-for-acfe' ),
'name' => $this->field_name . 'product_id',
'type' => 'select',
'instructions' => __( 'Use this to add a WooCommerce Product to the Cart.', 'conditional-form-actions-for-acfe' ),
'required' => 0,
'conditional_logic' => 0,
'wrapper' => [
'width' => '',
'class' => '',
'id' => '',
'data-instruction-placement' => 'field',
],
'acfe_permissions' => '',
'default_value' => '',
'placeholder' => '',
'allow_null' => 1,
'multiple' => 0,
'ui' => 0,
//'ajax' => 1,
//'ajax_action' => 'cfafa_get_products',
'return_format' => 'value',
'choices' => $this->product_choices_get(),
];
// Add Conditional Field.
$code = 'product_conditional';
$label = __( 'Conditional On', 'conditional-form-actions-for-acfe' );
$conditional = $this->mapping_field_get( $code, $label );
$conditional['placeholder'] = __( 'Always add', 'conditional-form-actions-for-acfe' );
$conditional['wrapper']['data-instruction-placement'] = 'field';
$conditional['instructions'] = __( 'To add the Product to the Cart only when a Form Field is populated (e.g. "First Name") link this to the Form Field. To add the Product to the Cart only when more complex conditions are met, link this to a Hidden Field with value "1" where the conditional logic of that Field shows it when the conditions are met.', 'conditional-form-actions-for-acfe' );
$fields[] = $conditional;
// --<
return $fields;
}
/**
* Builds Product data array from mapped Fields.
*
* @since 0.1
*
* @param array $form The array of Form data.
* @param integer $current_post_id The ID of the Post from which the Form has been submitted.
* @param string $action The customised name of the action.
* @return array $data The array of Product data.
*/
public function form_product_data( $form, $current_post_id, $action ) {
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
// Add Product ID.
$data['product_id'] = get_sub_field( $this->field_key . 'product_id' );
// Get Product Conditional Reference.
$data['product_conditional_ref'] = get_sub_field( $this->field_key . 'map_product_conditional' );
$conditionals = [ $data['product_conditional_ref'] ];
// Populate array with mapped Conditional Field values.
$conditionals = acfe_form_map_vs_fields( $conditionals, $conditionals, $current_post_id, $form );
// Save Product Conditional.
$data['product_conditional'] = array_pop( $conditionals );
// --<
return $data;
}
/**
* Sends the WooCommerce Product given data from mapped Fields.
*
* @since 0.1
*
* @param array $product_data The array of Product data.
* @return array|bool $product_data The Product data array, or false on failure.
*/
public function form_product_save( $product_data ) {
// Skip if the Product Conditional Reference Field has a value.
if ( ! empty( $product_data['product_conditional_ref'] ) ) {
// And the Product Conditional Field has no value.
if ( empty( $product_data['product_conditional'] ) ) {
return $product_data;
}
}
// Unset Product Conditionals.
if ( isset( $product_data['product_conditional'] ) ) {
unset( $product_data['product_conditional'] );
}
if ( isset( $product_data['product_conditional_ref'] ) ) {
unset( $product_data['product_conditional_ref'] );
}
// Strip out empty Fields.
$product_data = $this->form_data_prepare( $product_data );
// Sanity check.
if ( empty( $product_data['product_id'] ) ) {
return false;
}
// Add the Product to the WooCommerce Cart.
$result = $this->product_add_to_cart( $product_data );
// Bail on failure.
if ( $result === false ) {
return false;
}
// Add Cart Item key to Product data.
$product_data['cart_item_key'] = $result;
// --<
return $product_data;
}
/**
* Gets the WooCommerce Products as an array of options for ACF.
*
* @since 0.1
*
* @return array $options The array of Products formatted for ACF.
*/
public function product_choices_get() {
// Init return.
$options = [];
/*
* Build params to get all published Products.
*
* We have to query directly because WooCommerce has not been initialised
* at this point and wc_get_products() does not return any results. This
* is because ACF hooks into `init` with priority 5 *before* WooCommerce
* initialises.
*
* This may need to be looked at again in future.
*/
$args = [
'post_type' => 'product',
'post_status' => [ 'publish' ],
'no_found_rows' => true,
'posts_per_page' => -1,
];
// Do query.
$query = new WP_Query( $args );
// Do the loop.
if ( $query->have_posts() ) {
foreach ( $query->get_posts() as $found ) {
$options[ $found->ID ] = $found->post_title;
}
}
// Reset Post data just in case.
wp_reset_postdata();
// --<
return $options;
}
/**
* Adds the Product to the Cart.
*
* @since 0.1
*
* @param array $product_data The array of Product data.
* @return integer|bool $cart_item_key The key of the Cart Item, or false otherwise.
*/
public function product_add_to_cart( $product_data ) {
// Get the Product.
$product = wc_get_product( $product_data['product_id'] );
// Sanity check.
if ( empty( $product ) ) {
return false;
}
// Some defaults.
$product_id = $product->get_id();
$quantity = 1;
$variation_id = 0;
$variation = [];
$cart_item_data = [];
// Build Cart Item data?
// Add the configured Product to the Cart.
$cart_item_key = WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation, $cart_item_data );
// --<
return $cart_item_key;
}
} // Class ends.