Woocommerce: downloadable files disappear after database restore

后端 未结 2 944
时光说笑
时光说笑 2021-01-26 13:57

After restoring my WordPress WooCommerce database, all the Downloadable Files for my virtual products have disappeared.

I have queried the wp_post

2条回答
  •  执念已碎
    2021-01-26 14:16

    Digging into this I think that the issue is that WooCommerce is generating an MD5 of something about the Downloadable File URL which does not transfer from one server to another.

    Looking at data in the WordPress wp_postmeta table, I see

    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";}}  
    

    My guess is that the fccc91f867cc071737bea5433d1c3181 value is somehow not recognized as valid when the database is transferred to a new host.

    To work around the issue, I wrote a PHP script to read the database and then reload the Downloadable Files using the WooCommerce REST API via Gerhard Potgieter's WooCommerce REST API PHP client.

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

提交回复
热议问题