【问题标题】:Woocommerce: downloadable files disappear after database restoreWoocommerce:可下载的文件在数据库恢复后消失
【发布时间】:2015-04-14 18:51:29
【问题描述】:

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

我查询了wp_postmeta 表,发现_downloadable_files 条目仍然存在,并且我已验证表中的URL 仍然有效。

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

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

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

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

【问题讨论】:

    标签: php mysql wordpress woocommerce database-restore


    【解决方案1】:

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

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

    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();
    

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。 通过使用您的答案并使用 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();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-01-02
        • 1970-01-01
        • 1970-01-01
        • 2017-08-24
        • 1970-01-01
        • 1970-01-01
        • 2017-09-05
        相关资源
        最近更新 更多