Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My plugin pulls values from custom meta data on an order and posts it to discord. I made a change to the script and now it's double posting the custom meta data.

The full file of the script is uploaded as well as an example file that contains just the loop. The loop can be found in the full file as well.

On line 22 I changed

add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'send_order' ), 15 );


To

add_action( 'woocommerce_order_status_processing', array( $this, 'send_order' ), 15 );

Once I change to the status woocommerce_order_status_processing my for loop starts double posting everything. I need the double posting to stop.

Example of how the discord post looks

Bob Joe <-- clients name

Order #123123 <-- order number

Brand New Tee Shirt <-- Product Name

Color: Red <-- What should post

Size: Medium

Color: Red <-- Double post

Size Medium

Only custom options double up and only with that one filter set. I need that filter set though or at least one that triggers when a post is set to processing.

Solution should only trigger when set to processing.

Here is my complete code

<?php
/**
 * WP Discord Post WooCommerce
 *
 * @author      Nicola Mustone
 * @license     GPL-2.0+
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Main class of the compatibility with WooCommerce.
 */
class WP_Discord_Post_WooCommerce {
	/**
	 * Adds the required hooks.
	 */
	public function __construct() {
		if ( 'yes' === get_option( 'wp_discord_enabled_for_woocommerce' ) ) {
			add_action( 'woocommerce_order_status_processing', array( $this, 'send_order' ), 15 );

		}
	}



	/**
	 * Sends the order to Discord using the specified webhook URL and Bot token.
	 *
	 * @param int $order_id The order ID.
	 */
	public function send_order( $order_id ) {
		$order            = wc_get_order( $order_id );

		$content          = $this->_prepare_order_content( $order );
		$embed            = array();

		if ( ! wp_discord_post_is_embed_enabled() ) {
			$embed   = $this->_prepare_order_embed( $order_id, $order );
		}

		//todo:: Need to support multiple orders.
		$tm_option = $this->_get_tm_option($order->get_items());

		$http = new WP_Discord_Post_HTTP( 'post', $tm_option);
		return $http->process( $content, $embed );
	}


	/**
	 * Prepares the request content for orders.
	 *
	 * @param  object $order The order object.
	 * @return string
	 */
	protected function _prepare_order_content( $order ) {
		$order_number   = strip_tags( $order->get_order_number() );
		$order_total    = html_entity_decode( strip_tags( $order->get_formatted_order_total() ) );

		$mention_everyone = get_option( 'wp_discord_post_mention_everyone' );
		$message_format   = get_option( 'wp_discord_order_message_format' );

		$content = str_replace(
			array( '%order_number%', '%order_total%' ),
			array( $order_number, $order_total ),
			$message_format
		);

		if ( empty( $content ) ) {
			$content = sprintf( esc_html__( 'Order #%1$s has been created. The order total is %2$s.', 'wp-discord-post' ), $order_number, $order_total );
		}

		if ( 'yes' === $mention_everyone && false === strpos( $content, '@everyone' ) ) {
			$content = '@everyone ' . $content;
		}

		$content = apply_filters( 'wp_discord_post_woocommerce_order_content', $content, $order );

		return $content;
	}

	/**
	 * Prepares the embed for the the order.
	 *
	 * @access protected
	 * @param  int    $order_id The order ID.
	 * @param  object $order    The order object.
	 * @return array
	 */
	protected function _prepare_order_embed( $order_id, $order ) {
		$embed = array(
			'title'       => sprintf( esc_html__( 'Order #%d', 'wp-discord-post' ), strip_tags( $order->get_order_number() ) ),
			'url'         => $order->get_edit_order_url(),
			'timestamp'   => get_the_date( 'c', $order_id ),
			'author'      => esc_html( $order->get_formatted_billing_full_name() ),
			'fields'      => array(),
		);



		$items = $order->get_items();
		foreach($items as $i)
		{
			$tm_extra_options = '';
			$meta = $i->get_meta_data();
			foreach( $meta as $m)
			{
				$meta_array = $m->get_data();
				if ($meta_array['key'] == '_tmcartepo_data')
				{
					foreach($meta_array['value'] as $mv)
					{
						if (!empty($mv['name']))
						{
							$name = $mv['name'];
							$value = $mv['value'];
							if (strpos($value, "Priority - Fast Completion") !== false) {
    						$name = str_replace("Extra Options", '', $name);
							}
							if (strpos($name, "Platform") !== false) {
							$name = str_replace("Platform", '', $name);
							}
						} else {
							$name = "(No Name)";
						}

						if (!empty($mv['value'])){
							$value = $mv['value'];
							if (strpos($value, "Priority - Fast Completion") !== false) {
							$value = str_replace("Priority - Fast Completion", '', $value);
							}
						} else {
							$value = "(No Value)";
						}
						if (!empty($name) && (!empty($value))){
							$tm_extra_options .= $name . ": " . $value . "\n";
						}
					}
				}
			}
			//break;

			$embed['fields'][] = array(
				'name'   => esc_html__($i->get_name(), 'wp-discord-post' ),
				'value'  => esc_html__($tm_extra_options),
			);
		}


		$embed = apply_filters( 'wp_discord_post_order_embed', $embed, $product );

		return $embed;
	}

	public function _get_tm_option($order_items)
	{
		$tc_type_array = array();
		$tc_label = get_option('wp_discord_post_settings_webhooks_tm_target_label');
		foreach($order_items as $item)
		{
			$meta = $item->get_meta_data();
			foreach( $meta as $m)
			{
				$meta_array = $m->get_data();
				if ($meta_array['key'] == '_tmcartepo_data')
				{
					foreach($meta_array['value'] as $mv)
					{
						if ($mv['name'] == $tc_label)
						{
							$tc_type_array[] =  strtolower($mv['value']);
						}
					}
				}
			}
		}

		if (count(array_unique($tc_type_array)) === 1)
		{
			return $tc_type_array[0];
		}
		else {
			return 'other';
		}
	}
}

return new WP_Discord_Post_WooCommerce();


What I have tried:

i changed the woocommerce_order_status_processing to woocommerce_order_status_completed but the error remained same
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900