Zdravím vás,
dle seriálu kde si pomocí návodu vytvoříte internetový obchod postavený na frameworku nette jsem si ho vytvořil ale mám pár problémů se kterými si nevím rady... https://www.itnetwork.cz/php/nette/e-shop
Takže máme velkoobchodníka u který nám poskytl xml import pro jeho zboží abychom si ho mohli nahrát na náš obchod... problém je v tom že na starým systému jsme měli udělaný kód který nám díky tomu mohl z xml vytahovat data a nahrávat na náš obchod... jenže tím že teď již máme jiný systém je potřeba ten kód upravit abychom ho mohli opět pomocí cronu volat....
<?php
/**
* Description of Import
*
* @author David Skála <skala2524@gmail.com>
*/
class Import {
CONST URL = 'http://navratws:ws13852@WWW.SPOKEY.CZ/i6ws/Default.asmx/GetResult?resultType=X-StoItemBaseComplEl';
private $slozka;
private $slozkaDb;
/** @var DibiConnection */
private $dibi;
public function __construct($dibi) {
$this->log('init');
$this->dibi = $dibi;
$this->slozka = __DIR__ . '/../files/catalog/items/';
$this->slozkaDb = 'catalog/items/';
/*
$cdir = scandir($this->slozka);
foreach ($cdir as $value) {
if (intval($value) > 112)
$this->rrmdir($this->slozka . $value);
}
exit;
*/
}
private function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (is_dir($dir . "/" . $object))
$this->rrmdir($dir . "/" . $object);
else
unlink($dir . "/" . $object);
}
}
rmdir($dir);
}
}
/**
*
* @param string $message
*/
private function log($message) {
echo sprintf('<span style="color: #CCC;">bellugio:</span> %s<br>', $message);
}
/**
*
* @return type
*/
public function getXml() {
$this->log('getXml: ' . self::URL);
return file_get_contents(self::URL);
}
public function zpracujXmlKategorie($xml) {
$poleLevel1 = [
'Míčové a společenské sporty' => 2,
'Fitness a posilovna' => 2,
'Vodní sporty' => 2,
'Skating' => 2,
'Bojové sporty' => 2,
'Tenis a badminton' => 2,
'Outdoor' => 3,
'Sport. obuv a nazouváky' => 4,
'Termoprádlo' => 4,
'Funkční oblečení' => 4
];
foreach ($xml->StoItem as $item) {
$tmp1 = explode(' ', $item->SPresentTree->Level1);
unset($tmp1[0]);
$level1 = trim(implode(' ', $tmp1));
$idLevel0 = isset($poleLevel1[$level1]) ? $poleLevel1[$level1] : 'false';
/* level 1 */
if ($idLevel0) {
$data = $this->vratDataProKategorii($idLevel0, $level1);
$kategorieId_1 = $this->dibi->query(sprintf('SELECT id FROM [eshop_category] WHERE [url] = "%s" LIMIT 1', $data['url']))->fetchSingle();
if (!$kategorieId_1) {
$this->dibi->query('INSERT INTO [eshop_category]', $data);
$this->log(sprintf('Vytvořena kategorie úrovně %d s názvem %s', 1, $level1));
$kategorieId_1 = $this->dibi->getInsertId();
}
/* level 2 */
$tmp2 = explode(' ', $item->SPresentTree->Level2);
unset($tmp2[0]);
$level2 = trim(implode(' ', $tmp2));
if ($level2) {
$data = $this->vratDataProKategorii($kategorieId_1, $level2);
$kategorieId_2 = $this->dibi->query(sprintf('SELECT id FROM [eshop_category] WHERE [url] = "%s" LIMIT 1', $data['url']))->fetchSingle();
if (!$kategorieId_2) {
$this->dibi->query('INSERT INTO [eshop_category]', $data);
$this->log(sprintf('Vytvořena kategorie úrovně %d s názvem %s', 2, $level2));
$kategorieId_2 = $this->dibi->getInsertId();
}
/* level 3 */
$tmp3 = explode(' ', $item->SPresentTree->Level3);
unset($tmp3[0]);
$level3 = trim(implode(' ', $tmp3));
if ($level3) {
$data = $this->vratDataProKategorii($kategorieId_2, $level3);
$kategorieId_3 = $this->dibi->query(sprintf('SELECT id FROM [eshop_category] WHERE [url] = "%s" LIMIT 1', $data['url']))->fetchSingle();
if (!$kategorieId_3) {
$this->dibi->query('INSERT INTO [eshop_category]', $data);
$this->log(sprintf('Vytvořena kategorie úrovně %d s názvem %s', 3, $level3));
}
}
}
}
}
}
private function vratDataProKategorii($parentId, $jmeno) {
return [
'parentid' => $parentId,
'main_category' => 1,
'imp_idgroup' => NULL,
'imp_id' => NULL,
'imp_parentid' => NULL,
'level' => 1,
'idlang' => 1,
'idlang_group' => 0,
'name' => $jmeno,
'url' => $this->webalize($jmeno),
'description' => '',
'order' => 0,
'inmenu' => 1,
'unpacked' => 0,
'idfile' => NULL,
'deleted' => 0,
'meta_title' => $jmeno,
'meta_description' => '',
'meta_keywords' => sprintf('%s, Spokey, Bellugio', $jmeno),
's_minorderprice' => 0,
's_minorderprice_curr' => 'CZK',
's_idpdprofile' => 0,
's_freedelivery' => 0,
's_freedelivery_curr' => 'CZK',
's_marginpricecoeff' => 1.6,
'similaritemprice' => 0,
'megaaction' => 0,
'megaaction_ladies' => 0,
'idmegaactionfile' => NULL,
'idmegaactionfile_ladies' => NULL,
'typ' => 0,
'druh' => 0,
'kolekce' => 0,
'tempish' => 0,
'path' => 0,
'bg_file' => 0,
'subimg_id' => 0,
'men' => 0,
'women' => 0,
'children' => 0,
'micro_kolobezky' => 0,
'micro_kolobezky_code' => NULL,
'prima_outdoor' => 0,
'prima_outdoor_code' => NULL,
'aspire' => NULL,
'aspire_code' => NULL,
'salming' => NULL,
'salming_code' => NULL,
'floorballplayer' => NULL,
'floorballplayer_code' => NULL,
'yate' => '',
'yate_code' => '',
'heureka' => '',
'mzd' => 0,
'taxbox' => '',
'taxbox0' => '',
'taxbox1' => '',
'taxbox2' => '',
'taxbox3' => '',
'taxbox4' => ''
];
}
/**
*
* @param type $xml
*/
public function zpracujXmlZbozi($xml) {
$order = $this->dibi->query("SELECT max(eshop_catalog.order) AS orders FROM [eshop_catalog]")->fetchSingle() + 1;
foreach ($xml->StoItem as $item) {
$idKat = $this->dibi->query(sprintf("SELECT itemid FROM [eshop_catalog_variants] WHERE [catno] = '%s' LIMIT 1", $item->Code))->fetchSingle();
$nazev = $idKat ? $this->dibi->query(sprintf("SELECT name FROM [eshop_catalog] WHERE [id] = %s LIMIT 1", $idKat))->fetchSingle() : false;
if (!$nazev) {
$this->log($item->Id . ': ' . $item->Name);
if ($item->SisName == 'Výprodej')
$action = 1;
if ($item->SisName == 'Novinka')
$news = 1;
$dataCatalog = [
'imp_id' => intval($item->Id),
'imp_idgroup' => NULL,
'idlang' => 1,
'idlang_group' => 0,
'name' => (string) $item->Name,
'shortname' => (string) $item->Name,
'url' => $this->webalize($item->Name),
'addpageid' => 0,
'addmagazin' => '',
'exturl' => NULL,
'ptitle' => (string) $item->Name,
'pdescription' => (string) $item->Note,
'pkeywords' => '',
'shortdescription' => '',
'description' => (string) $item->Note,
'varranty' => '24 měsíců',
'vatflag' => 0,
'idmanufacturer' => NULL,
'iddistributor' => NULL,
'topsales' => 0,
'index' => 0,
'index_ladies' => 0,
'action' => $action,
'sleva' => 0,
'news' => $news,
'megaaction' => 0,
'megaaction_ladies' => 0,
'deleted' => 0,
'idpdprofile' => NULL,
'idfile' => NULL, //ssss
'idmegaactionfile' => NULL,
'idmegaactionfile_ladies' => NULL,
'idgal' => NULL,
'v_simple' => 1,
'v_simpleid' => NULL,
'currency' => 'CZK',
'unit' => '',
'similaritempriceflag' => 1,
'similaritemprice' => 0,
'order' => $order++,
'orderindex' => 0,
'user_note' => '',
'url_import' => '',
'color_import' => '',
'id_import' => 0,
'tempish_id' => '',
'men' => 0,
'women' => 0,
'children' => 0,
'micro_kolobezky_code' => NULL,
'prima_outdoor_code' => NULL,
'aspire_code' => NULL,
'salming_code' => NULL,
'floorballplayer_code' => NULL,
'yate_code' => NULL,
'brand' => 1,
'updated' => date('Y-m-d H:i:s'),
'offdate' => '0000-00-00 00:00:00',
'ondate' => '0000-00-00 00:00:00',
'import_active' => 1,
'taxbox' => -1
];
$this->dibi->query('INSERT INTO [eshop_catalog]', $dataCatalog);
$lastId = $this->dibi->getInsertId();
/* cena */
$dataPrice = [
'itemid' => $lastId,
'imp_idgroup' => NULL,
'params' => '',
'catno' => (string) ($item->Code),
'partno' => '',
'ean' => '',
'purchaseprice' => intval($item->PriceDea),
'commonprice' => 0,
'pcvflag' => 0,
'price' => intval($item->PriceEU),
'availability' => 3,
'simple' => 1,
'order' => 0,
'deleted' => 0,
'usestorecount' => 0,
'storecount' => 0,
'minordercount' => 0,
'supplier_stock' => 0,
'OznX1' => '',
'PozY1' => '',
'supplier_taken' => 0
];
$this->dibi->query('INSERT INTO [eshop_catalog_variants]', $dataPrice);
/* katerogie */
foreach (['Level1', 'Level2', 'Level3'] AS $_level) {
$tmp = explode(' ', $item->SPresentTree->{$_level});
unset($tmp[0]);
$level = implode(' ', $tmp);
$kategorieId = $this->dibi->query(sprintf('SELECT id FROM [eshop_category] WHERE [name] = "%s" LIMIT 1', $level))->fetchSingle();
if ($kategorieId) {
$dataKat = [
'iditem' => $lastId,
'idcat' => $kategorieId,
'topsales' => 0
];
$this->dibi->query('INSERT INTO [eshop_catalog_bindcat]', $dataKat);
}
}
/* obrazky */
$dataKat = [
'imp_idgroup' => NULL,
'name' => '',
'description' => '',
'file' => '',
'extfile' => NULL,
'ext' => 0,
'type' => 1,
'order' => 0,
'owner_group' => NULL,
'idgal' => NULL,
'watermark' => 0,
'deleted' => 0,
'upload_date' => date('Y-m-d H:i:s')
];
$this->dibi->query('INSERT INTO [files]', $dataKat);
$idFotky = $this->dibi->getInsertId();
/* obrazky */
if (!file_exists($this->slozka . $lastId . '/' . $idFotky . '/'))
mkdir($this->slozka . $lastId . '/' . $idFotky . '/', 0777, true);
copy($item->ImgUrl, $this->slozka . $lastId . '/' . $idFotky . '/' . $this->webalize($item->Name) . '.jpg');
$this->dibi->query('UPDATE [eshop_catalog] SET idfile = ' . $idFotky . ' WHERE id = ' . $lastId);
$this->dibi->query('UPDATE [files] SET file = "' . ($this->slozkaDb . $lastId . '/' . $idFotky . '/' . $this->webalize($item->Name) . '.jpg') . '" WHERE id = ' . $idFotky);
} else {
$lastId = $this->dibi->query(sprintf("SELECT id FROM [eshop_catalog] WHERE [id] = %s LIMIT 1", $idKat))->fetchSingle();
foreach (['Level1', 'Level2', 'Level3'] AS $_level) {
$tmp = explode(' ', $item->SPresentTree->{$_level});
unset($tmp[0]);
$level = implode(' ', $tmp);
if ($level) {
$kategorieId = $this->dibi->query(sprintf('SELECT id FROM [eshop_category] WHERE [name] = "%s" LIMIT 1', $level))->fetchSingle();
if ($kategorieId) {
$bindCat = $this->dibi->query(sprintf("SELECT iditem FROM [eshop_catalog_bindcat] WHERE [iditem] = %s AND [idcat] = %s LIMIT 1", $lastId, $kategorieId))->fetchSingle();
if (!$bindCat) {
$dataKat = [
'iditem' => $lastId,
'idcat' => $kategorieId,
'topsales' => 0
];
$this->dibi->query('INSERT INTO [eshop_catalog_bindcat]', $dataKat);
}
}
}
}
$action = $news = 0;
if ($item->SisName == 'Výprodej')
$action = 1;
if ($item->SisName == 'Novinka')
$news = 1;
$this->dibi->query(sprintf('UPDATE [eshop_catalog] SET action = %s, news = %s WHERE id = %s', $action, $news, $lastId));
}
}
}
private $prevodniTabulka = [
'ä' => 'a',
'Ä' => 'A',
'á' => 'a',
'Á' => 'A',
'à' => 'a',
'À' => 'A',
'ã' => 'a',
'Ã' => 'A',
'â' => 'a',
'Â' => 'A',
'č' => 'c',
'Č' => 'C',
'ć' => 'c',
'Ć' => 'C',
'ď' => 'd',
'Ď' => 'D',
'ě' => 'e',
'Ě' => 'E',
'é' => 'e',
'É' => 'E',
'ë' => 'e',
'Ë' => 'E',
'è' => 'e',
'È' => 'E',
'ê' => 'e',
'Ê' => 'E',
'í' => 'i',
'Í' => 'I',
'ï' => 'i',
'Ï' => 'I',
'ì' => 'i',
'Ì' => 'I',
'î' => 'i',
'Î' => 'I',
'ľ' => 'l',
'Ľ' => 'L',
'ĺ' => 'l',
'Ĺ' => 'L',
'ń' => 'n',
'Ń' => 'N',
'ň' => 'n',
'Ň' => 'N',
'ñ' => 'n',
'Ñ' => 'N',
'ó' => 'o',
'Ó' => 'O',
'ö' => 'o',
'Ö' => 'O',
'ô' => 'o',
'Ô' => 'O',
'ò' => 'o',
'Ò' => 'O',
'õ' => 'o',
'Õ' => 'O',
'ő' => 'o',
'Ő' => 'O',
'ř' => 'r',
'Ř' => 'R',
'ŕ' => 'r',
'Ŕ' => 'R',
'š' => 's',
'Š' => 'S',
'ś' => 's',
'Ś' => 'S',
'ť' => 't',
'Ť' => 'T',
'ú' => 'u',
'Ú' => 'U',
'ů' => 'u',
'Ů' => 'U',
'ü' => 'u',
'Ü' => 'U',
'ù' => 'u',
'Ù' => 'U',
'ũ' => 'u',
'Ũ' => 'U',
'û' => 'u',
'Û' => 'U',
'ý' => 'y',
'Ý' => 'Y',
'ž' => 'z',
'Ž' => 'Z',
'ź' => 'z',
'Ź' => 'Z'
];
private
function webalize($string) {
$url = strtr($string, $this->prevodniTabulka);
$url = preg_replace('~[^\\pL0-9_]+~u', '-', $url);
$url = trim($url, "-");
$url = iconv("utf-8", "us-ascii//TRANSLIT", $url);
$url = strtolower($url);
$url = preg_replace('~[^-a-z0-9_]+~', '', $url);
return $url;
}
}
To je starý kód importu....
A druhá věc jak mohu v payments udělat možnost že pokud dotyčný nakoupí za 3000Kč tak má dopravu zadarmo?
Předem děkuji všem dotyčným za pomoc