Edit an existing WooCommerce meta box in WordPress

I am trying to edit an existing meta box that is being added by a plugin (WooCommerce) in WordPress, but I do not know how to do this.

The meta box is added with this line:

Read More
add_meta_box( 'woocommerce-product-data', __('Product Data', 'woocommerce'), 'woocommerce_product_data_box', 'product', 'normal', 'high' );

…so the function outputting the HTML to screen is woocommerce_product_data_box(). Is there any way to edit this function without loosing it all? I only want to remove parts of it and without editing the original function.

Can I accomplish this with filters somehow? Or any other ideas?

Thanks!

Related posts

Leave a Reply

1 comment

  1. You can find the actual function that writes the meta box for that in the writepanel-product_data.php file. At line 24:

    function woocommerce_product_data_box()
    

    That lists everything in the product data. You can add text boxes or drop downs by using the WooCommerce API. Find the place you’d like to add, or delete, then-

    For a text field –

    woocommerce_wp_text_input( array( 'id' => 'YOURCUSTOMID', 'class' => '', 'label' => __('THELABELOFTHEFIELD', 'woocommerce') ) );
    

    For a drop down –

    woocommerce_wp_select( array( 'id' => 'YOURCUSTOMID', 'label' => __('THELABELOFTHEFIELD', 'woocommerce'), 'options' => array(
                'OPTION1' => __('Option 1', 'woocommerce'),
                'OPTION2' => __('Option 2', 'woocommerce'),
                'OPTION3' => __('Option 3', 'woocommerce'),
                'OPTION4' => __('Option 4', 'woocommerce'),
                'OPTION5' => __('Option 5', 'woocommerce')
                ) ) );
    

    So, you give the input an ID, YOURCUSTOMID, and then label it whatever you want the admin to see when they are filling out the field, THELABELOFTHEFIELD.

    The same with custom meta boxes in WordPress posts/pages, you still have to save it, though. Find the function woocommerce_process_product_meta() somewhere around line 600. The comments above will tell you it is saving the meta box data. Then, insert a line of code to save whatever custom ID you just gathered –

     update_post_meta( $post_id, 'YOURCUSTOMID', stripslashes( $_POST['YOURCUSTOMID'] ) );
    

    and just change it for you custom ID. Just make sure you put that line of code anywhere after the globals are declared.

    Naturally, for removing fields, you can just comment out the fields that you don’t want gathered in both of those functions.

    Hope that helps.