1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Processes items for a payment form submission. |
4
|
|
|
* |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
defined( 'ABSPATH' ) || exit; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Payment form submission itemss class |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
class GetPaid_Payment_Form_Submission_Items { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Submission items. |
17
|
|
|
* @var GetPaid_Form_Item[] |
18
|
|
|
*/ |
19
|
|
|
public $items = array(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class constructor |
23
|
|
|
* |
24
|
|
|
* @param GetPaid_Payment_Form_Submission $submission |
25
|
|
|
*/ |
26
|
|
|
public function __construct( $submission ) { |
27
|
|
|
|
28
|
|
|
$data = $submission->get_data(); |
29
|
|
|
$payment_form = $submission->get_payment_form(); |
30
|
|
|
|
31
|
|
|
// Prepare the selected items. |
32
|
|
|
$selected_items = array(); |
33
|
|
|
if ( ! empty( $data['getpaid-items'] ) ) { |
34
|
|
|
$selected_items = wpinv_clean( $data['getpaid-items'] ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// For default forms, ensure that an item has been set. |
38
|
|
|
if ( $payment_form->is_default() && ! $submission->has_invoice() ) { |
39
|
|
|
$payment_form->set_items( $selected_items ); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// Process each individual item. |
43
|
|
|
foreach ( $payment_form->get_items() as $item ) { |
44
|
|
|
$this->process_item( $item, $selected_items ); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Process a single item. |
51
|
|
|
* |
52
|
|
|
* @param GetPaid_Form_Item $item |
53
|
|
|
* @param array $selected_items |
54
|
|
|
*/ |
55
|
|
|
public function process_item( $item, $selected_items ) { |
56
|
|
|
|
57
|
|
|
// Abort if this is an optional item and it has not been selected. |
58
|
|
|
if ( ! $item->is_required() && ! isset( $selected_items[ $item->get_id() ] ) ) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// (maybe) let customers change the quantities and prices. |
63
|
|
|
if ( isset( $selected_items[ $item->get_id() ] ) ) { |
64
|
|
|
|
65
|
|
|
// Maybe change the quantities. |
66
|
|
|
if ( $item->allows_quantities() ) { |
67
|
|
|
$item->set_quantity( (int) $selected_items[ $item->get_id() ]['quantity'] ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Maybe change the price. |
71
|
|
|
if ( $item->user_can_set_their_price() ) { |
72
|
|
|
$price = (float) wpinv_sanitize_amount( $selected_items[ $item->get_id() ]['price'] ); |
73
|
|
|
|
74
|
|
|
// But don't get lower than the minimum price. |
75
|
|
|
$price = max( $price, $item->get_minimum_price() ); |
76
|
|
|
|
77
|
|
|
$item->set_price( $price ); |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Save the item. |
84
|
|
|
$this->items[] = $item; |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|