Whatsapp link with current classified url

Hi,

Based on the follow code:

add_filter(
	'hivepress/v1/models/listing/fields',
	function( $fields, $listing ) {
		foreach ( $fields as $name => $field ) {
			if ( isset( $field['display_template'] ) && strpos( $field['display_template'], '%listing_url%' ) !== false ) {
				$fields[ $name ]['display_template'] = str_replace( '%listing_url%', get_permalink( $listing->get_id() ), $field['display_template'] );
			}
		}

		return $fields;
	},
	1000,
	2
);

How to change the snippet to get %listing_url% parameter and insert it on seller’s block area, including listing title on whatsapp message link, for example:

https://wa.me/%wa_number%?text=please+send+me+more+info+about+this+item:+%listing_title%+%listing_url%

Thank you very much!

1 Like

Please try this PHP snippet. It will change the %listing_url% custom token on the listing URL and the %listing_title% custom token on the listing title. Then please try to create a Vendor attribute, choose attribute area as Block (primary) or Block (secondary) to show it on the listing page, and please set the Format attribute option by your requirements

add_filter(
	'hivepress/v1/models/vendor/fields',
	function( $fields, $vendor ) {
		$queried_object = get_queried_object();
		
		if(is_object($queried_object) && 'hp_listing' === $queried_object->post_type){
			$listing = HivePress\Models\Listing::query()->get_by_id( $queried_object->ID );
			
			if($listing){
				foreach ( $fields as $name => $field ) {
				if ( isset( $field['display_template'] ) && strpos( $field['display_template'], '%listing_url%' ) !== false ) {
					$fields[ $name ]['display_template'] = str_replace( ['%listing_url%', '%listing_title%'], [get_permalink( $listing->get_id()), $listing->get_title()], $field['display_template']);
				}
			}	
			}
		}
		

		return $fields;
	},
	1000,
	2
);
1 Like

It works great, but how to avoid it to appears in the seller’s page? Since on it there is no product link nor product title?

The single vendor page doesn’t have the “current listing” context like the listing page does since vendors can have multiple listings (although this page has a “current vendor” context).

Yes, I understand, but I would like to hide the button in vendor page otherwise it will display just the %listing_url% and %listing_title% as plain text, not the correct value because there is no such info on that page. So how to avoid display this in such page?

Please try this PHP snippet instead. This snippet will show your attribute with %listing_url% only in the vendor block on the listing page if you set Block (primary) or/and Block (secondary) as areas for the attribute

add_filter(
	'hivepress/v1/models/vendor/fields',
	function( $fields, $vendor ) {
		$queried_object = get_queried_object();
		
		foreach ( $fields as $name => $field ) {
			if (!isset($field['display_template']) || strpos( $field['display_template'], '%listing_url%' ) === false ) {
				continue;
			}
			
			if(is_object($queried_object) && isset($queried_object->post_type) && 'hp_listing' === $queried_object->post_type){
				$listing = HivePress\Models\Listing::query()->get_by_id( $queried_object->ID );
				
				if($listing){
					$fields[ $name ]['display_template'] = str_replace( ['%listing_url%', '%listing_title%'], [get_permalink( $listing->get_id()), $listing->get_title()], $field['display_template']);
				}
			}else{
				$fields[$name]['_display_areas'] = [];
			}
		}
		

		return $fields;
	},
	1000,
	2
);
2 Likes

Thank you very much!

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