Anonymous Anonymous - 8 months ago
435 0

No description

PHP

PHP API

<link rel="stylesheet" type="text/css" href="/matt/api.css">
<?php
	// Interpret the CSV file and store each row as an array.
	$csv = array();
	$line = array();

	if (FALSE !== $handle = fopen("https://www.fmasarovic.com/export_feed.php?preset=poshbag_boutique", "r")) {
		while (FALSE !== $row = fgetcsv($handle)) {
			$csv[] = $row;
		}
	}

	foreach ($csv as $row) {
		$new_row = array();
		for ($i = 0, $c = count($csv[0]); $i < $c; ++$i) {
			$new_row[$csv[0][$i]] = $row[$i]; 
		}
		$line[] = $new_row;
	}
	
	// Function getPreviousColumnValue.
	function getPreviousColumnValue(array $data, $from, $column) {
		$data = array_slice($data, 0, $from + 1); // extract sub data array. $from id the current line
		$data = array_reverse($data); // reverse for analyze

		// Loop on each reversed line (from 0 to $from)
		foreach( $data as $row ) {
			// If the index does not exist, skip
			if( !isset($row[$column]) ) continue;

			// If the value (is null or empty) and different of '0', skip
			if( is_null($row[$column]) OR $row[$column] === '' ) continue;

			// Else, return this value !
			return $row[$column];
		}
		return null; // Null, there is no value :(
	}

	// Create client session.
	$client = new SoapClient('https://ts564737-container.zoeysite.com/api/v2_soap/?wsdl');
	$sessionId = $client->login('Matt', '829hdas7bd78');
	
	// Product variables.
	$attributeSets = $client->catalogProductAttributeSetList($sessionId);
	$attributeSet = current($attributeSets);
	$total_rows = 3;
	// $total_rows = count($csv);

	foreach($data as $iLineNumber => $line) {

    // Include this loop at the begenning of you code
    // Iterate on each row of the line:
    foreach($line as $iRowKey => $row) {
        // If the value (is null or empty) and different of '0' ...
        // ... we need to find the previous valid value
        if(is_null($row) OR $row === '') {
            $line[$iRowKey] = getPreviousColumnValue($data, $iLineNumber, $iRowKey);
        }
        // So, if value is valid => use it ; else, find correct one via the function
    }

	// Create Products from CSV.
	for ($sku = 1, $name = 1, $price = 1, $desc = 1, $table = 1, $qty = 1, $cond = 1, $pos = 1, $image = 1, $brand = 1, $color = 1, $material = 1, $style = 1;
		$sku < $total_rows, $name < $total_rows, $price < $total_rows, $desc < $total_rows, $table < $total_rows, $qty < $total_rows, $cond < $total_rows, $pos < $total_rows, $image < $total_rows, $brand < $total_rows, $color < $total_rows, $material < $total_rows, $style < $total_rows;
		++$sku, ++$name, ++$price, ++$desc, ++$table, ++$qty, ++$cond, ++$pos, ++$image, ++$brand, ++$color, ++$material, ++$style) {

		if ($line[$sku]['Product SKU'] != '') {
			try {
				// Create product data.
				$client->catalogProductCreate($sessionId, 'simple', $attributeSet->set_id, $line[$sku]['Product SKU'], array(
					'name' => $line[$name]['Product Name'],
					'price' => $line[$price]['Price'],
					'categories' => array(
						'category' => 'DifferentCategory'
					),
					'description' => $line[$desc]['Product Description (Including HTML)'] . "<br><br>" . $line[$table]['Param table'],
					'weight' => 1,
					'status' => 1,
					'visibility' => 4,
					'tax_class_id' => 1,
					'stock_data' => array(
						'qty' => $line[$qty]['Stock Quantity'],
						'is_in_stock' => 1
					),
					'additional_attributes' => array(
						'single_data' => array(
							array('key' => 'condition', 'value' => $line[$cond]['Condition (Including HTML)']),
							array('key' => 'brand', 'value' => $line[$brand]['Brand']),
							array('key' => 'color', 'value' => $line[$color]['Color']),
							array('key' => 'material', 'value' => $line[$material]['Material']),
							array('key' => 'style', 'value' => $line[$style]['Style'])
						)
					)
				),0);

				// Add product images.
				$client->catalogProductAttributeMediaCreate($sessionId, $line[$sku]['Product SKU'], array(
					'file' => array(
						'content' => base64_encode(file_get_contents($line[$image]['Main Image'])),
						'mime' => 'image/png',
					),
					'position' => $line[$pos]['Image Rank Position'],
					'types' => array('image')
				), 0);
				
				// Echo success message.
				echo '<div class="success_box"><span class="success_box_text"><b>Success:</b> Product <b>' . $line[$sku]['Product SKU'] . '</b> has been created.</div>';
			} catch (SoapFault $e) {
				// Echo error message.
				echo '<div class="error_box"><span class="error_box_text"><b>Error:</b> Cannot create product <b>' . $line[$sku]['Product SKU'] . '</b> - ' . $e->getMessage() . '.</span></div>';
			}
		} else {
			$client->catalogProductAttributeMediaCreate($sessionId, $line[$sku]['Product SKU'], array(
				'file' => array(
					'content' => base64_encode(file_get_contents($line[$image]['Main Image'])),
					'mime' => 'image/png',
				),
				'position' => $line[$pos]['Image Rank Position'],
				'types' => array('image')
			), 0);
		}
	}

	var_dump($line);
	
	}
?>