1
votes

I am consuming the WooCommerce REST API (in a mobile app) to retrieve a list of products. My problem is that URLs for the product images are relative.

For example, in the response from the service /wp-json/wc/v1/products, I get images[0].src: "/wp-content/uploads/2013/06/cd_6_angle.jpg" when I was expecting "http://example.com/wp-content/uploads/2013/06/cd_6_angle.jpg".

This appears to happen for all services that return product image URLs.

I am using WordPress version 4.7.4 with WooCommerce version 3.0.4. I don't think this problem occurred with an earlier version I tested a few weeks ago (I don't know which version exactly, but it was up-to-date at the time).

The WooCommerce REST API gives an example (http://woocommerce.github.io/woocommerce-rest-api-docs/#list-all-products) of output showing an absolute URL for the product image.

Is this an undocumented change to the API, or is it a configuration setting somewhere? Or do I perhaps have to customize the code of WooCommerce to get an absolute URL?

2

2 Answers

0
votes

I had this problem with my Wordpress installation on Microsoft Azure. I found that in the attachment/media viewer the URL values were always relative instead of absolute.

I found this issue directing my attention to the definition of WP_CONTENT_URL in wp-config.php. For me, a relative path was defined rather than an absolute path.

Long story short. Replace:
define('WP_CONTENT_URL', '/wp-content');
With:
define('WP_CONTENT_URL', 'http://'. filter_input(INPUT_SERVER, 'HTTP_HOST', FILTER_SANITIZE_STRING) . '/wp-content');
or
define( 'WP_CONTENT_URL', get_option('siteurl') . '/wp-content');

0
votes

since you can call "images[0].src", can't you just say

var baseUrl = "http://www.example.com";
var response = baseurl + images[0].src;

I'd think this would work right?

I'm trying to use the WooCommerce REST API aswell, but for me the images attributes are all null or empty... Looking for a fix still haha.