WooCommerce API creates images in media even if they exist - woocommerce

What might be a reason?
Trying to batch update product images (and price) in WooCommerce via REST API. Images already exist in wordpress media library and links are correctly copied. However, instead of using image it creates new image.
Request: POST.
URL: https://SiteURL/wp-json/wc/v2/products/xxxx?consumer_key=c______&consumer_secret=_____
Body:
{"update":[
{"regular_price":"448",
"images":[
{"src":"https://urlToImage.jpg",
"position":0, "name": "test"}
],"id":"xxxx"}]}
In response I can see it creates image with the same url but as there is already an image in the library, it adds -1 to url.
Thank you in forward :)

Since this is an update where the images already exists you need to set the id member of the images element. Otherwise, WooCommerce interprets this as a request to add a new image to the product gallery.

Related

Can't get featured image with WordPress REST API

I'm using WordPress REST API and can receive data from https://websitename.com/wp-json/wp/v2/posts?_fields=author,id,excerpt,title,link,featured_media_src_url.
The problem is featured_media_src_url is added with This WordPress Plugin and I can't retrieve it using URL filters. (it works by fetching just .../w/v2/posts without params)
It looks like the plugin gets the path using featured_media which returns an id. Adding this to the fields solves the problem:
https://websitename.com/wp-json/wp/v2/posts?_fields=author,id,excerpt,title,link,featured_media_src_url,featured_media

How to display images of a post as a media gallery in wordpress

Situation
I used user-frontend API to make user capable of uploading image. But this post page posts the image and the image is visible in media library(which contain other previously submitted posts as well).
Problem
I want to display the images associated with a particular post to be displayed as a media gallery.
Methods I tried
user-frontend API had option of selecting post type as user gallery but even after opting for it its not working properly.
I think I can use user-post gallery API that allows me to display the images as a gallery, but I dont know how to integrate both the APIs.
I have some other requirements for which user-frontend API is best choice so cant remove this.
I need some guidance regarding how it can be done maybe through above methods or some meta key or php code.
Kindly ignore the silliness. Its my first wordpress project:)
Direct me if its not possible or if it is possible only without user-frontend API.

Images in Media Library improperly duplicated via Product Image URL field in API

I have a Woo store with an API integration from Salesforce.
Each time a product is saved in Salesforce, the data from all connected fields pushes over to Woo and overwrites the data in Woo.
The Product Image is being populated with a full URL from a Salesforce field, like this:
http://thecrucible.org/wp-content/uploads/crucible-test-image.jpg
Each time the field pushes over to Woo, the image is duplicated in the Media Library, like this:
http://thecrucible.org/wp-content/uploads/crucible-test-image-2.jpg
http://thecrucible.org/wp-content/uploads/crucible-test-image-3.jpg
http://thecrucible.org/wp-content/uploads/crucible-test-image-4.jpg
And so on..
The Product always uses the latest version of the duplicated image.
Is this a core function of Woo, to duplicate an image when its URL is re-populated via an API integration? I don't imagine I would want to try to "turn off" that feature. Or maybe it's actually WP core function?
After speaking to an engineer at WooCommerce.com, they confirmed that if I create/update the image via the API using the "id" field rather than the "src" field, the image duplication problem will go away because WordPress will correctly recognize the image as an existing image in the Media Library.

Wordpress: Get link to the post an images was used in

I'm want to use the Recent Photos Plugin in my sidebar to show the last uploaded photos but instead of linking them to the bigger versions of the images I want to link to the post they were used in.
Can somebody tell me how I get the link of the last post an image was used in?
Thanks, udo
If you do a sidebar based on the posts and display the image you can display the image from a post then have it link to the permalink based on the post. If you do it based on the images like you are doing, there is no identifier that tells wordpress whether or not the image is part of any particular post. So in turn if you wanted, you can set a condition to pull posts and put it in a array and have it cross reference and descend the order by the date of upload per image.

WordPress Media as a Custom Post Type

I am building a WordPress website that will allow members to upload media such as videos, images, etc. They will do this on a public form at for example: domain.com/upload
The form will ask for a Title, a brief summary, show some categories to choose from, and finally a file uploader. The user will then be able to submit the form and it will appear on the site as a post but instead of being just text it is a piece of media.
I have created a custom post type and all seems to be fine, the problem I have is how to deal with the media upload, as the default WP method is to upload an item and then manually insert it into the post (and you can have as many inserted as you like). Where as I want users to be only able to upload one file per post and not have to insert the file into a post because the file itself is the post. They will also be able to upload a featured image.
Any ideas on how I can develop this? Thanks.
You could use WP upload methods to upload your file to the server. Then (depending on your upload's media type) you would insert HTML representation of the file into the post (e.g. parse a text file, create tag for image or embed a video).
If you want to see some working example, please let me know. I just outline the flow:
wp-handle_upload() to physically upload a file
wp_update_post() or wp_insert_post() depending on whether you're creating a new user post or editing existing one. Both methods require one argument - Post object. That's where you put your content (as a post_content field of course) and post data

Resources