• Resolved MTWK-IDG

    (@mtwk-idg)


    We are using woocommerce and gravity forms. A gravity form id is being assigned (as well as other info)to each product. I’m trying to allow us to use Quick Edit to update the gravity form id. I’ve populated the Quick Edit but the code below doesn’t actually update the field.

    add_action( 'save_post','rachel_carden_save_post', 10, 2 );
    function rachel_carden_save_post( $post_id, $post ) {
    
       // don't save for autosave
       if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
          return $post_id;
    
       // dont save for revisions
       if ( isset( $post->post_type ) && $post->post_type == 'product' )
          return $post_id;
    
       switch( $post->post_type ) {
    
          case 'product':
    
             // release date
    	 // Because this action is run in several places, checking for the array key keeps WordPress from editing
             // data that wasn't in the form, i.e. if you had this post meta on your "Quick Edit" but didn't have it
             // on the "Edit Post" screen.
    	 if ( array_key_exists( 'ai_gravityforms', $_POST ) ) {
    
    $gravity_form_meta = get_post_meta( $post_id,'_gravity_form_data');
    // need to update the entire gravity form array for the post - can't grab just the id and change.
    
    $get_gravity_form_meta[0]['id'] = $_POST[ 'ai_gravityforms' ] ;
    // replace original data in the individual field with new data from the quick edit form
    update_post_meta( $post_id, '_gravity_form_data',$get_gravity_form_meta  ); 
    
    		}
    	 break;
    
       }
    
    }
Viewing 8 replies - 1 through 8 (of 8 total)
  • Maybe your question would best be asked at the plugin’s support page:
    https://www.gravityhelp.com/support/

    Thread Starter MTWK-IDG

    (@mtwk-idg)

    THey say it’s beyond their support.

    Thread Starter MTWK-IDG

    (@mtwk-idg)

    Okay. Still wrestling with this code. I’ve tested this code with serialized and non-serialized post meta.

    This code works perfectly fine with non-serialized meta. I go into Quick Edit, update the field and voila – the field is updated and displays correctly in the Admin List. So far so good.

    // BEGIN SAVE THE QUICK EDIT DATA   +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    add_action( 'save_post','rachel_carden_save_post', 10, 2 );
    function rachel_carden_save_post( $post_id, $post ) {
    
       // don't save for autosave
       if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
          return $post_id;
    
       // dont save for revisions
       if ( isset( $post->post_type ) && $post->post_type == 'revision' )
          return $post_id;
    
       switch( $post->post_type ) {
    
          case 'pattern':
    
             // release date
    	 // Because this action is run in several places, checking for the array key keeps WordPress from editing
             // data that wasn't in the form, i.e. if you had this post meta on your "Quick Edit" but didn't have it
             // on the "Edit Post" screen.
    	 if ( array_key_exists( 'pattern_company', $_POST ) )
    	    update_post_meta( $post_id, 'wpcf-own-pattern', $_POST[ 'pattern_company' ] );
    
    	 break;
    
       }
    
    }
    // END SAVE THE QUICK EDIT DATA   +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    '
    
    Now I take that same EXACT code but attempt to use it with serialized data. I go into quick edit, update the item and voila - it now shows nothing in the targeted information - in this case, the id of the gravity form is now blank.
    '// BEGIN SAVE THE QUICK EDIT DATA   +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    add_action( 'save_post','rachel_carden_save_post', 10, 2 );
    function rachel_carden_save_post( $post_id, $post ) {
    
       // don't save for autosave
       if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
          return $post_id;
    
       // dont save for revisions
       if ( isset( $post->post_type ) && $post->post_type == 'revision' )
          return $post_id;
    
       switch( $post->post_type ) {
    
          case 'product':
    
             // release date
    	 // Because this action is run in several places, checking for the array key keeps WordPress from editing
             // data that wasn't in the form, i.e. if you had this post meta on your "Quick Edit" but didn't have it
             // on the "Edit Post" screen.
    	 if ( array_key_exists( 'ai_gravityforms', $_POST ) )
    
    	 	$gravity_form_data = get_post_meta( $post_id,'_gravity_form_data');// grab the current serialized array in the database
    	 	$gravity_form_data[0]['id'] = '10'; // replace the id in the serialized array with some test data
    	 	 //$gravity_form_data[0]['id'] = '$_POST[ 'ai_gravityforms' ]; this is what I really want to grab
    		update_post_meta( $post_id, '_gravity_form_data', $gravity_form_data );
    	 break;
    
       }
    
    }
    // END SAVE THE QUICK EDIT DATA   +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    When I test the following in the footer of page:
    $gravity_form_data = get_post_meta( get_the_ID(),’_gravity_form_data’);
    I can do a var_dump and see all the post meta for the particular product page that I’m on.
    I can also replace the id by
    $gravity_form_data[0][‘id’] = ’10’;
    This all works but not inside the above function.

    So the bottom line: How do I update serialized data when being processed via Quick Edit.

    Moderator bcworkz

    (@bcworkz)

    One issue could be you are checking the existence of ‘ai_gravityforms’ key in $_POST before retrieving the key ‘_gravity_form_data’. Perhaps the way your form works this is OK, but it appears to be wrong.

    The real issue is how you are addressing your updated array. If you were to look at the meta data in phpMyAdmin, you’ll see the data is all there, neatly stacked in array levels. While the following example may seem like the same thing, it’s not. Something about how arrays are serialized, I’m not entirely sure how to explain it.

    Instead of $gravity_form_data[0]['id'] = '10';
    do this:

    $gravity_form_data = $gravity_form_data[0];
    $gravity_form_data['id'] = '10';

    This keeps the serialized array from growing new levels each time it’s updated.

    Thread Starter MTWK-IDG

    (@mtwk-idg)

    Thanks for the response. I will try that.

    Thread Starter MTWK-IDG

    (@mtwk-idg)

    Thank you. Thank you. Thank you. I didn’t have to pull the if statement. The change in the array made all the difference. You made my month! I’ve been banging my head for weeks on this.

    Thread Starter MTWK-IDG

    (@mtwk-idg)

    updating status of post

    Moderator bcworkz

    (@bcworkz)

    ?? You’re most welcome! That was a sneaky little bug. Your logic was correct, it just didn’t work logically ??

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Update a Gravity Form id with Quick Edit’ is closed to new replies.