问题
After restoring my WordPress WooCommerce database, all the Downloadable Files for my virtual products have disappeared.
I have queried the wp_postmeta
table and see that the _downloadable_files
entries are still there and I have verified that the URL's in the table are still valid.
However, the files no longer appear as links in order emails, no longer appear in the My Account page, and no longer appear in the Downloadable Files section in the Product Data in the Edit Product.
The only fix that I know works is to manually re-enter all the files.
I'm using Apache2 and MySQL. The files are stored on the same Apache server that is serving WordPress.
I am trying to copy a development database from a different development server to a new environment and am trying to find an efficient way to do this without having to manually re-enter all the downloadable files.
回答1:
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.
<?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:
I had the same problem. Fixed it by using your answer & using update_post_meta(), instead of 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();
来源:https://stackoverflow.com/questions/29635202/woocommerce-downloadable-files-disappear-after-database-restore