Code Duplication    Length = 41-42 lines in 2 locations

includes/class-wc-ajax.php 2 locations

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