Skip to content

Instantly share code, notes, and snippets.

@plugin-republic
Created August 20, 2025 10:47
Show Gist options
  • Save plugin-republic/e60e6a8d5367aebdda4520be79ccfe02 to your computer and use it in GitHub Desktop.
Save plugin-republic/e60e6a8d5367aebdda4520be79ccfe02 to your computer and use it in GitHub Desktop.
<?php
/**
* Add custom class parameter to swatch options
*/
function demo_custom_class_param( $option_count, $group_id, $item_key, $item, $key ) {
$name = '_product_extra_groups_' . esc_attr( $group_id ) . '_' . esc_attr( $item_key ) . '[field_options][' . esc_attr( $option_count ) . ']';
$class = isset( $item['field_options'][esc_attr( $key )]['class'] ) ? $item['field_options'][esc_attr( $key )]['class'] : '';
?>
<td class="pewc-option-extra">
<input type="text" class="pewc-field-option-class" name="<?php echo $name; ?>[class]" value="<?php echo esc_attr( $class ); ?>">
</td>
<?php }
add_action( 'pewc_after_option_params', 'demo_custom_class_param', 20, 5 );
function demo_add_custom_class_title( $group_id, $item_key, $item ) {
printf(
'<th class="pewc-option-extra-title"><div class="pewc-label">%s</div></th>',
__( 'Class', 'pewc' )
);
}
add_action( 'pewc_after_option_params_titles', 'demo_add_custom_class_title', 20, 3 );
function demo_swatch_wrapper_custom_class( $wrapper_classes, $item, $key, $option_value, $option_index ) {
if( ! empty( $option_value['class'] ) ) {
$wrapper_classes[] = esc_attr( $option_value['class'] );
}
return $wrapper_classes;
}
add_filter( 'pewc_swatch_wrapper_classes', 'demo_swatch_wrapper_custom_class', 10, 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment