WooCommerce-REST-API-Client-Library icon indicating copy to clipboard operation
WooCommerce-REST-API-Client-Library copied to clipboard

Creating products not working after WooCommerce update.

Open ghost opened this issue 10 years ago • 24 comments

I have been using the package for a few weeks now but since upgrading my WooCommerce version to 2.4.7 I have been having issues.

I use the feed to create products from several feeds but since the update I am getting an error when trying to upload images: woocommerce_api_invalid_remote_product_image

The code has not changed and has worked for several weeks before the upgrade, images were automatically synced with the media section.

Below is the structure of the data I use which is passed into the following line: print_r($client->products->create($data));

$data = Array ( [product] => Array ( [title] => Title1 [type] => external [sku] => 369100007 [product_url] => valiurl [regular_price] => 999.99 [sale_price] => [description] => Description [categories] => Array ( [0] => Main Cat [1] => Sub Cat ) [images] => Array ( [src] => validimageurl [position] => 0 ) [tags] => Array ( [0] => Tag1) [attributes] => Array ( [0] => Array ( [name] => Color [slug] => color [position] => 0 [visible] => 1 [options] => ) ) ) )

I have changed some of the data above to keep it short. The images don't seem to be uploading (which is throwing out the error) and there is also an issue with Categories and Tags not being set. Once again to reiterate I have not changed any code since the update.

ghost avatar Oct 01 '15 20:10 ghost

Just tried this on a complete fresh install of Wordpress / WooCommerce - having the same issue. Categories, images and tags aren't working. Same issue as above.

ghost avatar Oct 01 '15 20:10 ghost

I am having the exact same issue. all other properties are being created but not categories and tags. did you manage to come up with something?

harunthuo avatar Oct 07 '15 09:10 harunthuo

Yeah, posted on Stackoverflow and nothing - I have resorted to directly creating the posts / uploading images using the following modified code:

Create post using: http://wordpress.stackexchange.com/questions/137501/how-to-add-product-in-woocommerce-with-php-code

Upload images using: http://wordpress.stackexchange.com/questions/12379/programmatically-get-images-by-url-and-save-in-uploads-folder

I'm just using this as a temporary workaround until the package has been fixed but this works fine for me.

ghost avatar Oct 07 '15 09:10 ghost

thanks for getting back.

i'm picking my products plus their attributes from my database and populating them into my woo store using the api(was working perfectly fine before, till i updated my woo plugin to the latest, 2.4.7). i was achieving this using a simple php script

i'm not a guru in wp coding. would you mind explaining how your code works?

harunthuo avatar Oct 07 '15 09:10 harunthuo

following your example on how to create a post, i have been able to add a product. only issue remaining is how to create the product image

this does not seem to do it:

update_post_meta( $post_id, '_images', "image_url" );

any thoughts on this?

harunthuo avatar Oct 07 '15 18:10 harunthuo

Sorry Harun I have been away, just got back now.

Great news that you could create the post. Uploading the image is very straight forward.

You can use the following WP command: media_sideload_image( $url, $post_id, $description );

$url = the url of the image $post_id = The post ID you want to attatch the image to $description = image description.

Let me know if this works.

ghost avatar Oct 07 '15 18:10 ghost

i get an error:

fatal error: Call to undefined function media_sideload_image()

i should mention i'm creating the products in a plugin i have created

harunthuo avatar Oct 07 '15 18:10 harunthuo

Ah, my apologies - you'll have to import a few PHP files for the function to work.

require_once(ABSPATH . 'wp-admin' . '/includes/image.php');
require_once(ABSPATH . 'wp-admin' . '/includes/file.php');
require_once(ABSPATH . 'wp-admin' . '/includes/media.php');

Give that a try - you may need to tweak the paths as I don't know your setup.

ghost avatar Oct 07 '15 18:10 ghost

no longer throws an error but image is not created

i have however stumbled upon this piece of code online that works fine:

    $image_url  = ''; // Define the image URL here
$upload_dir = wp_upload_dir(); // Set upload folder
$image_data = file_get_contents($image_url); // Get image data
$filename   = basename($image_url); // Create image file name

// Check folder permission and define file location
if( wp_mkdir_p( $upload_dir['path'] ) ) {
    $file = $upload_dir['path'] . '/' . $filename;
} else {
    $file = $upload_dir['basedir'] . '/' . $filename;
}

// Create the image  file on the server
file_put_contents( $file, $image_data );

// Check image file type
$wp_filetype = wp_check_filetype( $filename, null );

