Skip to content

Instantly share code, notes, and snippets.

@levantoan
Last active September 14, 2023 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save levantoan/3e7e9faf841a1ab9e5c6480e80ea2676 to your computer and use it in GitHub Desktop.
Save levantoan/3e7e9faf841a1ab9e5c6480e80ea2676 to your computer and use it in GitHub Desktop.
Add price to Wp Search With Algolia
<?php
/**
* WP Search With Algolia autocomplete template file.
* Edit by levantoan.com
*
* @author WebDevStudios <contact@webdevstudios.com>
* @since 1.0.0
*
* @version 2.5.3
* @package WebDevStudios\WPSWA
*/
?>
<script type="text/html" id="tmpl-autocomplete-header">
<div class="autocomplete-header">
<div class="autocomplete-header-title">{{{ data.label }}}</div>
<div class="clear"></div>
</div>
</script>
<script type="text/html" id="tmpl-autocomplete-post-suggestion">
<a class="suggestion-link" href="{{ data.permalink }}" title="{{ data.post_title }}">
<# if ( data.images.thumbnail ) { #>
<img class="suggestion-post-thumbnail" src="{{ data.images.thumbnail.url }}" alt="{{ data.post_title }}">
<# } #>
<div class="suggestion-post-attributes">
<span class="suggestion-post-title">{{{ data._highlightResult.post_title.value }}}</span>
<# if ( data.price_html) { #>
<span class="suggestion-post-price">{{{ data.price_html }}}</span>
<# } #>
<# if ( data._snippetResult['content'] ) { #>
<span class="suggestion-post-content">{{{ data._snippetResult['content'].value }}}</span>
<# } #>
</div>
<?php
do_action( 'algolia_autocomplete_after_hit' );
?>
</a>
</script>
<script type="text/html" id="tmpl-autocomplete-term-suggestion">
<a class="suggestion-link" href="{{ data.permalink }}" title="{{ data.name }}">
<svg viewBox="0 0 21 21" width="21" height="21">
<svg width="21" height="21" viewBox="0 0 21 21">
<path
d="M4.662 8.72l-1.23 1.23c-.682.682-.68 1.792.004 2.477l5.135 5.135c.7.693 1.8.688 2.48.005l1.23-1.23 5.35-5.346c.31-.31.54-.92.51-1.36l-.32-4.29c-.09-1.09-1.05-2.06-2.15-2.14l-4.3-.33c-.43-.03-1.05.2-1.36.51l-.79.8-2.27 2.28-2.28 2.27zm9.826-.98c.69 0 1.25-.56 1.25-1.25s-.56-1.25-1.25-1.25-1.25.56-1.25 1.25.56 1.25 1.25 1.25z"
fill-rule="evenodd"></path>
</svg>
</svg>
<span class="suggestion-post-title">{{{ data._highlightResult.name.value }}}</span>
</a>
</script>
<script type="text/html" id="tmpl-autocomplete-user-suggestion">
<a class="suggestion-link user-suggestion-link" href="{{ data.posts_url }}" title="{{ data.display_name }}">
<# if ( data.avatar_url ) { #>
<img class="suggestion-user-thumbnail" src="{{ data.avatar_url }}" alt="{{ data.display_name }}">
<# } #>
<span class="suggestion-post-title">{{{ data._highlightResult.display_name.value }}}</span>
</a>
</script>
<script type="text/html" id="tmpl-autocomplete-footer">
<div class="autocomplete-footer">
<div class="autocomplete-footer-branding">
<a href="#" class="algolia-powered-by-link" title="Algolia">
<svg width="150px" height="25px" viewBox="0 0 572 64"><path fill="#36395A" d="M16 48.3c-3.4 0-6.3-.6-8.7-1.7A12.4 12.4 0 0 1 1.9 42C.6 40 0 38 0 35.4h6.5a6.7 6.7 0 0 0 3.9 6c1.4.7 3.3 1.1 5.6 1.1 2.2 0 4-.3 5.4-1a7 7 0 0 0 3-2.4 6 6 0 0 0 1-3.4c0-1.5-.6-2.8-1.9-3.7-1.3-1-3.3-1.6-5.9-1.8l-4-.4c-3.7-.3-6.6-1.4-8.8-3.4a10 10 0 0 1-3.3-7.9c0-2.4.6-4.6 1.8-6.4a12 12 0 0 1 5-4.3c2.2-1 4.7-1.6 7.5-1.6s5.5.5 7.6 1.6a12 12 0 0 1 5 4.4c1.2 1.8 1.8 4 1.8 6.7h-6.5a6.4 6.4 0 0 0-3.5-5.9c-1-.6-2.6-1-4.4-1s-3.2.3-4.4 1c-1.1.6-2 1.4-2.6 2.4-.5 1-.8 2-.8 3.1a5 5 0 0 0 1.5 3.6c1 1 2.6 1.7 4.7 1.9l4 .3c2.8.2 5.2.8 7.2 1.8 2.1 1 3.7 2.2 4.9 3.8a9.7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 1-8.8-14c1.4-3.7 5-6.3 9-6.3h6.4zM291 31.5A32 32 0 0 1 322.8 0h30.8c.6 0 1.2.5 1.2 1.2v61.5c0 1.1-1.3 1.7-2.2 1l-19.2-17a18 18 0 0 1-11 3.4 18.1 18.1 0 1 1 18.2-14.8c-.1.4-.5.7-.9.6-.1 0-.3 0-.4-.2l-3.8-3.4c-.4-.3-.6-.8-.7-1.4a12 12 0 1 0-2.4 8.3c.4-.4 1-.5 1.6-.2l14.7 13.1v-46H323a26 26 0 1 0 10 49.7c.8-.4 1.6-.2 2.3.3l3 2.7c.3.2.3.7 0 1l-.2.2a32 32 0 0 1-47.2-28.6z"></path></svg>
</a>
</div>
</div>
</script>
<script type="text/html" id="tmpl-autocomplete-empty">
<div class="autocomplete-empty">
<?php esc_html_e( 'No results matched your query ', 'wp-search-with-algolia' ); ?>
<span class="empty-query">"{{ data.query }}"</span>
</div>
</script>
<script type="text/javascript">
window.addEventListener('load', function () {
/* Initialize Algolia client */
var client = algoliasearch( algolia.application_id, algolia.search_api_key );
/**
* Algolia hits source method.
*
* This method defines a custom source to use with autocomplete.js.
*
* @param object $index Algolia index object.
* @param object $params Options object to use in search.
*/
var algoliaHitsSource = function( index, params ) {
return function( query, callback ) {
index
.search( query, params )
.then( function( response ) {
callback( response.hits, response );
})
.catch( function( error ) {
callback( [] );
});
}
}
/* Setup autocomplete.js sources */
var sources = [];
algolia.autocomplete.sources.forEach( function( config, i ) {
var suggestion_template = wp.template( config[ 'tmpl_suggestion' ] );
sources.push( {
source: algoliaHitsSource( client.initIndex( config[ 'index_name' ] ), {
hitsPerPage: config[ 'max_suggestions' ],
attributesToSnippet: [
'content:10'
],
highlightPreTag: '__ais-highlight__',
highlightPostTag: '__/ais-highlight__'
} ),
templates: {
header: function () {
return wp.template( 'autocomplete-header' )( {
label: _.escape( config[ 'label' ] )
} );
},
suggestion: function ( hit ) {
if ( hit.escaped === true ) {
return suggestion_template( hit );
}
hit.escaped = true;
for ( var key in hit._highlightResult ) {
/* We do not deal with arrays. */
if ( typeof hit._highlightResult[ key ].value !== 'string' ) {
continue;
}
hit._highlightResult[ key ].value = _.escape( hit._highlightResult[ key ].value );
hit._highlightResult[ key ].value = hit._highlightResult[ key ].value.replace( /__ais-highlight__/g, '<em>' ).replace( /__\/ais-highlight__/g, '</em>' );
}
for ( var key in hit._snippetResult ) {
/* We do not deal with arrays. */
if ( typeof hit._snippetResult[ key ].value !== 'string' ) {
continue;
}
hit._snippetResult[ key ].value = _.escape( hit._snippetResult[ key ].value );
hit._snippetResult[ key ].value = hit._snippetResult[ key ].value.replace( /__ais-highlight__/g, '<em>' ).replace( /__\/ais-highlight__/g, '</em>' );
}
return suggestion_template( hit );
}
}
} );
} );
/* Setup dropdown menus */
document.querySelectorAll( algolia.autocomplete.input_selector ).forEach( function( element ) {
var config = {
debug: algolia.debug,
hint: false,
openOnFocus: true,
appendTo: 'body',
templates: {
empty: wp.template( 'autocomplete-empty' )
}
};
if ( algolia.powered_by_enabled ) {
config.templates.footer = wp.template( 'autocomplete-footer' );
}
/* Instantiate autocomplete.js */
var autocomplete = algoliaAutocomplete( element, config, sources )
.on( 'autocomplete:selected', function ( e, suggestion ) {
/* Redirect the user when we detect a suggestion selection. */
window.location.href = suggestion.permalink;
} );
/* Force the dropdown to be re-drawn on scroll to handle fixed containers. */
window.addEventListener( 'scroll', function() {
if ( autocomplete.autocomplete.getWrapper().style.display === "block" ) {
autocomplete.autocomplete.close();
autocomplete.autocomplete.open();
}
} );
} );
var algoliaPoweredLink = document.querySelector( '.algolia-powered-by-link' );
if ( algoliaPoweredLink ) {
algoliaPoweredLink.addEventListener( 'click', function( e ) {
e.preventDefault();
window.location = "https://www.algolia.com/?utm_source=WordPress&utm_medium=extension&utm_content=" + window.location.hostname + "&utm_campaign=poweredby";
} );
}
});
</script>
<?php
/*
Author: levantoan.com
*/
add_filter( 'algolia_post_product_shared_attributes', 'devvn_add_price_to_algolia', 10, 2 );
add_filter( 'algolia_searchable_product_shared_attributes', 'devvn_add_price_to_algolia', 10, 2 );
function devvn_add_price_to_algolia($shared_attributes, $post){
$product = wc_get_product( $post );
$shared_attributes['price_html'] = $product->get_price_html();
return $shared_attributes;
}
/*
Author: levantoan.com
*/
.suggestion-post-title{
color: #1a1a1a;
}
.aa-dropdown-menu .aa-suggestion em {
background: transparent;
}
.suggestion-post-price {
font-size: 14px;
}
.suggestion-post-price > .amount,
.suggestion-post-price > ins .amount {
color: red;
text-decoration: none
}
.suggestion-post-price > ins{
text-decoration: none
}
.suggestion-post-price del {
font-size: 12px;
color: grey
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment