1

I'm using woocommerce as my e-commerce solution and the metabox plugin ( http://www.deluxeblogtips.com/meta-box/ ) to make my life a lot easier when implementing meta boxes. I've never had any issues with it but I now need to add my own custom meta box to the woocommerce product post type....but it never gets added? If I add the meta box in the old fashioned add_meta_box way and the meta box gets added correctly....so I was just wondering if you would have any ideas why using the plugin would cause an issue? Here is the code I'm using to add the meta box to the global meta_boxes array:

global $meta_boxes;

$prefix = "esfproduct_";

// Aggregator metaboxes

$meta_boxes[] = array(
'id'    => 'additionalproductdetails',
'title' => 'Additional Details',
'pages' => array( 'product' ),
'context' => 'normal',
'priority' => 'side',
'fields' => array(
    array(
        'name' => 'Product Features',
        'id'   => "{$prefix}productfeatures",
        'type' => 'text'
    ),
)
);

Thanks for any help you can provide!

1
  • Where are you actually adding the meta box? You're not creating a new instance of the meta box class in the code above, I don't think just adding it to the global $meta_boxes will work.
    – Milo
    Dec 9, 2012 at 17:24

2 Answers 2

4

Here's the metabox code what I'm using which is working fine for me:

// Add meta boxes with TinyMCE via wp_editor() function

// Define the custom box
add_action( 'add_meta_boxes', 'product_details_add' );                                                      
// Do something with the data entered
add_action( 'save_post', 'product_details_save' );
// Adds a box to the main column on the Product post_type edit screens
function product_details_add() {
    add_meta_box( 'product_details', 'Product Details', 'product_details_call', 'product', 'normal', 'high' );
}
// Prints the box content
function product_details_call( $post ) {
    // Use nonce for verification
    wp_nonce_field( plugin_basename( __FILE__ ), 'product_details_noncename' ); 
    $field_value = get_post_meta( $post->ID, 'product_details_meta', false );
    wp_editor( $field_value[0], 'product_details_meta' );
}
// When the post is saved, saves our custom data
function product_details_save( $post_id ) {  
    // verify if this is an auto save routine. 
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
            return;
    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if ( ( isset ( $_POST['product_details_noncename'] ) ) && ( ! wp_verify_nonce( $_POST['product_details_noncename'], plugin_basename( __FILE__ ) ) ) )
            return;
    // Check permissions
    if ( ( isset ( $_POST['post_type'] ) ) && ( 'page' == $_POST['post_type'] )  ) {
        if ( ! current_user_can( 'edit_page', $post_id ) ) {
            return;
        }       
    }
    else {
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
    }
    // OK, we're authenticated: we need to find and save the data
    if ( isset ( $_POST['product_details_meta'] ) ) {
        update_post_meta( $post_id, 'product_details_meta', $_POST['product_details_meta'] );
    }   
}

////////////

However, get_post_meta() is not returning the data in my template pages so I'm using a more "direct" method to get show the meta content:

<?php echo $product->product_custom_fields['product_details_meta'][0];?>

Of course this assumes that you also declare global $product; somewhere above this code in your template file.

0

I think the code you use is out of date. Here is the code that adds meta boxes and custom fields to WooCommerce product which works with the latest version of Meta Box:

add_filter( 'rwmb_meta_boxes', 'your_prefix_meta_boxes' );
function your_prefix_meta_boxes( $meta_boxes ) {
    $prefix = 'esfproduct_';
    $meta_boxes[] = array(
        id' => 'additionalproductdetails',
        'title' => 'Additional Details',
        'context' => 'normal',
        'priority' => 'side',
        'fields'     => array(
            array(
                'name' => 'Product Features',
                'id'   => "{$prefix}productfeatures",
                'type' => 'text'
            ),
        ),
    );
    return $meta_boxes;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.