Skip to content

Instantly share code, notes, and snippets.

@juniorthiesen
Created August 15, 2017 19:19
Show Gist options
  • Save juniorthiesen/a62b05f99ba15118763e876ba297ea14 to your computer and use it in GitHub Desktop.
Save juniorthiesen/a62b05f99ba15118763e876ba297ea14 to your computer and use it in GitHub Desktop.
ADICIONA CAMPO DE NCM AO PRODUTO
// ADICIONA CAMPO DE NCM AO PRODUTO
add_action('add_meta_boxes', 'add_product_metaboxes');
// Add the Beast Selling Meta Boxes
function add_product_metaboxes()
{
add_meta_box('wc_ncm', 'NCM', 'wc_ncm', 'product', 'side', 'high');
}
function wc_ncm()
{
global $post;
echo '<input type="hidden" name="ncmproduct_noncename" id="ncmproduct_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
$_ncm = get_post_meta($post->ID, '_ncm', true);
echo '<input type="text" name="_ncm" id="_ncm" value="' . $_ncm . '" >';
}
add_action('save_post', 'save_product_ncm');
function save_product_ncm() {
global $post;
$post_id=get_the_ID();
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['ncmproduct_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
if (get_post_type($post_id) == 'product') {
$ncm_meta['_ncm'] = $_POST['_ncm'];
// Add values of $events_meta as custom fields
foreach ($ncm_meta as $key => $value) { // Cycle through the $events_meta array!
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}
}
}
// ADICIONA CAMPO DE NCM AO PRODUTO (FIM)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment