Woocommerce: downloadable files disappear after database restore

后端 未结 2 943
时光说笑
时光说笑 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.

    <?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();
    
    0 讨论(0)
  • 2021-01-26 14:22

    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();
    
    0 讨论(0)
提交回复
热议问题