Code Duplication    Length = 41-42 lines in 2 locations

includes/class-wc-ajax.php 2 locations

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