WooCommerce Product Variations Dynamic Tag Outputs All variations for Bricks Builder

This custom dynamic tag solution displays all WooCommerce product variations as a simple text list on your website.

Can be used inside a product loop or single product page output is just text slug list like variation1, variation2…etc.

{woo_product_all_attributes}

<?php
/**
 * WooCommerce Product Variations Dynamic Tag for Bricks Builder
 * Outputs all product variations as a simple text list
 * {woo_product_all_attributes}
 */

// Step 1: Register the dynamic tag in Bricks Builder
add_filter( 'bricks/dynamic_tags_list', 'add_woo_variations_tag_to_builder' );
function add_woo_variations_tag_to_builder( $tags ) {
    $tags[] = [
        'name'  => '{woo_product_all_attributes}',
        'label' => 'WooCommerce Product All Variations',
        'group' => 'WooCommerce',
    ];

    return $tags;
}

// Step 2: Handle tag rendering when called specifically
add_filter( 'bricks/dynamic_data/render_tag', 'get_woo_variations_tag_value', 20, 3 );
function get_woo_variations_tag_value( $tag, $post, $context = 'text' ) {
    if( ! is_string( $tag ) ) {
        return $tag;
    }

    // Clean the tag name
    $clean_tag = str_replace( [ '{', '}' ], '', $tag );

    // Only process our specific tag
    if ( $clean_tag !== 'woo_product_all_attributes' ) {
        return $tag;
    }

    // Get the variations list
    $value = get_woo_product_variations_list( $post );

    return $value;
}

// Step 3: Handle content rendering for mixed content
add_filter( 'bricks/dynamic_data/render_content', 'render_woo_variations_tag', 20, 3 );
add_filter( 'bricks/frontend/render_data', 'render_woo_variations_tag', 20, 2 );
function render_woo_variations_tag( $content, $post, $context = 'text' ) {
    // Check if our tag exists in the content
    if ( strpos( $content, '{woo_product_all_attributes}' ) === false ) {
        return $content;
    }

    // Get the variations list
    $variations_output = get_woo_product_variations_list( $post );

    // Replace the tag with the actual content
    $content = str_replace( '{woo_product_all_attributes}', $variations_output, $content );

    return $content;
}

// Main function to get variations as simple text list
function get_woo_product_variations_list( $post = null ) {
    // Return early if WooCommerce is not active
    if ( ! class_exists( 'WooCommerce' ) ) {
        return '';
    }

    // Get current post ID
    if ( $post ) {
        $product_id = $post->ID;
    } else {
        $product_id = get_the_ID();
    }

    // Return empty if no product ID
    if ( ! $product_id ) {
        return '';
    }

    // Get the product using official WooCommerce function
    $product = wc_get_product( $product_id );

    // Return empty if no product found or not a variable product
    if ( ! $product || ! $product->is_type( 'variable' ) ) {
        return '';
    }

    $variations_list = array();

    // Get all variation IDs using official method
    $variation_ids = $product->get_children();
    
    foreach ( $variation_ids as $variation_id ) {
        // Get variation object using official function
        $variation = wc_get_product( $variation_id );
        
        if ( ! $variation || ! $variation->exists() ) {
            continue;
        }
        
        // Get variation attributes using official method
        $variation_attributes = $variation->get_variation_attributes();
        $variation_parts = array();
        
        foreach ( $variation_attributes as $attr_name => $attr_value ) {
            if ( $attr_value ) {
                $variation_parts[] = $attr_value;
            }
        }
        
        if ( ! empty( $variation_parts ) ) {
            $variations_list[] = implode( ' - ', $variation_parts );
        }
    }

    // Return comma-separated list
    return implode( ', ', $variations_list );
}