Created
May 18, 2022 17:01
-
-
Save marcusig/7cb84785b0750908be98a03c3436b215 to your computer and use it in GitHub Desktop.
Get colors from a product's configurator data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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