WordPress.org

Ready to get started?Download WordPress

Forums

Woocommerce osCommerce Import
I re-wrote image importing in case it helps anyone (1 post)

  1. j893
    Member
    Posted 5 months ago #

    I was working to import an OS Commerce store for a client who had created tens of thousands of product images that contained spaces, commas, extra periods, upper and lower case characters, dashes, underscores etc. and found that no files that contained spaces or commas were importing correctly. They would import as 404 error HTML pages with a .jpg (or .gif .png etc.) extension.

    First I added a php.ini to the "woocommerce-osc-import" plugin directory, the image "uploads" directory, and the main wordpress directory, containing the following PHP directives (to help the importing process complete and generally keep PHP restrictions from getting in the way. The max execution time is the main thing to change):

    upload_max_filesize = 64M
    post_max_size = 64M
    max_execution_time = 3000

    Then I made the following changes to the plugin:

    Starting with the function "woocommerce_osc_import_image", I added/changed (see comments) the following code (found on lines: 31-46, 85-88, 223, and 479-494 in version 1.2 of the plugin):

    Changed "urlencode" to "rawurlencode" because only then would it handle spaces correctly. And added a bunch of RegEx to clean up image file names and make everything lowercase in the imported files and database entries. (change/remove what you want to suite your needs. I have it only allowing a-z and 0-9 characters. No dashes or underscores.) Please excuse the somewhat inefficient RegEx and techniques used, as I was troubleshooting (though it does work perfectly for me, so hopefully it will for you as well). I also added a bunch of echo statements to help me see everything that is happening and to make sure importing was working properly. Feel free to remove/change anything you don't want or use.

    Between lines 31-46:

    function woocommerce_osc_import_image($url){
    
    		$brk = '<br />';
    
    		echo $url.' <strong>This is the URL we are working with.</strong>'.$brk;
    
    		$attach_id = 0;
    		$wp_upload_dir = wp_upload_dir();
    
    		/* Removed WordPress "sanitize_file_name", as we will do that below with more precision after we are sure we have the original file captured. */
    		$filename = $wp_upload_dir['path'].'/'.basename($url);
    
    /*		$filename = preg_replace('/\s+/', ' ', $filename); */
    
    		echo $filename.' Step 1: Unsanitized path stored.'.$brk;
    
    		if(file_exists($filename)){
    			$url = $filename;
    			echo $url.' Step 2a: File exists.'.$brk;
    		}else{
    			//Encode the URL.  Changed from "urlencode" to "rawurlencode" to correctly support images with spaces in filename.
    			$base = basename($url);
    			$url = str_replace($base,rawurlencode($base),$url);
    			echo $url.' Step 2b: URL raw encoded from original OSCommerce store path.'.$brk;
    		}
    
    		if($f = @file_get_contents($url)){
    
    			/* Make entire url and file name lower case. */
    			$filename = strtolower($filename);
    
    			echo $filename.' Step 3: Full path changed to lower case.'.$brk;
    
    	/*		$filename = preg_replace('/\s+/', '', $filename);  */
    
    			/* Select only the path without the file name. */
    			$justpathpattern = '/^(.*[\/])/';
    			preg_match($justpathpattern, $filename, $matchesa);
    			$justpath = $matchesa[1];
    
    			echo $justpath.' Step 4: Just the path.'.$brk;
    
    			/* Select only the file name without the path. */
    			$justfilenamepattern = '/([^\/]+)$/';
    			preg_match($justfilenamepattern, $filename, $matchesb);
    			$justfilename = $matchesb[1];
    
    			echo $justfilename.' Step 5: Just the file name and extension.'.$brk;
    
    			/* Select only the file name without the extension. */
    			$justfilenamewithoutextpattern = '/^.*(?=(\.))/';
    			preg_match($justfilenamewithoutextpattern, $justfilename, $matchesc);
    			$justfilenamewithoutext = $matchesc[0];
    
    			echo $justfilenamewithoutext.' Step 6: Just the file name without extension.'.$brk;
    
    			/* Make the file name contain only a-z and 0-9 characters. */
    			$justfilenamewithoutext = preg_replace("/[^a-z0-9]/", '', $justfilenamewithoutext);
    
    			echo $justfilenamewithoutext.' Step 7: Just the file name without extension and no bad characters.'.$brk;
    
    			/* Select only the extension. */
    			$justextpattern = '/\.[^\.]+$/';
    			preg_match($justextpattern, $justfilename, $matchesd);
    			$justext = $matchesd[0];
    
    			echo $justext.' Step 8: Just the file extension.'.$brk;
    
    			/* Reassemble full path and file name with bad characters removed. */
    			$filename = $justpath.$justfilenamewithoutext.$justext;
    
    			echo $filename.' Step 9: The full reassembled path and file name with no bad characters. Ready to be used as imported path.'.$brk.$brk.$brk;
    
    			file_put_contents($filename,$f);

    Removed "urlencode" in the below line, and commented out "add_woocommerce_term_meta" as it is unnecessary and creates duplicate entries:

    Between lines 85-88:

    $url = rtrim($_POST['store_url'],'/').'/images/'.$category['categories_image'];
    						$attach_id = woocommerce_osc_import_image($url);
    					}
    /*  Not needed.  	add_woocommerce_term_meta($term['term_id'], 'order',$category['sort_order']); */

    Removed "urlencode" in the below line:

    On line 223:

    $url = rtrim($_POST['store_url'],'/').'/images/'.$product['products_image'];

    Clarified descriptive text in some form functions below:

    Between lines 479-494:

    <form action="<?php echo $_SERVER['REQUEST_URI'];?>" method="post">
    				<h3>Import data from osCommerce</h3>
    				<p>Enter your oscommerce database information (you will need remote access to your oscommerce database)</p>
    				<p><label>osCommerce store URL (for importing product images. E.g. <strong>http://www.yourstore.com/shop/catalog</strong> ): <input type="text" name="store_url" value="<?php echo $_POST['store_url'];?>" /></label></p>
    				<p><label>osCommerce Database Host: <input type="text" name="store_host" value="localhost" /></label></p>
    				<p><label>osCommerce Database User: <input type="text" name="store_user" value="<?php echo $_POST['store_user'];?>" /></label></p>
    				<p><label>osCommerce Database Password: <input type="text" name="store_pass" value="<?php echo $_POST['store_pass'];?>" /></label></p>
    				<p><label>osCommerce Database Name: <input type="text" name="store_dbname" value="<?php echo $_POST['store_dbname'];?>" /></label></p>
    				<p>Data to Import:<br>
    					<label><input type="checkbox" name="dtype[customers]" value="1" /> Customers (passwords will not be transferred)</label><br>
    					<label><input type="checkbox" name="dtype[orders]" value="1" /> Orders</label><br>
    					<label><input type="checkbox" name="dtype[products]" value="1" /> Categories/Products</label><br>
    					<label><input type="checkbox" name="dtype[pages]" value="1" /> Information Pages</label>
    				</p>
    				<p><input type="submit" value="Import Data" class="button button-primary button-large" /></p>
    			</form>

    https://wordpress.org/plugins/woocommerce-oscommerce-import/

Reply

You must log in to post.

About this Plugin

About this Topic