Skip to content

Instantly share code, notes, and snippets.

@andyleach
Last active January 13, 2017 08:17
Show Gist options
  • Save andyleach/b7a0e476f1118854b0dd to your computer and use it in GitHub Desktop.
Save andyleach/b7a0e476f1118854b0dd to your computer and use it in GitHub Desktop.
WooCommerce Product Category Swatches
<?php
function display_product_colors() {
$colors = array();
$options = get_post_meta(get_the_ID(), '_swatch_type_options', true);
foreach($options as $option) {
foreach($option as $item) {
foreach($item as $att) {
$colors[] = $att['color'];
}
}
}
$colors = array_unique($colors);
foreach($colors as $color) { ?>
<div class="archive-swatch" style="background-color:<?php echo $color; ?>"></div>
<?php } ?>
<style>
.archive-swatch {
display: inline-block;
width: 20px;
height: 20px;
outline: solid 1px #ccc;
}
</style>
<?php
}
add_action('woocommerce_after_shop_loop_item', 'display_product_colors');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment