0

I have a plugin that adds videos in place of the featured image of Woocoemerce products. The plugin just adds the video URL and saves it within the product. I would like to provide the saved URL via Metadata, so that I can edit each product by exporting to a .CSV (Woocommerce export functionality) table.

I was able to find in the plugin the moment the URL was saved in the product. But I don't know how to transform this information as Woocomerce Metadata.

    public function woofv_save_video_box( $post_id ) {
      if ( ! isset( $_POST['woofv_video_box_nonce'] ) ) {
        return $post_id;
      }

      $nonce = $_POST['woofv_video_box_nonce'];


      if ( ! wp_verify_nonce( $nonce, 'woofv_video_box' ) ) {
        return $post_id;
      }


      if ( 'product' == $_POST['post_type'] ) {
        if ( ! current_user_can( 'edit_page', $post_id ) ) {
          return $post_id;
        }
      } else {
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
          return $post_id;
        }
      }

      $woofv_data = array_map('sanitize_text_field', $_POST['woofv_video_embed'] );
      update_post_meta( $post_id, '_woofv_video_embed', $woofv_data );
    }

The plugin saves the video link in the variable $video_url. And that's exactly this variable I'd like to add as Woocomerce metadata. How would it be possible to make this work?

Plugin page if necessary: Wordpress plugin page - Woo featured video

0

Lucas!

Typically when a plugin like this is used to save data to a specific product (images, notes, or in your case, video) that "save" is storing that information in the product's meta for use, so it should already be there for you. Judging by the code snipped provided, this is happening in..:

update_post_meta( $post_id, '_woofv_video_embed', $woofv_data );

With the video link being stored in the variable "$woofv_data" in this specific function, and "_woofv_video_embed" being the meta key you would use to access this value later.

If you would like to see what is going on within your product's meta, there are plugins like JSM's Show Post Meta that will actually display a list of all your product / other post type's meta keys and values below. Having something like this when developing can be pretty handy as it takes out a lot of the guesswork when trying to assess what information is or isn't being stored, and where it's stored at.

  • Thank you so much for spending your time writing an excellent and intuitive answer, Syd! I got confused because it looked like everything was ok with the code. I installed JSM's Show Post Meta and saw through it that the information goes to the _woofv_video_embed attribute. Tried to change it by CSV. and I did it. However, the value did not appear within <textarea> (in the field where links are added), so the video did not play and the problem was not resolved. As I know nothing about PHP, I think the problem is with this piece of code: dontpad.com/code6587 (inside the textarea tag). – Lucas Aug 2 '19 at 22:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.