// Set attachment data
$attachment = array(
    'post_mime_type' => $wp_filetype['type'],
    'post_title'     => sanitize_file_name( $filename ),
    'post_content'   => '',
    'post_status'    => 'inherit'
);

// Create the attachment
$attach_id = wp_insert_attachment( $attachment, $file, $post_id );

// Include image.php
require_once(ABSPATH . 'wp-admin/includes/image.php');

// Define attachment metadata
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );

// Assign metadata to attachment
wp_update_attachment_metadata( $attach_id, $attach_data );

// And finally assign featured image to post
set_post_thumbnail( $post_id, $attach_id );

harunthuo avatar Oct 07 '15 18:10 harunthuo

Hmm that's strange works fine for me. Anyway it's working now :) - hopefully the API will roll out the update to include tags / categories soon.

ghost avatar Oct 07 '15 18:10 ghost

Had a chat earlier with the woocommerce guys. check it out here:

https://github.com/woothemes/woocommerce-rest-api-docs/issues/20#issuecomment-146188582

harunthuo avatar Oct 07 '15 18:10 harunthuo

One more thing: how do we set the description and short description for a product?

tried this but it isn't working:

update_post_meta( $post_id, '_description', "test description" ); update_post_meta( $post_id, '_short_description', "test short description" );

harunthuo avatar Oct 07 '15 19:10 harunthuo

update_post_meta( $post_id, 'post_content', "test description" ); update_post_meta( $post_id, 'post_excerpt', "test short description" );

If I remember correctly.

ghost avatar Oct 07 '15 19:10 ghost

still doesn't work. the above creates properties for 'Custom Fields' in woocommerce but not description and short description

harunthuo avatar Oct 07 '15 19:10 harunthuo

Try this, when you are inserting the post:

$post_information = array( 'post_title' => 'new item shop', 'post_content' => 'Description', 'post_excerpt' => 'Short product description', 'post_type' => 'post', 'post_status' => 'publish' );

ghost avatar Oct 07 '15 19:10 ghost

all is well now..everything works absolutely fine.

really grateful for your invaluable help

cheers

harunthuo avatar Oct 07 '15 19:10 harunthuo

My pleasure, this should be helpful for others anyway! Glad I could help.

ghost avatar Oct 07 '15 19:10 ghost

Have you noted that you can create the SAME product over and over. this seems a major drawback. this is why the api is the way to go since it checks if an sku exists and throws an error so the same product will not be duplicated

could you think of a way we can achieve the same here?

harunthuo avatar Oct 07 '15 19:10 harunthuo

if ( FALSE === get_post_status( $id ) ) { // The post does not exist } else { // The post exists }

ghost avatar Oct 07 '15 19:10 ghost

get_post_status( $id ) returns the status of a page e.g 'publish', 'draft'

when will it ever be set to 'false'?

harunthuo avatar Oct 07 '15 20:10 harunthuo

It will return false if the post doesn't exist. i.e. you check for post 1111 and the post with the ID 1111 does not exist either in draft or publish.

If you want to check for draft or public just add another check.

ghost avatar Oct 07 '15 21:10 ghost

Hey buddy

from your method, is there a way of retrieving a product attribute.. say, i would like to view the existing sku's?

harunthuo avatar Oct 08 '15 13:10 harunthuo

get_post_meta( $prodict->id , '_product_attributes' );

On Thu, Oct 8, 2015 at 2:08 PM, harun thuo [email protected] wrote:

Hey buddy

from your method, is there a way of retrieving a product attribute.. say, i would like to view the existing sku's?

— Reply to this email directly or view it on GitHub https://github.com/kloon/WooCommerce-REST-API-Client-Library/issues/129#issuecomment-146538312 .

ghost avatar Oct 08 '15 13:10 ghost

Hey there. hope you're well

i'm creating product attributes as shown below:

$avail_attributes = array( 'green' ); wp_set_object_terms($post_id, $avail_attributes, 'pa_color');

$thedata = Array('pa_color'=>Array( 'name'=>'pa_color', 'value'=>'', 'is_visible' => '1', 'is_variation' => '1', 'is_taxonomy' => '1' ));

update_post_meta( $post_id,'_product_attributes',$thedata);

works perfect for the attributes 'color' and 'size'. in addition to this, i want to have an attribute for my product brands but when i try, the attribute 'brand' is not captured in the list of attributes. is creating attributes restricted to certain values only (like size and color)?

harunthuo avatar Nov 11 '15 12:11 harunthuo