双重发布自定义元数据wordpress [英] Double posting the custom meta data wordpress

查看:56
本文介绍了双重发布自定义元数据wordpress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的插件从订单中的自定义元数据中提取值并将其发布到discord。我对脚本进行了更改,现在它将双重发布自定义元数据。



上传脚本的完整文件以及仅包含脚本的示例文件循环。循环也可以在完整的文件中找到。



在22行我改变了



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 );



一旦我改变状态woocommerce_order_status_processing,我的for循环开始双重发布所有内容。我需要双重发布才能停止。



不和谐帖子的外观示例



Bob Joe< ; - 客户名称



订单#123123< - 订单编号



全新T恤< - 产品名称



颜色:红色< - 应该发布什么



尺寸:中等



颜色:红色< - 双贴



尺寸中等



只有自定义选项加倍并且只有一个过滤器集。我需要过滤器设置,或者至少有一个过滤器设置为处理时触发。



解决方案只应在设置为处理时触发。



这是我的完整代码




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();





我尝试了什么:



i将woocommerce_order_status_processing更改为woocommerce_order_status_completed但错误仍然相同



What I have tried:

i changed the woocommerce_order_status_processing to woocommerce_order_status_completed but the error remained same

推荐答案

this,'send_order'),15);
this, 'send_order' ), 15 );











To

add_action( 'woocommerce_order_status_processing', array(


this,'send_order'),15);
this, 'send_order' ), 15 );



一旦我改变状态woocommerce_order_status_processing,我的for循环开始双重发布所有内容。我需要双重发布才能停止。



不和谐帖子的外观示例



Bob Joe< ; - 客户名称



订单#123123< - 订单编号



全新T恤< - 产品名称



颜色:红色< - 应该发布什么



尺寸:中等



颜色:红色< - 双贴



尺寸中等



只有自定义选项加倍并且只有一个过滤器集。我需要过滤器设置,或者至少有一个过滤器设置为处理时触发。



解决方案只应在设置为处理时触发。



这是我的完整代码




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);

}
}



/ **
*使用指定的webhook URL和Bot将订单发送到Discord令牌。
*
* @param int
this, 'send_order' ), 15 ); } } /** * Sends the order to Discord using the specified webhook URL and Bot token. * * @param int


这篇关于双重发布自定义元数据wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