0
votes

In WordPress 4.7 I am attempting to add/update post meta when an admin creates or makes a change to a WooCommerce product in the dashboard.

I am doing this using a function on the action save_post_post_type as show to be used from WP3.7+ at: https://developer.wordpress.org/reference/hooks/save_post_post-post_type/

Here is my function:

    function my_function() {
        update_post_meta($post_id, '_gravity_form_data', 'a:15:{s:2:"id";s:1:"3";s:13:"display_title";b:0;s:19:"display_description";b:0;s:25:"disable_woocommerce_price";s:2:"no";s:12:"price_before";s:0:"";s:11:"price_after";s:0:"";s:20:"disable_calculations";s:2:"no";s:22:"disable_label_subtotal";s:2:"no";s:21:"disable_label_options";s:2:"no";s:19:"disable_label_total";s:2:"no";s:14:"disable_anchor";s:2:"no";s:14:"label_subtotal";s:8:"Subtotal";s:13:"label_options";s:7:"Options";s:11:"label_total";s:5:"Total";s:8:"use_ajax";s:2:"no";}');
    }
    add_action( 'save_post_product', 'my_function', 10 );

The custom post type (set by WooCommerce) is product

I cannot seem to get this to fire. I did manage to do at some point which fired when creating (not editing) a product but I have since amended the code too many times to retrace.

I have tried with transients, add_post_meta, wp_insert_post, etc.

1

1 Answers

0
votes

I fixed this by using wp_insert_post (works on new/edits) and checking for the post type in a conditional.

A side issue was that I was attempting to add in serialized data and update_post_meta serializes the data.