Gists

4 weeks ago
<?php
// Creates a custom dynamic tag {{ my-color-swatches }} to use in the Listing Builder, that displays color swatches for each product.
// This snippet works with swatches from Iconic's WooCommerce Attribute Swatches plugin and colors set as product attribute.
// See: https://facetwp.com/help-center/using-facetwp-with/woocommerce-attribute-swatches/#create-a-custom-dynamic-tag-to-output-color-swatches-for-each-product

add_filter( 'facetwp_builder_dynamic_tag_value', function( $tag_value, $tag_name, $params ) {
    
  // Replace 'my-color-swatches' with the name of your custom dynamic tag name . 
  if ( 'my-color-swatches' == $tag_name ) { 
    
    // Replace 'pa_color' with the name of the WooCommerce attribute used for the product color. 
    // Note that 'pa_' is automatically added by WooCommerce before the attribute's slug.  
    $terms = wp_get_post_terms( $params['post']->ID, 'pa_color', array( 'fields' => 'ids' ) );
    $tag_value = '';
    foreach($terms as $term_id) {
      $color_code = get_term_meta($term_id, 'iconic_was_term_meta', true);
      $color_code = maybe_unserialize($color_code);
      $color_code = $color_code['colour-swatch'];
      if ($color_code) {
        $tag_value .= '<div class="color-swatch" style="background-color: ' . $color_code . '; width: 24px; height:24px; display: inline-block; margin: 0 12px 0 0;"></div>'; // Adapt the CSS as needed.
      }
    }
  }
 
  return $tag_value;
}, 10, 3 );
3 months ago
<?php

// Creates a custom dynamic tag {{ childterms }} to display post terms of a specific taxonomy in a Listing Builder (HTML) item.
// Excludes top-level parent terms, so shows child terms only.
// Displays the term names in a comma-separated list.
// Replace 'my_taxonomy' with the name of your taxonomy.
// Use the custom dynamic tag in a (HTML) builder item like this: {{ childterms }}


add_filter( 'facetwp_builder_dynamic_tag_value', function( $tag_value, $tag_name, $params ) {
  if ( 'childterms' == $tag_name ) {
    $taxonomy = 'my_taxonomy';
    $terms = wp_get_post_terms( $params['post']->ID,  $taxonomy );
    $child_terms = array();

    foreach ( $terms as $term ) {
      $parent_id = $term->parent;
      if ( $parent_id !== 0 ) { // Exclude top-level parent terms (that have a parent ID of 0).
        $child_terms[] = $term->name; // Get term names
      }
    }
    $tag_value = implode( ', ', $child_terms ); // Output the term names as a comma-separated list

  }
  return $tag_value;
}, 10, 3 );
8 months ago
<?php
/** use dynamic tags to translate words in layout builder
 **/
add_filter( 'facetwp_builder_dynamic_tag_value', function( $tag_value, $tag_name, $params ) {
    if ( 'prefix:autor' == $tag_name ) {
        $lang = ( !empty( FWP()->facet->http_params['lang'] ) ) ? FWP()->facet->http_params['lang'] :  apply_filters( 'wpml_current_language', null );  
        switch ( $lang ) {
            case 'en' :
                $tag_value = 'Author';
                break;
			default :
				$tag_value = 'Autor';
        }
    }
    return $tag_value;
}, 10, 3 );
1 year ago
<?php
/** Creates a dynamic tag for use in the Listing Builder
 ** https://facetwp.com/help-center/developers/hooks/output-hooks/facetwp_builder_dynamic_tag_value/
 ** see https://facetwp.com/help-center/facets/facet-types/proximity/#display-the-post-distance
 ** for how to customize this output
 ** To use this dynamic tag in the Listing Builder, create a HTML element and add the tag to the "Content" field. E.g. <div class="distance"> {{ distance }} </div>
 ** Note that the distance will only be output when the Proximity facet is actually in use.
 **/
add_filter( 'facetwp_builder_dynamic_tag_value', function( $tag_value, $tag_name, $params ) {
    if ( 'distance' == $tag_name ) {
        $distance = facetwp_get_distance();
        // Round distance to 2 decimals and append ' mi'
        if ( false !== $distance ) {
            $tag_value = round( $distance, 2 ) . ' mi';
        } 
    }
    return $tag_value;
}, 10, 3 );
1 year ago
// Creates custom dynamic tag {{ resource_type }} that outputs term slugs to be used as classes in a HTML builder element in a Listing Builder listing
// <div class="{{ post:type }} {{ resource_type }}"></div>

add_filter( 'facetwp_builder_dynamic_tag_value', function( $tag_value, $tag_name, $params ) {

  if ( 'resource_type' == $tag_name ) {
    $terms = wp_get_post_terms( $params['post']->ID, 'resource-type', array( 'fields' => 'slugs' ) );
    $tag_value = implode( ' ', $terms );
  }

  return $tag_value;
}, 10, 3 );
2 years ago
<?php
/** creates a dynamic tag for use in the facetwp layout builder - {{ cart }} **/

add_filter( 'facetwp_builder_dynamic_tag_value', function( $tag_value, $tag_name, $params ) {
    if ( 'cart' == $tag_name ) {
        global $product;
        $product = wc_get_product( get_the_ID() );
        ob_start();
        woocommerce_template_loop_add_to_cart();
        $tag_value = ob_get_clean();
    }
    return $tag_value;
  }, 10, 3 );
2 years ago
<?php

/** get a user acf field for the post author
 ** use as {{ acf:author_title }} in an HTML item in layout builder
 **/

add_filter( 'facetwp_builder_dynamic_tag_value', function( $tag_value, $tag_name, $params ) {
  if ( 'acf:author_title' == $tag_name ) {
      $author = get_post_field( 'post_author', $params['post']->ID );
      $tag_value = get_field( 'first_name', 'user_' . $author );
  }
  return $tag_value;
}, 10, 3 );