Skip to content

Instantly share code, notes, and snippets.

@wickywills
Created January 23, 2019 15:11
Show Gist options
  • Save wickywills/e7f6c9e63d130739e3e79afd6f19dcd2 to your computer and use it in GitHub Desktop.
Save wickywills/e7f6c9e63d130739e3e79afd6f19dcd2 to your computer and use it in GitHub Desktop.
```php
/**
* Custom fields in Woocommerce product
*/
function vads_create_custom_fields() {
global $post;
$pictograms = (array) get_post_meta( $post->ID, '_pictograms', true );
?><p class='form-field _pictograms'>
<label for='_pictograms'><?php _e( 'Pictograms', 'woocommerce' ); ?></label>
<select name='_pictograms[]' class='wc-enhanced-select' multiple='multiple' style='width: 80%;'>
<option <?php selected( in_array( 'picto-1', $pictograms ) ); ?> value='picto-1'>Picto 1</option>
<option <?php selected( in_array( 'picto-2', $pictograms ) ); ?> value='picto-2'>Picto 2</option>
<option <?php selected( in_array( 'picto-3', $pictograms ) ); ?> value='picto-3'>Picto 3</option>
<option <?php selected( in_array( 'picto-4', $pictograms ) ); ?> value='picto-4'>Picto 4</option>
</select>
<img class='help_tip' data-tip="<?php _e( 'Select multiple pictograms', 'woocommerce' ); ?>" src='<?php echo esc_url( WC()->plugin_url() ); ?>/assets/images/help.png' height='16' width='16'>
</p>
<?php
}
add_action( 'woocommerce_product_options_general_product_data', 'vads_create_custom_fields' );
function vads_save_custom_fields() {
global $post;
$woocommerce_pictograms_field = $_POST['_pictograms'];
array_walk($woocommerce_pictograms_field, 'esc_attr');
update_post_meta( $post->ID, '_pictograms', $woocommerce_pictograms_field );
}
add_action('woocommerce_process_product_meta', 'vads_save_custom_fields');
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment