Code Duplication    Length = 41-42 lines in 2 locations

includes/class-wc-ajax.php 2 locations

@@ 1304-1344 (lines=41) @@
1301
	/**
1302
	 * Reduce order item stock.
1303
	 */
1304
	public static function reduce_order_item_stock() {
1305
		check_ajax_referer( 'order-item', 'security' );
1306
		if ( ! current_user_can( 'edit_shop_orders' ) ) {
1307
			die(-1);
1308
		}
1309
		$order_id       = absint( $_POST['order_id'] );
1310
		$order_item_ids = isset( $_POST['order_item_ids'] ) ? $_POST['order_item_ids'] : array();
1311
		$order_item_qty = isset( $_POST['order_item_qty'] ) ? $_POST['order_item_qty'] : array();
1312
		$order          = wc_get_order( $order_id );
1313
		$order_items    = $order->get_items();
1314
		$return         = array();
1315
		if ( $order && ! empty( $order_items ) && sizeof( $order_item_ids ) > 0 ) {
1316
			foreach ( $order_items as $item_id => $order_item ) {
1317
				// Only reduce checked items
1318
				if ( ! in_array( $item_id, $order_item_ids ) ) {
1319
					continue;
1320
				}
1321
				$_product = $order_item->get_product();
1322
				if ( $_product->exists() && $_product->managing_stock() && isset( $order_item_qty[ $item_id ] ) && $order_item_qty[ $item_id ] > 0 ) {
1323
					$stock_change = apply_filters( 'woocommerce_reduce_order_stock_quantity', $order_item_qty[ $item_id ], $item_id );
1324
					$new_stock    = $_product->reduce_stock( $stock_change );
1325
					$item_name    = $_product->get_sku() ? $_product->get_sku() : $_product->id;
1326
1327
					if ( ! empty( $_product->variation_id ) ) {
1328
						$note = sprintf( __( 'Item %s variation #%s stock reduced from %s to %s.', 'woocommerce' ), $item_name, $_product->variation_id, $new_stock + $stock_change, $new_stock );
1329
					} else {
1330
						$note = sprintf( __( 'Item %s stock reduced from %s to %s.', 'woocommerce' ), $item_name, $new_stock + $stock_change, $new_stock );
1331
					}
1332
1333
					$return[]     = $note;
1334
					$order->add_order_note( $note );
1335
				}
1336
			}
1337
			do_action( 'woocommerce_reduce_order_stock', $order );
1338
			if ( empty( $return ) ) {
1339
				$return[] = __( 'No products had their stock reduced - they may not have stock management enabled.', 'woocommerce' );
1340
			}
1341
			echo implode( ', ', $return );
1342
		}
1343
		die();
1344
	}
1345
1346
	/**
1347
	 * Increase order item stock.
@@ 1349-1390 (lines=42) @@
1346
	/**
1347
	 * Increase order item stock.
1348
	 */
1349
	public static function increase_order_item_stock() {
1350
		check_ajax_referer( 'order-item', 'security' );
1351
		if ( ! current_user_can( 'edit_shop_orders' ) ) {
1352
			die(-1);
1353
		}
1354
		$order_id       = absint( $_POST['order_id'] );
1355
		$order_item_ids = isset( $_POST['order_item_ids'] ) ? $_POST['order_item_ids'] : array();
1356
		$order_item_qty = isset( $_POST['order_item_qty'] ) ? $_POST['order_item_qty'] : array();
1357
		$order          = wc_get_order( $order_id );
1358
		$order_items    = $order->get_items();
1359
		$return         = array();
1360
		if ( $order && ! empty( $order_items ) && sizeof( $order_item_ids ) > 0 ) {
1361
			foreach ( $order_items as $item_id => $order_item ) {
1362
				// Only reduce checked items
1363
				if ( ! in_array( $item_id, $order_item_ids ) ) {
1364
					continue;
1365
				}
1366
				$_product = $order_item->get_product();
1367
				if ( $_product->exists() && $_product->managing_stock() && isset( $order_item_qty[ $item_id ] ) && $order_item_qty[ $item_id ] > 0 ) {
1368
					$old_stock    = $_product->get_stock_quantity();
1369
					$stock_change = apply_filters( 'woocommerce_restore_order_stock_quantity', $order_item_qty[ $item_id ], $item_id );
1370
					$new_quantity = $_product->increase_stock( $stock_change );
1371
					$item_name    = $_product->get_sku() ? $_product->get_sku() : $_product->id;
1372
1373
					if ( ! empty( $_product->variation_id ) ) {
1374
						$note = sprintf( __( 'Item %s variation #%s stock increased from %s to %s.', 'woocommerce' ), $item_name, $_product->variation_id, $old_stock, $new_quantity );
1375
					} else {
1376
						$note = sprintf( __( 'Item %s stock increased from %s to %s.', 'woocommerce' ), $item_name, $old_stock, $new_quantity );
1377
					}
1378
1379
					$return[]     = $note;
1380
					$order->add_order_note( $note );
1381
				}
1382
			}
1383
			do_action( 'woocommerce_restore_order_stock', $order );
1384
			if ( empty( $return ) ) {
1385
				$return[] = __( 'No products had their stock increased - they may not have stock management enabled.', 'woocommerce' );
1386
			}
1387
			echo implode( ', ', $return );
1388
		}
1389
		die();
1390
	}
1391
1392
	/**
1393
	 * Calc line tax.