Tags

, , , ,

downloadable-product-overview

Hello Everyone, Today I am going to share my experience of debugging WooCommerce download links which was lost after the transferring the host. Actually transferring the host  sometimes broke the serialised data and in our case serialised value of   _downloadable_files  meta key was broken. After detecting the issue was from broken serialised data I went to take help of plugin below which doesn’t help me on this case, So I went on development of module which will fix this error:

Note: Please keep backup of your database before applying this method

Plugins I tested with:

Please copy the codes below on to your theme functions.php file and saved it.

/**
* correct broken serialized data of woocommerce download meta data
*/
$args = array(
	'post_type' => 'product',
	'posts_per_page' => -1,
	'meta_key' => '_downloadable_files',
	'fields' => 'ids'
);

$product = get_posts( $args );
foreach ($product as $key => $product_id ) {
	$post_meta = get_post_meta( $product_id ); 
	$coorected_serialized_data = $post_meta[ '_downloadable_files' ];
	$unserialize = unserialize( themeslug_serialize_corrector( $coorected_serialized_data[0] ) );
	if( $unserialize  ) {
               //Comment out this loop if your server doesn't support SSL or you do not want to change your download links to https:
		foreach ($unserialize as $hash_key => $file_details ) {
			$file_details[ 'file' ] = esc_url_raw( preg_replace( '/http/', 'https', $file_details[ 'file' ] ) );
			//construct array
			$final_meta_value = array(
				$hash_key => $file_details
			);
		}
		
		//array variable will be automatically serialized before saving on to database.
		update_post_meta( absint( $product_id  ), '_downloadable_files', $unserialize  );
	}
	
}
/** 
* @return corrected serialised string 
*/
function themeslug_serialize_corrector($serialized_string){
    // at first, check if "fixing" is really needed at all. After that, security checkup.
    if ( @unserialize($serialized_string) !== true &&  preg_match('/^[aOs]:/', $serialized_string) ) {
         $serialized_string = preg_replace_callback( '/s\:(\d+)\:\"(.*?)\";/s',    function($matches){return 's:'.strlen($matches[2]).':"'.$matches[2].'";'; },   $serialized_string );
    }
    return $serialized_string;
}

Note: This code was tested with website of 50 products, So I highly encourage you to use cron job to implements this code on large store. Also please remove the code after the use.

Let me know if you have any question or queries or any other best solution.You can ping me on adhsushil7@gmail.com

Best Regards

Advertisements