-1

I am new to WooCommerce development and REST API provided by WooCommerce. I am facing an issue to upload an image for adding new product as a vendor from my Mobile app which is in React Native.

When I add product from website, all images are being uploaded on wp_content/uploads folder of server and then uploaded image URL is being used to add product API something like this:

"images": [
           {
             "src": "URL of uploaded image"
           },
           {
             "src": "URL of uploaded image"
           }
         ]

Can anyone let me know how can I upload images from my Mobile App to uploads folder and return URL of image? Any help will be appreciated.

Thanks.

EDIT 1

What I achieved till now is upload image from URL like this:

function addProductImage($request) {
        $image_url = "https://image.shutterstock.com/image-photo/impressive-summer-view-lovatnet-lake-260nw-692930053.jpg";
        //$image_url = $request["image"];
        $filename = "myDummyImage.jpg";
        $upload_dir = wp_upload_dir();
        $image_data = file_get_contents($image_url);
        $filename = basename($image_url);
        if (wp_mkdir_p($upload_dir['path'])) {
            $file = $upload_dir['path'] . '/' . $filename;
        } else {
            $file = $upload_dir['basedir'] . '/' . $filename;
        }

        file_put_contents($file, $image_data);

        $wp_filetype = wp_check_filetype($filename, null);
        $attachment = array(
            'post_mime_type' => $wp_filetype['type'],
            'post_title' => sanitize_file_name($filename),
            'post_content' => '',
            'post_status' => 'inherit',
        );
        $attach_id = wp_insert_attachment($attachment, $file, $post_id);
        require_once ABSPATH . 'wp-admin/includes/image.php';
        $attach_data = wp_generate_attachment_metadata($attach_id, $file);
        $res1 = wp_update_attachment_metadata($attach_id, $attach_data);
        $attachment_url = wp_get_attachment_url($attach_id);
        return $attachment_url;
    }

This works fine if I upload image from URL, but I want to upload image from my uri of the image from mobile app. I don't have URL of the image, I have image chosen from mobile gallery or taken from camera. How can I achieve this? Please let me know where I am going wrong.

Your Answer

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

Browse other questions tagged or ask your own question.