Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 删除Woocommerce中重复的导入订单_Php_Mysql_Sql_Wordpress_Orders - Fatal编程技术网

Php 删除Woocommerce中重复的导入订单

Php 删除Woocommerce中重复的导入订单,php,mysql,sql,wordpress,orders,Php,Mysql,Sql,Wordpress,Orders,我在Woocommerce中有重复的订单,我想删除它们,以保持唯一性,以便进行干净的记账 我不擅长SQL,我写了这个请求,但当存在重复时,它会列出两个重复 SELECT * FROM `wp_posts` WHERE post_type = 'shop_order' AND post_status = 'wc-completed' GROUP BY post_date HAVING count(*) > 1 ORDER BY `wp_posts`.`post_date` DESC

我在Woocommerce中有重复的订单,我想删除它们,以保持唯一性,以便进行干净的记账

我不擅长SQL,我写了这个请求,但当存在重复时,它会列出两个重复

SELECT *
FROM `wp_posts` 
WHERE post_type = 'shop_order' AND post_status = 'wc-completed' 
GROUP BY post_date 
HAVING count(*) > 1 
ORDER BY `wp_posts`.`post_date` DESC
这给了我307个结果

我如何编写正确的请求来删除重复项并只保留唯一的订单

样本数据:

ID  post_author post_date       post_date_gmt   post_content    post_title  post_excerpt    post_status comment_status  ping_status post_password   post_name   to_ping pinged  post_modified   post_modified_gmt   post_content_filtered   post_parent guid    menu_order  post_type   post_mime_type  comment_count   
22282   227 2018-02-04 01:00:00 2018-02-04 00:00:00     Order – February 4, 2018 @ 01:00 AM       wc-completed    open    closed      order-4-02-18-6         2018-03-19 17:12:32 2018-03-19 16:12:32     0   https://www.hemen-biarritz.com/?post_type=shop_ord...   0   shop_order      2   
22277   0   2018-01-29 01:00:00 2018-01-29 00:00:00     Order – January 29, 2018 @ 01:00 AM       wc-completed    open    closed      order-29-01-18-2            2018-03-19 17:12:33 2018-03-19 16:12:33     0   https://www.hemen-biarritz.com/?post_type=shop_ord...   0   shop_order      2   
22276   0   2018-01-28 01:00:00 2018-01-28 00:00:00     Order – January 28, 2018 @ 01:00 AM       wc-completed    open    closed      order-28-01-18-2            2018-03-19 17:12:33 2018-03-19 16:12:33     0   https://www.hemen-biarritz.com/?post_type=shop_ord...   0   shop_order      2   
22275   0   2018-01-25 01:00:00 2018-01-25 00:00:00     Order – January 25, 2018 @ 01:00 AM       wc-completed    open    closed      order-25-01-18-2            2018-03-19 17:12:33 2018-03-19 16:12:33     0   https://www.hemen-biarritz.com/?post_type=shop_ord...   0   shop_order      2   
22232   154 2018-01-24 00:00:00 2018-01-24 00:00:00     Order – January 24, 2018 @ 12:00 AM       wc-completed    open    closed      order-24-01-18-4            2018-01-24 00:00:00 2018-01-24 00:00:00     0   https://www.hemen-biarritz.com/?post_type=shop_ord...   0   shop_order      0   
22230   213 2018-01-23 00:00:00 2018-01-23 00:00:00     Order – January 23, 2018 @ 12:00 AM       wc-completed    open    closed      order-23-01-18-2            2018-01-23 00:00:00 2018-01-23 00:00:00     0   https://www.hemen-biarritz.com/?post_type=shop_ord...   0   shop_order      0   
22229   0   2018-01-22 00:00:00 2018-01-22 00:00:00     Order – January 22, 2018 @ 12:00 AM       wc-completed    open    closed      order-22-01-18-2            2018-01-22 00:00:00 2018-01-22 00:00:00     0   https://www.hemen-biarritz.com/?post_type=shop_ord...   0   shop_order      0   
22228   224 2018-01-20 00:00:00 2018-01-20 00:00:00     Order – January 20, 2018 @ 12:00 AM       wc-completed    open    closed      order-20-01-18-2            2018-01-20 00:00:00 2018-01-20 00:00:00     0   https://www.hemen-biarritz.com/?post_type=shop_ord...   0   shop_order      0   
22227   0   2018-01-19 00:00:00 2018-01-19 00:00:00     Order – January 19, 2018 @ 12:00 AM       wc-completed    open    closed      order-19-01-18-2            2018-01-19 00:00:00 2018-01-19 00:00:00     0   https://www.hemen-biarritz.com/?post_type=shop_ord...   0   shop_order      0   
22226   0   2018-01-17 00:00:00 2018-01-17 00:00:00     Order – January 17, 2018 @ 12:00 AM       wc-completed    open    closed      order-17-01-18-2            2018-01-17 00:00:00 2018-01-17 00:00:00     0   https://www.hemen-biarritz.com/?post_type=shop_ord...   0   shop_order      0   
22225   0   2018-01-16 00:00:00 2018-01-16 00:00:00     Order – January 16, 2018 @ 12:00 AM       wc-completed    open    closed      order-16-01-18-2            2018-01-16 00:00:00 2018-01-16 00:00:00     0   https://www.hemen-biarritz.com/?post_type=shop_ord...   0   shop_order      0   
22224   0   2018-01-15 00:00:00 2018-01-15 00:00:00     Order – January 15, 2018 @ 12:00 AM       wc-completed    open    closed      order-15-01-18-4            2018-01-15 00:00:00 2018-01-15 00:00:00     0   https://www.hemen-biarritz.com/?post_type=shop_ord...   0   shop_order      0   
22222   0   2018-01-14 00:00:00 2018-01-14 00:00:00     Order – January 14, 2018 @ 12:00 AM       wc-completed    open    closed      order-14-01-18-6            2018-01-14 00:00:00 2018-01-14 00:00:00     0   https://www.hemen-biarritz.com/?post_type=shop_ord...   0   shop_order      0   
编辑:

