Skip to content

Instantly share code, notes, and snippets.

@marcusig
Created May 18, 2022 17:01
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 marcusig/7cb84785b0750908be98a03c3436b215 to your computer and use it in GitHub Desktop.
Save marcusig/7cb84785b0750908be98a03c3436b215 to your computer and use it in GitHub Desktop.
Get colors from a product's configurator data
<?php
/**
* Get the hex colors from a product's configurator data
*
* @param int $product_id
* @return array
*/
function get_product_colors_from_configuration( $product_id ) {
$colors = [];
if ( mkl_pc_is_configurable( $product_id ) ) {
$content = mkl_pc( 'db' )->get( 'content', $product_id );
foreach( $content as $content_per_layer ) {
$c = new MKL\PC\Choice( get_the_id(), 0, $content_per_layer['layerId'], 0, 1 );
if ( 'colors' == $c->get_layer( 'display_mode' ) ) {
foreach( $content_per_layer['choices'] as $choice ) {
if ( isset( $choice['color'] ) ) $colors[] = $choice['color'];
};
}
}
}
return $colors;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment