Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

programmatically add product #28

Open
ArmandHeijder opened this issue Nov 13, 2019 · 7 comments
Open

programmatically add product #28

ArmandHeijder opened this issue Nov 13, 2019 · 7 comments
Assignees

Comments

@ArmandHeijder
Copy link

Hello there,

I have a script that programmatically adds products from a xml, now i would like to incorporate your plugin to add the external images and i use the following script

function Generate_Image( $post_id, $image_url, $flag ){ if( $flag == 0){ update_post_meta($post_id,'_knawatfibu_url',$image_url); } if( $flag == 1 ){ # Add gallery image to product $attach_id_array = get_post_meta($post_id,'_knawatfibu_wcgallary', true); $attach_id_array .= ','.$image_url; update_post_meta($post_id,'_knawatfibu_wcgallary',$attach_id_array); } }

But it does not seem to add the image to the product meta,

Could you please help me?

Thanks!

@ArmandHeijder
Copy link
Author

Hello! I see that you assigned someone to take up this 'issue', is there an update?

@Super-Saiyan-Mohamed
Copy link
Contributor

There will be an update soon but right now we are working in the Woocommerce plugin.

@ArmandHeijder
Copy link
Author

Hello, thank you for your response, what can i expect with soon? I got a webshop that needs to import 30000 products with multiple images, and needs to go live in the end of the week, and you can imagine the server size if i need to import the images to the server.. If you could please look into it for me? I could even help translate the plugin to Dutch or something in return

@smoovo
Copy link

smoovo commented Nov 27, 2019

Hi,

After searching online for this solution, i got here thanks to you. Your function is simple and clean, but you are trying to use array as string, it is impossible and not right. We need to supply URL and SIZES to the database. I changed it and now it is working perfectly.

function Generate_Image( $post_id, $image ) {
	
	if( array_key_exists('img_url', $image) ) { // Featured image

		update_post_meta( $post_id, '_knawatfibu_url', $image );

	} else { // Gallery image

		$attach_id_array = get_post_meta( $post_id, '_knawatfibu_wcgallary', true );
		$attach_id_array[] = $image;
		update_post_meta( $post_id, '_knawatfibu_wcgallary', $attach_id_array );

	} 
	
}

// Example for image data array to insert/update featured image
$image = array( 'img_url'=>'https://avatars.mds.yandex.net/get-pdb/49816/6e931887-5667-4080-9fc2-125f5d7e2b5c/s1200', 'width'=>693, 'height'=>960 ); 

// Example for image data array to insert/update gallery
$image = array( 'url'=>'https://avatars.mds.yandex.net/get-pdb/49816/6e931887-5667-4080-9fc2-125f5d7e2b5c/s1200', 'width'=>693, 'height'=>960 );

Thanks.

@ArmandHeijder
Copy link
Author

Hi smoove,

Thankyou for your help! I got the Featured image working, but for some reason I cant seem to get the Gallery image working..

I get the images from an API and I try to loop through them with the following code:
(I only want to show the first two images that I get back from the API)

$img_nr = 0; foreach ($product->images->image as $image) { if($img_nr == 0){ $the_image = array( 'img_url'=>"$image", 'width'=>675, 'height'=>900 ); Generate_Image($product_id, $the_image); $img_nr++; }else if ($img_nr == 1){ $the_image = array( 'url'=>"$image", 'width'=>675, 'height'=>900 ); Generate_Image($product_id, $the_image); $img_nr++; } }
Do you see what the problem is?

The function (Generate_Image) is the same as in your response!

Thanks a lot!

@smoovo
Copy link

smoovo commented Dec 1, 2019

Sure, no problem.

It will be easier to get a sample of export of that API. Copy paste here just few products as an array, how they showing at your end.

I guess in your case it will be a screenshot of the export of these lines.

echo "<pre>";
print_r( $product );
echo "</pre>";

Hope i can help.

@ignition-studio
Copy link

ignition-studio commented Jul 14, 2020

I have a question... Does this filter code apply to using a column in my Woocommerce product import CSV file? Instead of default "Image" that Woocommerce uses, I would title my columns "_knawatfibu_url" and "_knawatfibu_wcgallary" so that when I do my product import it does not also import the media files to the media library?

function Generate_Image( $post_id, $image ) {
   
   if( array_key_exists('img_url', $image) ) { // Featured image

   	update_post_meta( $post_id, '_knawatfibu_url', $image );

   } else { // Gallery image

   	$attach_id_array = get_post_meta( $post_id, '_knawatfibu_wcgallary', true );
   	$attach_id_array[] = $image;
   	update_post_meta( $post_id, '_knawatfibu_wcgallary', $attach_id_array );

   } 
   
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants