Code Duplication    Length = 65-65 lines in 2 locations

includes/api/class-wc-rest-order-refunds-controller.php 1 location

@@ 132-196 (lines=65) @@
129
		);
130
131
		// Add line items.
132
		foreach ( $refund->get_items() as $item_id => $item ) {
133
			$product      = $refund->get_product_from_item( $item );
134
			$product_id   = 0;
135
			$variation_id = 0;
136
			$product_sku  = null;
137
138
			// Check if the product exists.
139
			if ( is_object( $product ) ) {
140
				$product_id   = $product->id;
141
				$variation_id = $product->variation_id;
142
				$product_sku  = $product->get_sku();
143
			}
144
145
			$meta = new WC_Order_Item_Meta( $item, $product );
146
147
			$item_meta = array();
148
149
			$hideprefix = 'true' === $request['all_item_meta'] ? null : '_';
150
151
			foreach ( $meta->get_formatted( $hideprefix ) as $meta_key => $formatted_meta ) {
152
				$item_meta[] = array(
153
					'key'   => $formatted_meta['key'],
154
					'label' => $formatted_meta['label'],
155
					'value' => $formatted_meta['value'],
156
				);
157
			}
158
159
			$line_item = array(
160
				'id'           => $item_id,
161
				'name'         => $item['name'],
162
				'sku'          => $product_sku,
163
				'product_id'   => (int) $product_id,
164
				'variation_id' => (int) $variation_id,
165
				'quantity'     => wc_stock_amount( $item['qty'] ),
166
				'tax_class'    => ! empty( $item['tax_class'] ) ? $item['tax_class'] : '',
167
				'price'        => wc_format_decimal( $refund->get_item_total( $item, false, false ), $dp ),
168
				'subtotal'     => wc_format_decimal( $refund->get_line_subtotal( $item, false, false ), $dp ),
169
				'subtotal_tax' => wc_format_decimal( $item['line_subtotal_tax'], $dp ),
170
				'total'        => wc_format_decimal( $refund->get_line_total( $item, false, false ), $dp ),
171
				'total_tax'    => wc_format_decimal( $item['line_tax'], $dp ),
172
				'taxes'        => array(),
173
				'meta'         => $item_meta,
174
			);
175
176
			$item_line_taxes = maybe_unserialize( $item['line_tax_data'] );
177
			if ( isset( $item_line_taxes['total'] ) ) {
178
				$line_tax = array();
179
180
				foreach ( $item_line_taxes['total'] as $tax_rate_id => $tax ) {
181
					$line_tax[ $tax_rate_id ] = array(
182
						'id'       => $tax_rate_id,
183
						'total'    => $tax,
184
						'subtotal' => '',
185
					);
186
				}
187
188
				foreach ( $item_line_taxes['subtotal'] as $tax_rate_id => $tax ) {
189
					$line_tax[ $tax_rate_id ]['subtotal'] = $tax;
190
				}
191
192
				$line_item['taxes'] = array_values( $line_tax );
193
			}
194
195
			$data['line_items'][] = $line_item;
196
		}
197
198
		$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
199
		$data    = $this->add_additional_fields_to_object( $data, $request );

includes/api/class-wc-rest-orders-controller.php 1 location

@@ 170-234 (lines=65) @@
167
		$data['shipping'] = $order->get_address( 'shipping' );
168
169
		// Add line items.
170
		foreach ( $order->get_items() as $item_id => $item ) {
171
			$product      = $order->get_product_from_item( $item );
172
			$product_id   = 0;
173
			$variation_id = 0;
174
			$product_sku  = null;
175
176
			// Check if the product exists.
177
			if ( is_object( $product ) ) {
178
				$product_id   = $product->id;
179
				$variation_id = $product->variation_id;
180
				$product_sku  = $product->get_sku();
181
			}
182
183
			$meta = new WC_Order_Item_Meta( $item, $product );
184
185
			$item_meta = array();
186
187
			$hideprefix = 'true' === $request['all_item_meta'] ? null : '_';
188
189
			foreach ( $meta->get_formatted( $hideprefix ) as $meta_key => $formatted_meta ) {
190
				$item_meta[] = array(
191
					'key'   => $formatted_meta['key'],
192
					'label' => $formatted_meta['label'],
193
					'value' => $formatted_meta['value'],
194
				);
195
			}
196
197
			$line_item = array(
198
				'id'           => $item_id,
199
				'name'         => $item['name'],
200
				'sku'          => $product_sku,
201
				'product_id'   => (int) $product_id,
202
				'variation_id' => (int) $variation_id,
203
				'quantity'     => wc_stock_amount( $item['qty'] ),
204
				'tax_class'    => ! empty( $item['tax_class'] ) ? $item['tax_class'] : '',
205
				'price'        => wc_format_decimal( $order->get_item_total( $item, false, false ), $dp ),
206
				'subtotal'     => wc_format_decimal( $order->get_line_subtotal( $item, false, false ), $dp ),
207
				'subtotal_tax' => wc_format_decimal( $item['line_subtotal_tax'], $dp ),
208
				'total'        => wc_format_decimal( $order->get_line_total( $item, false, false ), $dp ),
209
				'total_tax'    => wc_format_decimal( $item['line_tax'], $dp ),
210
				'taxes'        => array(),
211
				'meta'         => $item_meta,
212
			);
213
214
			$item_line_taxes = maybe_unserialize( $item['line_tax_data'] );
215
			if ( isset( $item_line_taxes['total'] ) ) {
216
				$line_tax = array();
217
218
				foreach ( $item_line_taxes['total'] as $tax_rate_id => $tax ) {
219
					$line_tax[ $tax_rate_id ] = array(
220
						'id'       => $tax_rate_id,
221
						'total'    => $tax,
222
						'subtotal' => '',
223
					);
224
				}
225
226
				foreach ( $item_line_taxes['subtotal'] as $tax_rate_id => $tax ) {
227
					$line_tax[ $tax_rate_id ]['subtotal'] = $tax;
228
				}
229
230
				$line_item['taxes'] = array_values( $line_tax );
231
			}
232
233
			$data['line_items'][] = $line_item;
234
		}
235
236
		// Add taxes.
237
		foreach ( $order->get_items( 'tax' ) as $key => $tax ) {