31. Jan 2024 |

Empty actionscheduler_logs quickly

Just these cli commands help you out.

wp db query "SELECT * FROM wp_actionscheduler_logs LIMIT 1"

wp db query "DELETE FROM wp_actionscheduler_logs"

wp db query "DELETE FROM wp_actionscheduler_actions WHERE status = 'complete'"– you can modify this as you want.

7. Aug 2023 |

WPML and WP-CLI – how to delete products

Whey I tried to delete products using WP-CLI then products only in default language were deleted. Products in other languages could not be deleted using wp post delete. I used the following command:

wp post delete $(wp wc product list --user=1 --format=ids --per_page=10) --force

More information about deleting WooCommerce products using wp-cli can be found there.

In order to delete products in all languages this needs to be checked: WPML → Settings → “When deleting a post, delete translations as well”.

7. May 2023 |

wp cli delete orders older than…

Overview of WooCommerce CLI can be found here and examples here.

Delete shop order by ID, need to add user id that has permissions to delete orders.

wp wc shop_order delete <id> --user=1

Delete orders where date is older than…

for id in $(wp db query "SELECT id FROM wp_posts WHERE post_date < '2021-01-01' AND post_type='shop_order'" --skip-column-names); do wp wc shop_order delete $id --user=1 --force=1; done

21. Mar 2023 |

wp cli to delete unattached images

Yes, I did it. I imported new images with every product import so now I have 15000+ images in my media library.

for id in $(wp db query "SELECT id FROM wp_posts WHERE post_date <= '2023-03-20' AND post_date >= '2023-03-15' AND post_type='attachment' AND post_parent=0 AND post_author NOT IN(1,4)" --skip-column-names); do wp post delete --force $id; done

This is the wp cli command that does the job. I found it here.

Just to see how many images will be deleted you can use following query:

wp db query "SELECT COUNT(id) FROM wp_posts WHERE post_date <= '2023-03-20' AND post_date >= '2023-03-01' AND post_type='attachment' AND post_parent = 0 AND post_author NOT IN (1,4)"

You can modify post_author IDs and date ranges as you like. For testing you can as LIMIT parameter to the query.

4. Jun 2022 |

WooCommerce – add product custom field to order email

What you need, is to use a hook and a function to modify e-mail template. That is it.

First idea probably would be changing the template. In this file: wp-content/plugins/woocommerce/templates/emails/email-order-items.php

First $product = $item->get_product(); and then $key = $product->get_meta('custom_field_key'); and then this thread was helpful but this is the hook and action were what I really needed.

Also useful:

'customer_completed_order' === $email->id and order email ids that are available.

I order to test e-mails locally Sendinblue works really well as SMTP and is simpler to set up than Sendgrid.

12. May 2022 |

FacetWP reindex products automatically (simple solution with wp-cli + cron)

FacetWP had introduced WP-CLI interface which I was very much looking for. Just run wp facetwp index and reindex for the entire site will be created.

I just had a moment in one of my online stores where I saw that the index was last built 8 months ago. Now can add this line to WP All Import bash script and index will be built quickly every time. Or run it once day for example together with daily backup. I find it very convenient.

9. Nov 2019 |

Bulk delete WooCommerce products by category using WP-CLI

If you would like to know how to bulk delete posts from a specific category using the WP-CLI then here it is.

WP-CLI is command line interface for maintenance of WordPress. This is something I try to figure out.

Deleting one product

This deletes 1 product. 1157 is product ID. Without –force it does not work, because posts of type ‘product’ do not support being sent to trash.
So you need to use –force flag to skip trash and delete them permanently.

wp post delete 1157 --force

Deleting 100 products

Now I try to figure out how can I delete all products within category. This allows to delete 100 products at a time.

wp post delete $(wp wc product list --user=1 --format=ids --category=64) --force

Deleting specific number of products

wp post delete $(wp wc product list --user=1 --format=ids --category=64 --per_page=10) --force

By specifying –per_page you can limit how many products will be deleted with one execution. But it only works between 1 and 100 products.

Reason is that WooCommerce commands are implement as REST API requests and they are limited to a maximum of 100 results per request. This is a limitation that comes from WooCommerce, not WP-CLI. It’s unfortunate they have limited it that way, as it seriously lessens the benefits of having CLI commands in the first place.

Final solution that works (tested with 5000+ products)

for run in {1..2}; do wp post delete $(wp wc product list --user=1 --format=ids --category=64 --per_page=10) --force; done

This line does the job. Loops I found here and here. This sample deletes 20 products by using two runs, 10 products each. And this deletes products with status as “draft” (reference):

for run in {1..2}; do wp post delete $(wp wc product list --user=1 --format=ids --status=draft --per_page=10) --force; done

Delete products by product tag

Another one is here. tag is product tag ID.

wp post delete $(wp wc product list --user=1 --format=ids --tag=82 --per_page=2) --force

Deleting revisions

It is not uncommon sites to have multiple revisions on their pages. This is simply due editing and updating content. Let’s say for example a site has 700 pages or posts with 150 revisions on each, this would be over 100,000 entries in the database. To delete unnecessary revisions you can use the following command line:

wp post delete $(wp post list --post_type='revision' --format=ids) --force

I have posted the question also to WordPress.org forum here for discussion and #cli channel. Hopefully someone comes up with the answer 🙂 Also sent it to Eric. Maybe he knows.

Bonus:
WP-allimport faster finder: [wc_get_product_id_by_sku({sku[1]})]
Delete all images in media library: wp post delete --force $(wp post list --post_type='attachment' --format=ids)

2. Sep 2019 |

Location of WooCommerce language file

In order to modify strings you need to find translation file. For plugins these are usually here.

/htdocs/wp-content/languages/plugins

In case of WooCommerce please take following language files from this folder.

woocommerce-et.mo
woocommerce-et.po

You can edit these by using Poedit. Install Poedit to your computer and open .po file. Translate or modify the string accoringly and then save.

/htdocs/wp-content/languages/woocommerce – this is correct folder where edited language files should be uploaded in case of WooCommerce IF you do not want to lose translations during plugin update.

But, if the string is not available like here and here there may be different ways to fix it.