Code Duplication    Length = 41-42 lines in 2 locations

includes/class-wc-ajax.php 2 locations

@@ 1276-1316 (lines=41) @@
1273
	/**
1274
	 * Reduce order item stock.
1275
	 */
1276
	public static function reduce_order_item_stock() {
1277
		check_ajax_referer( 'order-item', 'security' );
1278
		if ( ! current_user_can( 'edit_shop_orders' ) ) {
1279
			die(-1);
1280
		}
1281
		$order_id       = absint( $_POST['order_id'] );
1282
		$order_item_ids = isset( $_POST['order_item_ids'] ) ? $_POST['order_item_ids'] : array();
1283
		$order_item_qty = isset( $_POST['order_item_qty'] ) ? $_POST['order_item_qty'] : array();
1284
		$order          = wc_get_order( $order_id );
1285
		$order_items    = $order->get_items();
1286
		$return         = array();
1287
		if ( $order && ! empty( $order_items ) && sizeof( $order_item_ids ) > 0 ) {
1288
			foreach ( $order_items as $item_id => $order_item ) {
1289
				// Only reduce checked items
1290
				if ( ! in_array( $item_id, $order_item_ids ) ) {
1291
					continue;
1292
				}
1293
				$_product = $order_item->get_product();
1294
				if ( $_product->exists() && $_product->managing_stock() && isset( $order_item_qty[ $item_id ] ) && $order_item_qty[ $item_id ] > 0 ) {
1295
					$stock_change = apply_filters( 'woocommerce_reduce_order_stock_quantity', $order_item_qty[ $item_id ], $item_id );
1296
					$new_stock    = $_product->reduce_stock( $stock_change );
1297
					$item_name    = $_product->get_sku() ? $_product->get_sku() : $_product->id;
1298
1299
					if ( ! empty( $_product->variation_id ) ) {
1300
						$note = sprintf( __( 'Item %s variation #%s stock reduced from %s to %s.', 'woocommerce' ), $item_name, $_product->variation_id, $new_stock + $stock_change, $new_stock );
1301
					} else {
1302
						$note = sprintf( __( 'Item %s stock reduced from %s to %s.', 'woocommerce' ), $item_name, $new_stock + $stock_change, $new_stock );
1303
					}
1304
1305
					$return[]     = $note;
1306
					$order->add_order_note( $note );
1307
				}
1308
			}
1309
			do_action( 'woocommerce_reduce_order_stock', $order );
1310
			if ( empty( $return ) ) {
1311
				$return[] = __( 'No products had their stock reduced - they may not have stock management enabled.', 'woocommerce' );
1312
			}
1313
			echo implode( ', ', $return );
1314
		}
1315
		die();
1316
	}
1317
1318
	/**
1319
	 * Increase order item stock.
@@ 1321-1362 (lines=42) @@
1318
	/**
1319
	 * Increase order item stock.
1320
	 */
1321
	public static function increase_order_item_stock() {
1322
		check_ajax_referer( 'order-item', 'security' );
1323
		if ( ! current_user_can( 'edit_shop_orders' ) ) {
1324
			die(-1);
1325
		}
1326
		$order_id       = absint( $_POST['order_id'] );
1327
		$order_item_ids = isset( $_POST['order_item_ids'] ) ? $_POST['order_item_ids'] : array();
1328
		$order_item_qty = isset( $_POST['order_item_qty'] ) ? $_POST['order_item_qty'] : array();
1329
		$order          = wc_get_order( $order_id );
1330
		$order_items    = $order->get_items();
1331
		$return         = array();
1332
		if ( $order && ! empty( $order_items ) && sizeof( $order_item_ids ) > 0 ) {
1333
			foreach ( $order_items as $item_id => $order_item ) {
1334
				// Only reduce checked items
1335
				if ( ! in_array( $item_id, $order_item_ids ) ) {
1336
					continue;
1337
				}
1338
				$_product = $order_item->get_product();
1339
				if ( $_product->exists() && $_product->managing_stock() && isset( $order_item_qty[ $item_id ] ) && $order_item_qty[ $item_id ] > 0 ) {
1340
					$old_stock    = $_product->get_stock_quantity();
1341
					$stock_change = apply_filters( 'woocommerce_restore_order_stock_quantity', $order_item_qty[ $item_id ], $item_id );
1342
					$new_quantity = $_product->increase_stock( $stock_change );
1343
					$item_name    = $_product->get_sku() ? $_product->get_sku() : $_product->id;
1344
1345
					if ( ! empty( $_product->variation_id ) ) {
1346
						$note = sprintf( __( 'Item %s variation #%s stock increased from %s to %s.', 'woocommerce' ), $item_name, $_product->variation_id, $old_stock, $new_quantity );
1347
					} else {
1348
						$note = sprintf( __( 'Item %s stock increased from %s to %s.', 'woocommerce' ), $item_name, $old_stock, $new_quantity );
1349
					}
1350
1351
					$return[]     = $note;
1352
					$order->add_order_note( $note );
1353
				}
1354
			}
1355
			do_action( 'woocommerce_restore_order_stock', $order );
1356
			if ( empty( $return ) ) {
1357
				$return[] = __( 'No products had their stock increased - they may not have stock management enabled.', 'woocommerce' );
1358
			}
1359
			echo implode( ', ', $return );
1360
		}
1361
		die();
1362
	}
1363
1364
	/**
1365
	 * Calc line tax.