Code Duplication    Length = 65-65 lines in 2 locations

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

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

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

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