SELECT DISTINCT post_date FROM `wp_posts` WHERE post_type = 'shop_order' AND post_status = 'wc-completed'
给我614个结果,是前一个请求的两倍

WordPress:5.0

Woocommerce:3.5.2

尝试删除连接,任意保留带有max
ID的记录

DELETE
FROM wp_posts
WHERE post_type = 'shop_order' AND post_status = 'wc-completed' AND
      ID NOT IN (SELECT ID
                 FROM (SELECT MAX(ID) AS ID FROM wp_posts
                       GROUP BY post_date, post_author) t);
如果您只想查看删除了逻辑副本的帖子,请尝试:

SELECT w1.*
FROM wp_posts w1
INNER JOIN
(
    SELECT post_date, post_author, MAX(ID) AS max_id
    FROM wp_posts
    WHERE post_type = 'shop_order' AND post_status = 'wc-completed'
    GROUP BY post_date, post_author
) w2
    ON w1.post_date = w2.post_date AND
       w1.post_author = w2.post_author AND
       w1.ID = w2.max_id
WHERE w1.post_type = 'shop_order' AND w1.post_status = 'wc-completed'

首先,如果您不知道,woocommerce订单数据位于四(4)个表中:

  • wp\u帖子
  • wp_postmeta
  • wp\u商业\u订单\u项目
  • wp\u woodcommerce\u order\u itemmeta
因此,下面的钩子函数使用
WPDB
类和方法。它将:

  • 在一个查询中获取所有重复的订单ID(最低ID)(搜索通常唯一的重复订单键)
  • 在一次查询中删除所有重复的查询订单
  • 但一定要在备份之前进行数据库备份

    该代码将在任何前端页面加载上一次完成任务(使用后删除):

    代码进入活动子主题(或活动主题)的function.php文件。测试和工作

    重复的订单将在第一次前端页面加载时删除。因此,在此之后,您可以删除或注释代码


    你想删除记录,还是只想查看重复记录之外的所有内容?嗨,我想先列出它们,然后删除它们。是什么使一对记录重复?这是post_date字段和post_author您希望从所有重复的记录中保留哪个记录?我只需要在wp_posts中搜索匹配的:WHERE post_type='shop_order'和post_status='wc-completed',您可以在我的question@Louis我更新了我的答案,很抱歉错过了。这不会改变整体战略。谢谢!使用您建议的
    选择w1.*
    命令,我得到了686个结果。似乎很多。这是否包括所有重复项(唯一项和它的重复项)?不,它不应该包括重复项,正如您定义的那样。我在导入订单后遇到同样的问题,并且每个订单都导入了5次。但是,每增加一个副本,订单号都是唯一的。你知道我该怎么解决这个问题吗?有同样的问题。我们的订单显示了3次,但订单号不同。
    add_action( 'template_redirect', 'progressive_delete_duplicated_orders' );
    function progressive_delete_duplicated_orders() {
        global $wpdb;
    
        // Get duplicated orders (smaller ID)
        $duplicated_orders = (array) $wpdb->get_col("
            SELECT p.ID, pm.meta_value, COUNT(*) as c
            FROM {$wpdb->prefix}postmeta as pm
            INNER JOIN {$wpdb->prefix}posts as p ON p.ID = pm.post_id
            WHERE p.post_status = 'wc-completed'
            AND pm.meta_key = '_order_key'
            GROUP BY pm.meta_value
            HAVING c > 1
        ");
    
        if( sizeof($duplicated_orders) == 1 )
            $where_clause = 'WHERE p.ID = ' . reset($duplicated_orders);
        elseif( sizeof($duplicated_orders) > 1 )
            $where_clause = 'WHERE p.ID IN (' . implode( ',',$duplicated_orders ) . ')';
        else return; // Exit
    
        // Delete duplicated Orders data everywhere
        $wpdb->query("
            DELETE p, pm, woi, woim
            FROM {$wpdb->prefix}posts as p
            INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id
            INNER JOIN {$wpdb->prefix}woocommerce_order_items as woi ON p.ID = woi.order_id
            INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
            $where_clause
        ");
    }