<cfscript> function getAllVariations( required struct attributes ) { // Helper function to combine arrays of objects private function combineAttributes( required array currentCombinations, required array newAttribute, required string attributeName ) { var newCombinations = []; // For each existing combination for ( var combination in currentCombinations ) { // For each value of the new attribute for ( var value in newAttribute ) { // Create a new struct with the existing combination plus the new attribute // ACF 11-2018 & Lucee 5 & 6 Compatible var newCombination = structCopy( combination ); newCombination[ arguments.attributeName ] = value; arrayAppend( newCombinations, newCombination ); // ACF 2021+ Compatible // newCombinations.append( { ...combination, "#attributeName#": value } ); } } return newCombinations; } // Start with an array containing an empty struct var combinations = [{}]; // Iterate through each attribute type for ( var attributeName in attributes ) { combinations = combineAttributes( combinations, attributes[attributeName], attributeName ); } // Set final price for( var variation in combinations ){ variation[ "cost_adjusted" ] = !variation.keyExists( "cost_adjusted" ) ? variation[ "cost_base" ] : variation[ "cost_adjusted" ]; for ( var key in variation ) { // If option has "cost_adjust" key, add it to the cost_final if ( isStruct( variation[ key ] ) && variation[ key ].keyExists( "cost_adjust" ) ) { variation[ "cost_adjusted" ] += variation[ key ][ "cost_adjust" ]; } } } return combinations; } // Example usage: itemAttributeOptions = { "size": [ {"label": "S", "cost_adjust": 0.00}, {"label": "M", "cost_adjust": 0.00}, {"label": "L", "cost_adjust": 0.00}, {"label": "XL", "cost_adjust": 0.00}, {"label": "2XL", "cost_adjust": 5.00}, {"label": "3XL", "cost_adjust": 5.00} ], "color": [ {"label": "Red", "cost_adjust": 0.00}, {"label": "Blue", "cost_adjust": 0.00}, {"label": "Green", "cost_adjust": 0.00}, {"label": "Black", "cost_adjust": 0.00} ], "sleeve": [ {"label": "Long Sleeve", "cost_adjust": 0.00}, {"label": "Short Sleeve", "cost_adjust": -3.00} ], "cost_base": [9.99], "cost_adjusted": [9.99], "meta": [{"material": "cotton-blend"}] }; allItemVariations = getAllVariations( itemAttributeOptions ); writeDump( allItemVariations ); writeOutput( "Total variations: #allItemVariations.len()#" ); </cfscript>