• Resolved NSPT

    (@nsptfr)


    Hello,

    How to automatically attach uploaded images from the form, to the newly created post, please ?

    Regards,

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello @nsptfr,

    Usually, when a user uploads an image through a form, the image is already available in the post.

    Please make sure you have enabled visibility of the post data for image upload field by navigating through WP-admin > WPUF > Post form > Edit post form > edit image upload field > advanced option > show data in post is set to yes.

    Next, navigate through WP-admin > WPUF > Settings > Frontend posting and then make sure you have enabled this option.

    Please confirm that you have checked the above two steps. If the image is still unavailable in the post, please let me know.

    Regards,

    Thread Starter NSPT

    (@nsptfr)

    Hello,

    Thanks for your quick reply,

    The pictures are well uploaded on the server, but not attached to the post/product

    My thing is: user can create Product by filling a form containing all the informations required, also Primary photo upload and a photo gallery upload. Once done, the form is save has draft in woocommerce. The primary photo is well saved and attached to the product, but for the gallery photos, they are not attached to the draft.

    is it possible to do so ?

    Regards,

    Thread Starter NSPT

    (@nsptfr)

    I’m good, I added the function under to woocommerce_update_product.

    The Action is trigger when “$model->save()” is called, so you should definitely not add it to the method, or it will end in an infinite loop. I have no warranty about doing it that way, but it works.

    
    //get attached media by image and product Id, and set gallery with id array.
    add_action( 'woocommerce_update_product', 'woocommerce_update_mannequin_field', 10, 1);
    function woocommerce_update_mannequin_field( $product_id ) {
      $product = new WC_Product_Simple( $product_id );
    
      $photos_ids = [];
      $photos = get_attached_media( 'image', $product_id );
      foreach($photos as $photo){
          $photos_ids[]=$photo->ID;
      }
    $product->set_gallery_image_ids($photos_ids);
    }
    
    • This reply was modified 6 years, 8 months ago by NSPT.
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Attach uploaded Images to freshly created post’ is closed to new replies.