• I am creating a product feed from our legacy, in-house product database into WooCommerce. Eventually we will make WooCommerce the master database when everything is working. The basics are already working with variations and local attributes. Next I need to add a bunch of custom fields. (Are ‘custom fields’ the same thing as meta_data fields? ) To test this, I successfully added a meta_data field ‘brand’ to a product and now when I cURL get this product, it’s amongst the metafields viz:

    'meta_data' => [
                               {
                                 'key' => '_alg_ean',
                                 'value' => '8023517000273',
                                 'id' => 26536
                               },
                               {
                                 'id' => 27529,
                                 'key' => 'brand',
                                 'value' => 'Italfama'
                               }
                             ],
    
    

    When I go into WooCommerce Admin for this product, the field does not appear so we can’t change it. We had a similar thing with EAN but for that we used an add-on that added the ‘_alg_ean’ meta_data field that can also be seen. But that field is now editable in the Admin interface under the ‘Inventory’ section. Which is slightly odd as I read somewhere that a field started with _ would not be editable.

    How do we make a meta_data field editable?

    many thanks for any advice.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Correct, in WordPress “custom field” is a type of meta data. You can probably alter your field through the custom fields meta box, which might initially be hidden. It can be made visible through screen preferences. Some post types might intentionally not support the custom fields meta box.

    Meta key names beginning with an underscore are intended to be read-only. It is possible to alter these values with code, but they should not be editable through the WP UI.

    WooCommerce has a more complex meta data schema that involves custom WC specific tables. It’s likely why your added meta data is not appearing. To properly save data that’s compatible with WC, either examine in the DB how it saves similar data (table wp_wc_product_attributes_lookup for example); or look through its documentation; or ask in its dedicated support forum.

    Thread Starter mtgwoo

    (@mtgwoo)

    I was not aware of the ‘Custom Fields’ checkbox in the Screen Preferences. This simple change made the field view/editable, thank you.

    Next we need to display this on the relevant product page. If I have a field number_of_players, how would we display “Number of players: 3” on the product page? (if this is not the appropriate forum, please let me know which is the best forum to repost in….)

    Moderator bcworkz

    (@bcworkz)

    If you have a question about writing custom code in core WP, you’re in the right place 🙂 But if your question pertains to a theme or plugin, they have their own dedicated forums that are more appropriate. The goal is to direct you to where there’s the best chance of getting your question answered. Most people answering in this forum are not that knowledgeable about specific themes or plugins.

    To add custom content to a WooCommerce product page, there is likely an action hook you can use. The correct hook depends on where you want the custom content to appear. If you study the source code for the various product templates that WooCommerce uses, you might be able to find the right action hook. If you have trouble with that approach, the resources I linked to in my previous reply are your best options.

    As a guess, maybe ‘woocommerce_product_meta_end’ would be an appropriate action hook to use?

    add_action('woocommerce_product_meta_end', function() {
       $num = get_post_meta( get_the_ID(), 'number_of_players', true );
       echo "<br>Number of players: $num<br>";
    }

    Untested, but something to start with.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Making a metafield editable in the admin interface’ is closed to new replies.