tessguefen
3/2/2015 - 4:57 PM

Update Default Variant (for Shoestringking)

Update Default Variant (for Shoestringking)

LACES-TEAM-SEAHAWKS	LACES-FLAT-GREEN-45	1
LACES-TEAM-SEAHAWKS	LACES-FLAT-LTGRAY-45-55142	1
LACES-TEAM-SEAHAWKS	LACES-FLAT-WHITE-45-55131	1
Somethingelse	blah-2	2
Somethingelse	blah-1	3
Somethingelse	blah-4	1
- Create a blank file named import.xml
- Create a blank folder named temp
<?PHP
function readCSV($csvFile){
	$file_handle = fopen($csvFile, 'r');
	while (!feof($file_handle) ) {
		$line_of_text[] = fgetcsv($file_handle, 0);
	}
	fclose($file_handle);
	return $line_of_text;
}

if (isset($_POST['submit']) && isset($_FILES['csv']) && ($_FILES['csv']['name'] !== '' && strpos($_FILES['csv']['name'], '.csv') !== false)) {
        $working_file = 'temp/redirects.csv';
        move_uploaded_file($_FILES['csv']['tmp_name'], $working_file);
        // Set path to CSV file;
        $csv = readCSV($working_file);
        $lastParent = '';
        $counter = 0;
        $handle = fopen('import.xml', 'w');
        foreach ( $csv as $p ) {
            $counter++;
            $parent = $p[0];
            $child = $p[1];
            $quantity = $p[2];
                if ($parent == $lastParent) {
                    fwrite($handle, '<Part product_code="'.$child.'" quantity="1" />');
                } else {
                    if ($counter > 1) {
                        fwrite($handle, '</Parts>');
                        fwrite($handle, '<ProductVariantPricing>');
                        fwrite($handle, '<Method>master</Method>');
                        fwrite($handle, '</ProductVariantPricing>');
                        fwrite($handle, '</ProductVariant_Update_Default>');
                    }
                    if ($parent) {
                        fwrite($handle, '<ProductVariant_Update_Default product_code="'.$parent.'">');
                        fwrite($handle, '<Parts>');
                        fwrite($handle, '<Part product_code="'.$child.'" quantity="'.$quantity.'" />');
                    }
                }
                $lastParent = $parent;
        }
        fclose($handle);
        header('Content-Type: application/xhtml+xml');
        header('Content-Disposition: attachment; filename='.basename('import.xml'));
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize('import.xml'));
        readfile('import.xml');
        exit;
} else {
    $handle = fopen('import.xml', 'w');
    fwrite($handle, ' ');
    fclose($handle);
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
        <div class="row">
            <input type="file" name="csv" class="custom-file-input" />
        </div>
        <div class="row">
            <input type="submit" name="submit" value="Create XML File"/>
        </div>
    </form>
<?php } ?>