Page 1 of 1

How do I delete One Order in Woocommerce by ID via PHPmyadmi

Posted: Thu Nov 30, 2017 5:04 am
by simonmlewis
We have had a major issue, and need to delete two orders in our Trash.
We cannot access the Woocommerce trash, as one or more of these orders are causing the page not to load.

So we want to remove for example, order_id 2117 from the database.
It's marked as trash. I see it should be removed from two other tables, as well as wp_posts. what is the trick in one query to do it?

Simon

Re: How do I delete One Order in Woocommerce by ID via PHPmy

Posted: Thu Nov 30, 2017 11:37 am
by VladSun
I am not familiar with the platform you use, so my advice might be useless, but you should be careful with ON DELETE CASCADE foreign keys and (low probability) triggers being used.
I even created a DELETE CASCADE tracer for a legacy project to ensure I am not going to delete something important.

Code: Select all

class MySql_Delete_Cascade_Tracer {

  /**
   * @var Database
   */
  protected $db;

  /**
   * @var string[]
   */
  protected $affectedTables;

  /**
   * @param Database $db
   */
  public function __construct(Database $db) {
    $this->db = $db;
    $this->affectedTables = [];
  }

  /**
   * @param string $table
   * @return array[]
   */
  protected function findReferencingTablesForTable($table) {
    return $this->db->queryRows("
      SELECT
        TABLE_NAME
      FROM
        information_schema.REFERENTIAL_CONSTRAINTS
      WHERE
        CONSTRAINT_SCHEMA = '" . $this->db->_dbName . "'
        AND
        REFERENCED_TABLE_NAME = '" . $table . "'
        AND
        DELETE_RULE = 'CASCADE'
    ");
  }

  /**
   * @param string $table
   */
  protected function traceRecursively($table) {
    foreach ($this->findReferencingTablesForTable($table) as $referencingTable) {
      $referencingTableName = $referencingTable['TABLE_NAME'];
      if (!isset($this->affectedTables[$referencingTableName])) {
        $this->affectedTables[$referencingTableName] = $referencingTableName;
        $this->traceRecursively($referencingTableName);
      }
    }
  }

  /**
   * @param string $table
   */
  public function trace($table) {
    $this->affectedTables = [];
    $this->traceRecursively($table);
  }

  /**
   * @return string[]
   */
  public function getAffectedTables() {
    return array_values($this->affectedTables);
  }

}

$tracer = new MySql_Delete_Cascade_Tracer($db);
$tracer->trace($table);
print_r($tracer->getAffectedTables());

Re: How do I delete One Order in Woocommerce by ID via PHPmy

Posted: Fri Dec 08, 2017 8:27 am
by simonmlewis
It's Wordpress Woocommerce. We have five orders in Trash, but we canot open trash because one order is dodgy and it breaks the page.
So need to do it some other way.