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
125
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
<?php
namespace Objectiv\Plugins\Checkout\Model;
use WC_Order_Item; use WC_Product;
class Item { protected $thumbnail; protected $quantity; protected $quantity_control; protected $title; protected $url; protected $subtotal; protected $row_class; protected $item_key; protected $raw_item; protected $product; protected $data;
/** * @param array|WC_Order_Item $item */ public function __construct( $item ) { if ( is_array( $item ) ) { $this->ingest_cart_item( $item ); } else { $this->ingest_order_item( $item ); } }
/** * @param array $cart_item */ protected function ingest_cart_item( array $cart_item ) { // Some of our callbacks rely on cart_item_key being a string // Since PHP coerces scalar types to strings for typed function arguments, // we just have to handle the situation where the key is null, which is // for some reason not coerced due to ancient secret PHP knowledge $cart_item_key = $cart_item['key'] ?? '';
/** @var WC_Product $_product */ $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$this->thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image( 'cfw_cart_thumb' ), $cart_item, $cart_item_key ); $this->quantity = floatval( $cart_item['quantity'] ); $this->quantity_control = apply_filters( 'cfw_cart_item_quantity_control', '', $cart_item, $_product, $cart_item_key ); $this->title = apply_filters( 'woocommerce_cart_item_name', $_product->get_name(), $cart_item, $cart_item_key ); $this->url = get_permalink( $cart_item['product_id'] ); $this->subtotal = apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key ); $this->row_class = apply_filters( 'cfw_cart_item_row_class', '', $cart_item ); $this->item_key = $cart_item_key; $this->raw_item = $cart_item; $this->product = $_product; $this->data = $this->get_cart_item_data( $cart_item ); }
/** * @param WC_Order_Item $item */ protected function ingest_order_item( WC_Order_Item $item ) { $order = wc_get_order( $item->get_order_id() ); $item_product = $item->get_product(); $item_subtotal = $order->get_formatted_line_subtotal( $item );
$this->thumbnail = $item_product ? $item_product->get_image( 'cfw_cart_thumb' ) : ''; $this->quantity = $item->get_quantity(); $this->quantity_control = ''; $this->title = $item->get_name(); $this->url = $item_product ? get_permalink( $item->get_product_id() ) : ''; $this->subtotal = ! empty( $item_subtotal ) ? $item_subtotal : wc_price( $item->get_subtotal() ); $this->row_class = apply_filters( 'cfw_order_item_row_class', '', $item ); $this->item_key = $item->get_id(); $this->raw_item = $item; $this->product = $item_product; $this->data = $this->get_order_item_data( $item ); }
/** * @return string */ public function get_thumbnail(): string { return strval( $this->thumbnail ); }
/** * @return int */ public function get_quantity(): float { return floatval( $this->quantity ); }
/** * @return string */ public function get_quantity_control(): string { return strval( $this->quantity_control ); }
/** * @return string */ public function get_title(): string { return strval( $this->title ); }
/** * @return string */ public function get_url(): string { return strval( $this->url ); }
/** * @return string */ public function get_subtotal(): string { return strval( $this->subtotal ); }
/** * @return string */ public function get_row_class(): string { return strval( $this->row_class ); }
/** * @return string */ public function get_item_key(): string { return strval( $this->item_key ); }
/** * @return array|WC_Order_Item */ public function get_raw_item() { // TODO: Eliminate the necessity of this workaround in a future major version return $this->raw_item; }
/** * @return WC_Product */ public function get_product(): WC_Product { return $this->product; }
/** * @return array */ public function get_data(): array { return $this->data ?? array(); }
/** * @param array $item * @return array */ protected function get_cart_item_data( array $item ): array { $item_data = array();
// Variation values are shown only if they are not found in the title as of 3.0. // This is because variation titles display the attributes. if ( $item['data']->is_type( 'variation' ) && is_array( $item['variation'] ) ) { foreach ( $item['variation'] as $name => $value ) { $taxonomy = wc_attribute_taxonomy_name( str_replace( 'attribute_pa_', '', urldecode( $name ) ) );
if ( taxonomy_exists( $taxonomy ) ) { // If this is a term slug, get the term's nice name. $term = get_term_by( 'slug', $value, $taxonomy ); if ( ! is_wp_error( $term ) && $term && $term->name ) { $value = $term->name; } $label = wc_attribute_label( $taxonomy ); } else { // If this is a custom option slug, get the options name. $value = apply_filters( 'woocommerce_variation_option_name', $value, null, $taxonomy, $item['data'] ); $label = wc_attribute_label( str_replace( 'attribute_', '', $name ), $item['data'] ); }
// Check the nicename against the title. if ( '' === $value || wc_is_attribute_in_product_name( $value, $item['data']->get_name() ) ) { continue; }
$item_data[] = array( 'key' => $label, 'value' => $value, ); } }
// Filter item data to allow 3rd parties to add more to the array. $item_data = apply_filters( 'woocommerce_get_item_data', $item_data, $item ); $data = array();
foreach ( $item_data as $item_datum ) { $data[ $item_datum['key'] ] = $item_datum['value']; }
return $data; }
/** * @param WC_Order_Item $item * @return array */ protected function get_order_item_data( WC_Order_Item $item ): array { $data = array();
foreach ( $item->get_formatted_meta_data() as $meta ) { $data[ $meta->display_key ] = $meta->display_value; }
return $data; } }
|