Woocommerce:可下载文件在数据库恢复后消失

Woocommerce: downloadable files disappear after database restore

恢复我的 WordPress WooCommerce 数据库后,我的虚拟产品的所有 可下载文件 都消失了。

我已经查询了 wp_postmeta table,发现 _downloadable_files 条目仍然存在,并且我已经验证 URL 在 [=35] =] 仍然有效。

但是,这些文件不再显示为订单电子邮件中的链接,不再出现在 我的帐户 页面中,也不再出现在 可下载文件中产品数据中的部分编辑产品

据我所知,唯一可行的解​​决方法是手动重新输入所有文件。

我正在使用 Apache2 和 MySQL。这些文件存储在为 WordPress 提供服务的同一台 Apache 服务器上。

我正在尝试将开发数据库从不同的开发服务器复制到新环境 并且我正在尝试找到一种有效的方法来执行此操作,而无需手动重新输入所有可下载文件。

深入研究,我认为问题在于 WooCommerce 正在生成有关可下载文件 URL 的内容的 MD5,该文件不会从一台服务器传输到另一台服务器。

查看 WordPress wp_postmeta table 中的数据,我明白了

mysql> SELECT post_id,meta_value FROM wordpress.wp_postmeta where meta_key='_downloadable_files';
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| post_id | meta_value                                                                                                                                                                                            |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|      33 | a:1:{s:32:"fccc91f867cc071737bea5433d1c3181";a:2:{s:4:"name";s:3:"fox";s:4:"file";s:61:"http://_123456789_.com/wp-content/uploads/2015/03/fox.png";}}  

我的猜测是,当数据库转移到新主机时,fccc91f867cc071737bea5433d1c3181 值不知何故无法识别为有效。

为了解决这个问题,我写了一个 PHP 脚本来读取数据库,然后通过 Gerhard Potgieter 的 WooCommerce REST API PHP client 使用 WooCommerce REST API 重新加载可下载文件。

<?php

error_reporting( E_ALL );
ini_set( 'display_errors', 'On' );
require_once "class-wc-api-client.php";

$consumer_key = 'ck_examplexxx'; // Add your own Consumer Key here
$consumer_secret = 'cs_secretxxx'; // Add your own Consumer Secret here
$store_url = 'http://123456789/'; // Add the home URL to the store you want to connect to here

// Initialize the class
$wc_api = new WC_API_Client( $consumer_key, $consumer_secret, $store_url );

$servername = "_dbhost_";
$username = "wordpress";
$password = "__password__";
$dbname = "wordpress";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT post_id,meta_value FROM wordpress.wp_postmeta where meta_key='_downloadable_files'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  // output data of each row
  while($row = $result->fetch_assoc()) {
    $product_id = $row["post_id"];
    $meta_value = $row["meta_value"];
    preg_match("/.+\:\"(http:\/\/.+\/wp-content\/uploads\/.+\/.+\/(.+)\..+)\".+/", $meta_value, $m);
    print_r( $wc_api->update_product( $product_id, '{
          "product": {
            "downloads": [
              {
                "name": "' . $m[2] . '",
                "file": "' . $m[1] . '"
              }
            ]
          }
        }'));
  }
} else {
  echo "0 results";
}
$conn->close();

我遇到了同样的问题。 通过使用您的答案并使用 update_post_meta() 而不是 WooCommerce REST API

来修复它
$sql = "SELECT post_id,meta_value FROM wp_postmeta where meta_key='_downloadable_files'";
$result = $conn->query($sql);

if ($result->num_rows > 0) 
{
  // output data of each row
  while($row = $result->fetch_assoc()) 
  {
    $product_id = $row["post_id"];
    $meta_value = $row["meta_value"];

    $v = explode("\"", $meta_value);

    $results = array_filter($v, function($value) {
        return strpos($value, 'https') !== false;
    });

    $file_path =  array_pop($results); 

    preg_match("/.+\:\"(https:\/\/.+\/wp-content\/uploads\/.+\/.+\/(.+)\..+)\".+/", $meta_value, $mn);

    $file_name = $mn[2];
    $md5_num =  md5( $m );

    $abe_file = array();
     $abe_file[0][$md5_num] = array(
        'name'   =>  $file_name,
        'file'   =>  $file_path
    );

    if(update_post_meta( $product_id, '_downloadable_files', $abe_file[0] ))
    {
        echo "$product_id . yay <br/>";
    }
    else{echo "waz <br/>";}


  }



} else {
  echo "0 results";
}
$conn->close();