1

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); 
 }
6
  • Can you indent your code? It's difficult to read
    – Tom J Nowell
    Commented Jul 7, 2015 at 14:06
  • 2
    Also what errors appear in the error log? Is this javascript powered via AJAX calls or is the page meant to reload but with a different result?
    – Tom J Nowell
    Commented Jul 7, 2015 at 14:08
  • Thank you for your reply. It is actually only a part of the full code. It runs based on a get variable that is appended to the URL after clicking on a submit button. Commented Jul 7, 2015 at 15:12
  • EDIT: This is the weird part, there is no error at all. Other errors were shown before, but fixed all of them already. The page just loads and nothing happened. The only thing that could be an indicator is that "Thank you for creating with WordPress" in the bottom of the admin page is missing, when I run the function. Commented Jul 7, 2015 at 15:18
  • Do you mean errors in your error log, or errors displayed in the browser?
    – Tom J Nowell
    Commented Jul 7, 2015 at 16:29

2 Answers 2

1

I think the query we run is maybe limited in some way...

So far as I know, your query and the processing you do of the results is going to be limited by server resources-- memory, etc-- not by WordPress other than the WordPress defined PHP memory limit, to some extent. You can try altering that and see if it helps. Otherwise, you will need help from your host and you will need to post error logs and other debugging information.

5
  • The error log is empty unfortunately. This makes this issue so hard to debug. I increased the memory limit to 4096M and verified that it really is 4Gbyte on the WooCommerce System Status page. I also calculated with a few formulas the php size of the array I create and both arrays should not be larger than 100Mbyte (absolute max). Commented Jul 7, 2015 at 15:45
  • What about the MySQL server? What are its settings? Is it crashing?
    – s_ha_dum
    Commented Jul 7, 2015 at 15:58
  • That is another frustrating part of this project. We are not allowed to access the the backend of our client's server. We already pointed out that the setting is far too low for the current database size. They are using one of the small (baby gator) shared hosting package on HostGator. To kind of figure out if the MySQL server crashes, I immediately reloaded another tab of the website, when the error happens and it opened almost instantly (not cached in browser). This makes me believe that the SQL server keeps running, but I might be wrong. Commented Jul 7, 2015 at 16:17
  • I am not sure how to debug without getting that information. You need to know where the failure is occurring. My bet is still the MySQL server.
    – s_ha_dum
    Commented Jul 7, 2015 at 16:29
  • I agree with you. I think I was hoping that someone ran into a similar issue and can directly point me to a solution. It seems like I need to insist to get access to the server backend. Harry Om Gupta seemed to have a similar issue, so I will try to recode the script to run with $wpdb->get_results first and if that is not working I will try it with a classic mysql query. That should be the least resource intensive solution. I will write an update, once I know more. Thank you! Commented Jul 7, 2015 at 16:40
0

Yeah I think it works with limited no of items, I have also find similar issue with wp_query, on my client's site, there are 20,000+ products were there, btw I did resolve this by executing sql query,instead of wp_query. ;)

1
  • Thank you. I will try to rewrite the code and check if it works. Commented Jul 7, 2015 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.