Wp wc shop order delete

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen

Remove orders:

NAME

  wp wc shop_order delete

DESCRIPTION

  Delete an existing item.

SYNOPSIS

  wp wc shop_order delete <id> [--id=<id>] [--force=<force>] [--porcelain]

OPTIONS

  <id>
    The ID for the resource.

  [--id=<id>]
    Unique identifier for the resource.

  [--force=<force>]
    Whether to bypass trash and force deletion.

  [--porcelain]
    Output just the id when the operation is successful.

Delete vs. trash

By default, this command moves orders to the trash. To immediately delete orders permanently, use the boolean switch --force. Eg:

wp wc shop_order delete --user=4 --force=1 63003

Delete up to 100 orders at a time

wp wc shop_order list --user=4 --field=id | xargs -n1 wp wc shop_order delete --user=4 --force=1
  • Command wp wc shop_order list returns max. 100 rows at a time
  • It takes approx. 5s/order - Terribly slow for a serious site.

Delete all orders - Easy

To alluviate the two limits mentioned above: Use a loop + parallelisation. Simple example (8 threads):

date
for i in {1..20}
do
	wp wc shop_order list --user=4 --field=id --per_page=100               | xargs -n1 wp wc shop_order delete --user=4 --force=1 &
	wp wc shop_order list --user=4 --field=id --per_page=100 --offset=100  | xargs -n1 wp wc shop_order delete --user=4 --force=1 &
	wp wc shop_order list --user=4 --field=id --per_page=100 --offset=200  | xargs -n1 wp wc shop_order delete --user=4 --force=1 &
	wp wc shop_order list --user=4 --field=id --per_page=100 --offset=300  | xargs -n1 wp wc shop_order delete --user=4 --force=1 &
	wp wc shop_order list --user=4 --field=id --per_page=100 --offset=400  | xargs -n1 wp wc shop_order delete --user=4 --force=1 &
	wp wc shop_order list --user=4 --field=id --per_page=100 --offset=500  | xargs -n1 wp wc shop_order delete --user=4 --force=1 &
	wp wc shop_order list --user=4 --field=id --per_page=100 --offset=600  | xargs -n1 wp wc shop_order delete --user=4 --force=1 &
	wp wc shop_order list --user=4 --field=id --per_page=100 --offset=700  | xargs -n1 wp wc shop_order delete --user=4 --force=1
done
date

Delete all orders - Advanced

This function relies on several global variables (see Initialisation). It uses all available threads. This could be done more efficiently, because the inner loop waits until all threads are finished. Would be nicer if they are filled all the time. But quite good for now:

delete_orders()
{
	#
	# * Probably best to delete orders before deleting customers
	#
	#
	# Initialisation
	########################################
	#
	echo ""; echo ""; echo "### delete_orders()..."; echo ""
	if [ -z "$path" ]; then echo "	Variable 'path' not provided. Exiting"; exit; fi
	if [ ! -f "$path/wp-config.php" ]; then echo "	File ${path}/wp-config.php not found. Exiting"; exit; fi
	if [ -z "$number_of_threads" ]; then echo "	Variable 'number_of_threads' not provided. Exiting"; exit; fi
	if [ -z "$number_of_orders" ]; then echo "	Variable 'number_of_orders' not provided. Exiting"; exit; fi
	#
	cd "$path"


	# Figure out number of outer loops
	########################################
	#
	number_of_outer_loops=$(($number_of_orders/($number_of_threads*100)))
	#
	echo "	Number of orders: $number_of_orders"
	echo "	Number of orders processed by inner loop: $(($number_of_threads*100))"
	echo "	Required number of outer loops: $number_of_outer_loops"


	# Outer loop - Total number of orders
	########################################
	#
	# * Number of times that this loop has to go:
	#   number_of_order/(number_of_threads*100)
	#
	j=1
	for ((j; j<=$number_of_outer_loops; j++))
	do
		#
		# Inner loop - Number of threads
		########################################
		#
		# * E.g., with 16 threads, 1.600 orders are processed in this loop
		#   when this part of the code is processed
		#
		echo "	Outer loop - Counter: $j of $number_of_outer_loops"
		i=0
		for ((i; i<${number_of_threads}; i++))
		do
			#
			# Remove 100 orders - concurrently
			########################################
			#
			wp wc shop_order list --user=4 --field=id --per_page=100 --offset=$(($i*100))  | xargs -n1 wp wc shop_order delete --user=4 --force=1 &
			#
		done
		#
		# Wait!
		########################################
		#
		# * Make sure that the treads above are finished before the next iteration of the outside loop.
		# * Just the loop statements (without '&' of course) don't seem to work like what 'wait' does
		#
		wait
		#
	done	
}

See also