Friday, February 5, 2016

WP - media_handle_sideload duplicates images on WooCommerce import

If you are using WooCommerce import plugins, you will probably notice that your Media Folder will soon contain lots of duplicate images (or at least duplicate references ).

The reason for this is the fact that this is the default behavior for media_handle_sideload and download_url functions.

The easiest way to prevent this, is to find the code responsible for image processing in the WC Import plugin, and then first delete all the existing images attached to that product / post:

A script like this could help (in case of WooCommerce CSV Import plugin, this goes to save() function in class-woocommerce-product.php):

               $post_id = $this->body['ID'];

$old_att =  get_attached_media("image", $post_id);


foreach($old_att as $key=>$val)
{

wp_delete_attachment($key);

}


Here, $post_id refers to WooCommerce's product id.
This should  called only once, for each product, prior to uploading new images.

No comments:

Post a Comment

Ubuntu 12.04, 14.04, 16.04 - auto start an app or script before login

To run a command or application at startup, even before the user has logged in, you can use this file: /etc/rc.local The commands entered...