我开发了一个插件,可以以非常特定的方式搜索WooCommerce产品的副本。它在我们的测试环境中工作得很好,但在客户端的原始设置中它不会执行。它只是重新加载页面,就像什么都没发生一样。我们的设置与原始网站之间最明显的区别是产品的数量。我们的客户在他的WooCommerce商店里有60000多种产品,数量之多令人难以置信。
我认为我们运行的查询可能在某种程度上受到限制,但可能我完全走错了路。非常感谢您的帮助,因为我快要发疯了。
以下是相关代码:
$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);
}