Send an email when I, as administrator, delete the ad of a seller who has already posted an ad online

Hello, I would like to know if there is a solution for automatically sending an information email when I delete a seller’s ad that was online.

The purpose of this is to warn the seller that their ad has been removed from the site due to non-compliance with certain rules of use.
It’s a bit like a moderation email.

I’ve already set up an email to be sent when a seller submits an ad (not yet online) and I reject it, but I can’t find anything for an ad that’s already online.

Thank you very much for your help

All the best,

Hi,

Unfortunately, there is no such automatic email, it will require a custom implementation. If you are familiar with coding, we can provide general guidance.

Hello,

Thank you for your reply!

Yes I’m a developer, I’m familiar with JavaScript HTML/CSS, I guess I have to do the implementation with PHP, but it should be fine with ChatGPT :slight_smile:

Thanks for your advice !

1 Like

I’d be happy to take your general guidance on how to implement it. :innocent:

Hi,

You can use this hook hivepress/v1/models/listing/update_status and check if old status publish and new status trash, then use the wp_mail function to send an email.

I’m currently facing an issue when trying to send an email to the listing owner when a published listing is deleted (moved to trash). While I think I’m close to achieving my goal, I’m encountering a few problems that I can’t resolve on my own. I hope someone can help!

What I Want to Achieve

When an admin deletes a listing (moving it from “publish” to “trash”), an email should be sent to the listing owner informing them of the deletion. This is intended to notify the user about their listing status.

The Problem

  1. Status Transitions Are Inconsistent or Reversed:
    When tracking the old and new status of a listing, I noticed that the transitions are inverted. For example:
  • Moving from “draft” to “publish” appears in logs as “publish” to “draft”.
  • Similarly, moving from “publish” to “trash” is logged as “trash” to “publish”.
  • This makes it a bit tricky to identify the correct transition, but I’ve accounted for this inversion in my logic. Below are some example logs:Logs for Listing ID 1416:
[28-Nov-2024 14:42:50 UTC] Debug: Listing ID: 1416, Old Status: auto-draft, New Status: new
[28-Nov-2024 14:43:12 UTC] Debug: Listing ID: 1416, Old Status: pending, New Status: auto-draft
[28-Nov-2024 14:43:23 UTC] Debug: Listing ID: 1416, Old Status: publish, New Status: pending
[28-Nov-2024 14:43:30 UTC] Debug: Listing ID: 1416, Old Status: trash, New Status: publish

As you can see:

  • “publish” to “pending” might actually represent moderation approval.
  • “trash” to “publish” is where I intend to send the email (as it seems reversed).
  1. Cannot Retrieve the Listing Owner’s Email:
    When I attempt to fetch the listing owner’s email during the “trash” → “publish” transition (to send the email), I receive this error:
Debug: No user found for Listing ID: 1416.

I’ve tried fetching the owner’s email using $listing->get_owner_id() and get_userdata(), but something seems to be going wrong. I suspect the issue might be in how the owner is linked to the listing.

The Code I’m Using

Here’s the code I’ve implemented so far:

add_action('hivepress/v1/models/listing/update_status', function($listing_id, $old_status, $new_status) {
    // Log transitions for debugging
    error_log("Debug: Listing ID: $listing_id, Old Status: $old_status, New Status: $new_status");

    // Check if the listing transitions from "trash" to "publish" (reversed logic)
    if ($old_status === 'trash' && $new_status === 'publish') {
        // Get the listing object
        $listing = new HivePress\Models\Listing($listing_id);

        // If the listing is found
        if ($listing) {
            // Retrieve the owner's email
            $user = get_userdata($listing->get_owner_id());
            if ($user) {
                $user_email = $user->user_email;

                // Prepare the email
                $subject = "Your listing is now live";
                $message = "Hello,\n\nYour listing titled '{$listing->get_title()}' is now live.\n\nThank you for your understanding.\n\nThe Lig'Annonces Team.";
                $headers = ['Content-Type: text/plain; charset=UTF-8'];

                // Send the email via WP Mail SMTP
                wp_mail($user_email, $subject, $message, $headers);
                error_log("Debug: Email sent to $user_email.");
            } else {
                error_log("Debug: No user found for Listing ID: $listing_id.");
            }
        }
    }
}, 10, 3);

What I Need Help With

  1. Is there a better or more reliable way to retrieve the listing owner’s email?
  • $listing->get_owner_id() seems to fail in my case. Am I using it incorrectly?
  1. Why are the status transitions reversed in HivePress?
  • Is this expected behavior, or might there be something wrong with my setup?
  1. Is there anything I’ve missed in my logic or code that could improve the reliability of this feature?

Any advice or help would be greatly appreciated. Thanks in advance! :blush:

Sorry for the delay.

Please change this line:

}, 10, 3);

to:

}, 10, 4);

Then add the 4th $listing argument to the function brackets, then the listing object will be available by default, and you’ll be able to get the user email and send an email with wp_mail function:

$listing->get_user__email()

Currently the listing object is fetched incorrectly.