Add custom token to email template

Hi,

a few years ago there was a code snippet released which adds specific tokens to the order completed email. The code snippet added the listing URL token to the Order Completed email.

I just need to add, instead of the listing URL, a token representing a custom attribute of the listing. For example, I created a “Maximum capacity” attribute (slug max_capacity), which is a number. Probably the best way is adding a token representing the listing object (%listing%). Like that, I could reference any attributes like %listing.max_capacity%, etc. (The %listing% token is unfortunately not available in every email template)

This is the php snippet, which is good. Just at the end, instead of the permalink, I would need the whole listing object as a result, or the specific attribute (max_capacity).

add_filter(
	'hivepress/v1/emails/order_complete',
	function( $args ) {
		if ( !isset( $args['tokens']['order_number'] ) ) {
			return $args;
		}
		
		$order_id = intval(ltrim($args['tokens']['order_number'], '#'));
			
		if(!$order_id){
			return $args;
		}
		
		$order = wc_get_order($order_id);
		
		if(!$order){
			return $args;
		}
		
		$item = hivepress()->helper->get_first_array_value( $order->get_items() );

		if ( ! $item || ! $item->get_product_id() ) {
			return;
		}
		
		$product = $item->get_product();
		
		if(!$product){
			return $args;
		}
		
		$listing = null;
		$listing_id = $product->get_parent_id();

		if ( $listing_id ) {
			$listing = \HivePress\Models\Listing::query()->get_by_id( $listing_id );
		}
		
		if(!$listing){
			return $args;
		}
		
		$listing_url = get_permalink( $listing->get_id() );
		
		if(!$listing_url){
			return $args;
		}

		$args['tokens']['listing_url'] = $listing_url;

		return $args;
	},
	1000
);

Also, I would like to add it to the Order received email, instead of the order completed. I guess it would work swapping order_completed with order_received?

Any help?
Thanks!

Hi,

If you have the listing ID in the snippet, you can get the listing object in this way: $listing=\HivePress\Models\Listing::query()->get_by_id(123);

Regarding adding info to the Order received, please provide more details on what exactly you mean, and we will try to help.

Thanks

yes, with this code I showed you I can get the full listing object, but I can’t use it in the Order completed email.

I deleted

$listing_url = get_permalink( $listing->get_id() );
		
		if(!$listing_url){
			return $args;
		}
and then I swapped
$args['tokens']['listing_url'] = $listing_url; for $args['tokens']['listing'] = $listing;

My intention is that the token %listing% would now be usable in the Order Completed email template, but it is not. I am trying with test emais just writing %listing.attribute% (with real attribute names), and no attribute shows.

My other question was:
If hivepress/v1/emails/order_complete works for the Order Complete email template, which terms do I use to add tokens to the Order Received email template?
hivepress/v1/emails/order_received did not work I think

Thanks!

Hi,

If you are referring not to WooCommerce emails, but to those sent to the vendor, we recommend using these hooks:

hivepress/v1/emails/order_received
hivepress/v1/emails/order_completed

Also, if you need to send to an action, you can use the same hooks, just add /send at the end.

Using these hooks, you can add any tokens to the tokens array, based on those that are already available (and you can check which ones are available using: var_dump($email['tokens']) or error_log(print_r($email['tokens'], true)), and then view them in the logs after they are sent).

​I hope this is helpful to you.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.