I develop a plugin that searches for a duplicates of WooCommerce products in a very specific way. It works great in our test environment, but on the client's original setup it does not execute. It just reloads the page as if nothing happened. The most obvious difference between our setup and the original website is the amount of products. Our client has the ridiculous amount of 60,000+ products in his WooCommerce store.
I think the query we run is maybe limited in some way, but maybe I am completely on a wrong path. ANY help is appreciated, because I am about to get crazy.
Here is the code in question:
$type = 'product';
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1
);
function searchForId($id, $array) {
foreach ($array as $key => $val) {
if ($val['name'] === $id) {
return $key;
}
}
return null;
}
$my_query = null;
$my_query = new WP_Query($args);
$items = array();
$duplicates = array();
$name;
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$name = the_title( '', '', FALSE );
$thumb_url = get_post_thumbnail_id();
$product_description = get_the_content();
if (strpos($thumb_url,'default.png') !== false) {
$thumb_url = null;
}
$price = get_post_meta( get_the_ID(), '_price', true);
$sale = get_post_meta( get_the_ID(), '_sale_price', true);
if ($sale < $price && !empty($sale)) {
$price = $sale;
}
if (array_key_exists($name, $items)) {
$existing_product_description = $items[$name]['product_description'];
$existing_name = $items[$name]['name'];
$existing_price = $items[$name]['price'];
$existing_image = $items[$name]['featured_image'];
if ($existing_product_description != $product_description && $existing_price == $price && $existing_name == $name) {
if ($existing_image == null) {
$items[$name]['featured_image'] = $thumb_url;
}
$duplicate_id = get_the_id();
array_push($duplicates, $duplicate_id);
}
if ($existing_product_description != $product_description && $existing_price != $price && $existing_name == $name) {
if ($existing_image == null) {
$items[$name]['featured_image'] = $thumb_url;
}
}
if ($existing_product_description == $product_description && $existing_price != $price && $existing_name == $name) {
if ($existing_price > $price) {
$items[$name]['price'] = $price;
}
if ($existing_image == null) {
$items[$name]['featured_image'] = $thumb_url;
}
$duplicate_id = get_the_id();
array_push($duplicates, $duplicate_id);
}
if ($existing_product_description == $product_description && $existing_price == $price && $existing_name == $name) {
$duplicate_id = get_the_id();
array_push($duplicates, $duplicate_id);
}
}
else {
$items[$name] = array(
'id' => get_the_ID(),
'name' => $name,
'price' => $price,
'featured_image' => $thumb_url,
'product_description' => $product_description
);
}
?>
<?php
endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
foreach ($items as $product => $products) {
$product_id = $items[$product]['id'];
$product_new_price = $items[$product]['price'];
$product_new_thumb = $items[$product]['featured_image'];
$product_to_edit = new WC_Product($product_id);
$price = $product_to_edit->price;
set_post_thumbnail( $product_id, $product_new_thumb );
update_post_meta($product_id, '_price', $product_new_price);
update_post_meta($product_id, '_regular_price', $product_new_price);
update_post_meta( $product_id, '_sale_price', '' );
}
foreach ($duplicates as $index => $ids) {
wp_trash_post($ids);
}