Last active
August 2, 2019 09:03
-
-
Save forkbombe/7868fa24423d9dc25da91651dfd8f100 to your computer and use it in GitHub Desktop.
Delete WooCommerce Customers with 0 orders using WP-CLI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Download file to WordPress root directory | |
wget https://gist.githubusercontent.com/bambattajb/7868fa24423d9dc25da91651dfd8f100/raw/3b98a06fc84bd2f569ee9829842de29fa15073c5/delete-0-order-customers-wc.sh | |
# Make executable | |
chmod +x delete-0-order-customers-wc.sh | |
# Run | |
./delete-0-order-customers-wc.sh --admin=1 | |
# Params | |
--admin=(int) # Set the admin user id | |
./delete-0-order-customers-wc.sh --admin=1 | |
--allow-root # Force allow root on wp commands | |
./delete-0-order-customers-wc.sh --admin=1 --allow-root |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# TODO: Implement check WP installed in CWD | |
# TODO: Invoke cleanup on CTRL+C | |
declare -i ADMINUSER | |
ADMINUSER=0 | |
ERROR="\033[0;31mError: " | |
SUCCESS="\033[0;32mSuccess: " | |
WORKING="\033[0;34mWorking: " | |
TMPFILENAME="wp-custnoorderlist.txt" | |
# Depends on wp-cli | |
command -v wp >/dev/null 2>&1 || { | |
printf >&2 "$ERROR I require 'wp' but it's not installed.\n" | |
printf "https://wp-cli.org/#installing\n"; | |
exit; | |
} | |
function cleanup { | |
# Remove temp file | |
rm /tmp/$TMPFILENAME | |
} | |
cleanup > /dev/null 2>&1 # do cleanup before | |
# Store CLI args in vars | |
for i in "$@" | |
do | |
case $i in | |
-a=*|--admin=*) | |
ADMINUSER="${i#*=}" | |
shift | |
;; | |
--allow-root) | |
ALLOWROOT="--allow-root" | |
shift | |
;; | |
esac | |
done | |
if [ "${ADMINUSER}" -lt 1 ] | |
then | |
# If admin user not set | |
echo "Please specify an Admin User ID. I.e. --admin=1" | |
exit | |
fi | |
printf "ADMIN USER ID = ${ADMINUSER}\n" | |
printf "ALLOW ROOT = " | |
if [ "${ALLOWROOT}" ] | |
then | |
printf "TRUE\n" | |
else | |
printf "FALSE\n" | |
fi | |
read -p "Is this correct? (select y/N) " -n 1 -r | |
echo # move to a new line | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
printf "$WORKING Started...\n" | |
#if ! $(wp db create > /dev/null 2>&1) | |
#then | |
# printf "$ERROR Cannot connect to database\n" | |
# exit | |
#fi | |
#if ! $(wp core is-installed > /dev/null 2>&1) | |
#then | |
# printf "$ERROR Wordpress not installed here\n" | |
# exit | |
#fi | |
## | |
# Start by storing all CUSTOMER IDs into a temp file | |
##################################################### | |
# calculate number of pages and round up https://stackoverflow.com/questions/13182070/best-way-to-divide-in-bash-using-pipes | |
PAGES=$(wp user list --role=customer $ALLOWROOT | wc -l | awk '{print $1/100}' | awk '{print int($1+0.99)}') | |
# create the list of users and loop through each page | |
for i in $(seq 1 $PAGES); do | |
# redirect stdout to stderr so the text doesn't show up in our text file | |
printf "$WORKING Processing page ${i} of ${PAGES}\n" 1>&2 | |
# get the user ID and the number of orders per user, only show IDs that have 0 as order count with awk https://stackoverflow.com/questions/14739057/awk-print-column-3-if-2-a-specific-value | |
wp wc customer list --fields=id,orders_count $ALLOWROOT --user=${ADMINUSER} --page=${i} | awk '$2 == "0" {print $1}' | |
done > /tmp/$TMPFILENAME | |
printf "$SUCCESS Created user list\n" | |
## | |
# Delete CUSTOMER IDs in temp file | |
################################## | |
USERCOUNT=$(cat /tmp/$TMPFILENAME | wc -l) | |
COUNT=1 | |
read -p "$USERCOUNT customers found with no orders. Would you like to remove them? (select y/N)" -n 1 -r | |
echo # move to a new line | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
while read -r USER | |
do | |
printf "$WORKING Deleting user_id ${USER} which is number ${COUNT} of ${USERCOUNT}\n" | |
# delete customer | |
wp wc customer delete ${USER} --user=${ADMINUSER} --force=true $ALLOWROOT | |
# increment the COUNT | |
((COUNT++)) | |
done < /tmp/$TMPFILENAME | |
else | |
printf "Cancelled...\n" | |
fi | |
else | |
printf "Cancelled...\n" | |
fi | |
cleanup > /dev/null 2>&1 | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment