set_post_thumbnail or media_sideload_image adds image multiple times in media library

<?php $post_id = get_queried_object_id(); $bla = “myImageURL”; // Set the image URL $image_url = $bla; // Check if the image already exists in the media library $existing_image = get_posts(array( ‘post_type’ => ‘attachment’, ‘meta_key’ => ‘_wp_attached_file’, ‘meta_value’ => sanitize_file_name(basename($image_url)) )); if (!$existing_image) { // Image does not exist in the media library, so download it $image_id … Read more

How to update all products with same SKU prefixes when updating stock with CSV?

This how I finally made it to update duplicated product variations with ‘-2’ SKU suffix: function update_imported_product($object, $data) { if ($object->get_type() === ‘variation’){ $product_id = wp_get_post_parent_id($object->get_id()); } else { $product_id = $object->get_id(); } if(isset($product_id)) { $sku_in = get_post_meta($product_id, ‘_sku’, true); // Search and update variations with suffix -2 $args = array( ‘post_type’ => ‘product_variation’, ‘meta_query’ … Read more