daviddietz
Forum Replies Created
-
I should probably say that you can include other order statuses as needed in the code:
$allowed_statuses = array('processing', 'completed');
Order Statuses That Will NOT Trigger Notifications, unless they are defined in the array:
- Pending: Orders awaiting payment (e.g., manual bank transfer).
- On Hold: Orders that are paused, often awaiting manual review.
- Cancelled: Orders that have been cancelled by the customer or admin.
- Refunded: Orders that have been fully or partially refunded.
- Failed: Orders where the payment has failed.
I’ve taken the liberty to do this, along with adding additional info (items purchased). I’ve also adjusted the code to run for “Processing and Completed” orders, rather than “Pending.” Here’s the full code for to completely replace “/wp-content/plugins/discord-notifications-for-woocommerce/inc/Core/OrderHandler.php”:
<?php
namespace Kamal\DiscordWooNotif\Core;
if ( ! defined( 'ABSPATH' ) ) exit;
class OrderHandler {
private $processed_orders = array();
public function __construct() {
add_action('woocommerce_order_status_changed', array($this, 'handle_status_change'), 10, 3);
}
public function handle_status_change($order_id, $old_status, $new_status) {
// Only notify for specific statuses
$allowed_statuses = array('processing', 'completed');
if (in_array($new_status, $allowed_statuses)) {
$this->send_notification($order_id, 'status_change', $old_status, $new_status);
}
}
private function send_notification($order_id, $event_type, $old_status = '', $new_status = '') {
// Check if we've already processed this order in this request
if (in_array($order_id, $this->processed_orders)) {
return;
}
if (!get_option('discord_woo_notif_enabled', 0)) {
return;
}
$webhook_url = get_option('discord_woo_notif_webhook_url', '');
if (empty($webhook_url)) {
return;
}
$order = wc_get_order($order_id);
if (!$order) {
$this->send_debug_message($webhook_url, "Order ID #{$order_id} could not be retrieved.");
return;
}
$items = $order->get_items();
$ordered_items = "";
if (!empty($items)) {
foreach ($items as $item_id => $item) {
// Get product name and quantity
$product_name = $item->get_name();
$quantity = $item->get_quantity();
// Get variation details
$meta_data = $item->get_meta_data();
$variation_details = "";
if (!empty($meta_data)) {
foreach ($meta_data as $meta) {
// Exclude irrelevant meta keys. If your notifications are including irrelevant subscription metadata (e.g., _subscription_id or _subscription_renewal), these fields might show up in the Items Ordered section unless explicitly excluded.
$excluded_keys = array('_reduced_stock', 'select-option');
if (in_array($meta->key, $excluded_keys)) {
continue;
}
// Format meta data
$variation_details .= sprintf("%s: %s\n", $meta->key, $meta->value);
}
}
// Combine product details
$ordered_items .= sprintf("%s (x%d)\n%s", $product_name, $quantity, $variation_details);
}
} else {
$ordered_items = __('No items found.', 'discord-notifications-for-woocommerce');
$ordered_items .= "\n\nDebug Info: Raw Items\n" . print_r($items, true);
}
$order_total = html_entity_decode(wp_strip_all_tags(wc_price($order->get_total())));
$embed = array(
'title' => $event_type === 'new'
? sprintf(__('New Order #%s', 'discord-notifications-for-woocommerce'), $order->get_order_number())
: sprintf(__('New Order #%s', 'discord-notifications-for-woocommerce'), $order->get_order_number()),
'url' => $order->get_edit_order_url(),
'color' => 0x00ff00,
'fields' => array(
array(
'name' => __('Status', 'discord-notifications-for-woocommerce'),
'value' => $order->get_status(),
'inline' => true
),
array(
'name' => __('Customer', 'discord-notifications-for-woocommerce'),
'value' => $order->get_formatted_billing_full_name(),
'inline' => true
),
array(
'name' => __('Items Ordered', 'discord-notifications-for-woocommerce'),
'value' => $ordered_items,
'inline' => false
),
array(
'name' => __('Order Total', 'discord-notifications-for-woocommerce'),
'value' => $order_total,
'inline' => true
)
),
'footer' => array(
'text' => get_bloginfo('name'),
'icon_url' => get_site_icon_url()
),
'timestamp' => date('c')
);
$data = array(
'embeds' => array($embed)
);
$args = array(
'body' => wp_json_encode($data),
'headers' => array('Content-Type' => 'application/json'),
'method' => 'POST',
'data_format' => 'body'
);
$response = wp_remote_post($webhook_url, $args);
if (is_wp_error($response)) {
$this->send_debug_message($webhook_url, "Discord Webhook Error: " . $response->get_error_message());
}
$this->processed_orders[] = $order_id;
}
private function send_debug_message($webhook_url, $message) {
$data = array('content' => $message);
$args = array(
'body' => wp_json_encode($data),
'headers' => array('Content-Type' => 'application/json'),
'method' => 'POST',
'data_format' => 'body'
);
wp_remote_post($webhook_url, $args);
}
}@ccsachs The plugin submission is still pending WordPress approval, but you can use the link above and download it and install it manually.
@moswain I just heard back. He said, “I regret there is no progress to report on yet.”
Started a scan at 1:08 PM US/Central Time (13:08 — See the clock time in the bottom right corner of the toolbar).
Scan logs shows it was at 19:10 (6 hours ahead of my time).
WordPress is set to US/Central Time (Chicago).
CleanTalk settings show US and the time zone (should be spelled with two words) as “Easter” (should be spelled “Eastern”). I don’t have the option to change this setting. It’s showing a ?? symbol when my mouse hovers over the select option.
I see it everywhere there are timestamps. Specifically, the “Check users for spam,” it shows once completed and in the logs. I’m guessing you’re located in the U.K. or Europe because you’re 6 hours ahead of me, according to the logs. I just ran a scan (11 AM US/Central Time), and the results show in the logs that it ran at 1700 (6 PM).
Forum: Plugins
In reply to: [Editorial Calendar] WordPress Update Broke PluginHere’s a screenshot:
https://allthenourishingthings.com/wp-content/uploads/2020/09/Capture.pngI’m not sure what’s happening, but it doesn’t seem to load the calendar itself. I’m happy to provide access to the backend.
Forum: Plugins
In reply to: [Editorial Calendar] WordPress Update Broke PluginHi there! First, I’d like to say that I’ve enjoyed using your plugin for several years. Thank you for making such a vital part of my blog freely available! With that said, I’ve been unable to use the plugin recently. I’m not sure if there’s a jQuery conflict or what, but any help would be greatly appreciated.
Edit:
I’m on WP 5.5.1 and using plugin v. 3.7.7- This reply was modified 4 years, 6 months ago by daviddietz.
Done. Deactivated for now.
We only have the 1 custom post type right now, “recipes.”
I see. As of right now, I’m assuming that it’s hurting my SEO by having AMP enabled since Google is seeing (and informing me of) the errors. Would it be your recommendation to switch it off for the time being?
Most are a custom post type (Using the WP Ultimate Recipe Premium).
Forum: Plugins
In reply to: [Shortn.It] Documentation Link not workingI finally got it working.
Process:
In cPanel, add the domain as an “Add-on Domain”. Once it’s been added, go back to cPanel and click on “Redirects”. Redirect the new domain to the primary, and then check the “Wildcard Redirect” checkbox, and click “Add”. That’s it!Forum: Plugins
In reply to: [Shortn.It] Documentation Link not working^^^ What she said. How do I configure my secondary domain to act as the short URL?