horejsja Posted October 6, 2016 Share Posted October 6, 2016 (edited) Hi, im trying to make import script for my shop. since this is my first try to code something for prestashop i basically glued together code snippets i found here on forums anyways, i managed to import first xml feed without problem(5000 products in 200 categories within an hour), but second one is importing incredibly slow. it takes one category about a minute. Both scripts are basically the same, only difference is one more DB query.my code: function addCategory($cat_name, $ref, $pid = 0) { global $webservice; $xml = $webservice->get(array('resource' => 'categories?schema=blank')); $resources = $xml->children()->children(); $n_is_root_category = 0; $n_id_parent=(int)Db::getInstance()->getValue("SELECT prestashop_id FROM "._DB_PREFIX_."import_category WHERE import_id ='".$pid."'"); if($n_id_parent==0 || $pid<=0)$n_id_parent = 2; $n_active = 1; $n_l_id = 1; $n_name = $cat_name; $n_desc = "description"; $n_link_rewrite = Tools::link_rewrite($n_name); $n_meta_title = "metatitle"; $n_meta_description = "metadesc"; $n_meta_keywords = "metakeywords"; unset($resources -> id); unset($resources -> level_depth); unset($resources -> position); unset($resources -> id_shop_default); unset($resources -> date_add); unset($resources -> date_upd); $resources -> active = $n_active; $resources -> id_parent = $n_id_parent; $resources -> id_parent['xlink:href'] = PS_SHOP_PATH . '/api/categories/' . $n_id_parent; $resources -> is_root_category = $n_is_root_category; $node = dom_import_simplexml($resources -> name -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_name)); $resources -> name -> language[0][0] = $n_name; $resources -> name -> language[0][0]['id'] = $n_l_id; $resources -> name -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; // $node = dom_import_simplexml($resources -> description -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_desc)); $resources -> description -> language[0][0] = $n_desc; $resources -> description -> language[0][0]['id'] = $n_l_id; $resources -> description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> link_rewrite -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_link_rewrite)); $resources -> link_rewrite -> language[0][0] = $n_link_rewrite; $resources -> link_rewrite -> language[0][0]['id'] = $n_l_id; $resources -> link_rewrite -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> meta_title -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_meta_title)); $resources -> meta_title -> language[0][0] = $n_meta_title; $resources -> meta_title -> language[0][0]['id'] = $n_l_id; $resources -> meta_title -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $resources -> meta_description -> language[0][0] = $n_meta_description; $resources -> meta_description -> language[0][0]['id'] = $n_l_id; $resources -> meta_description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> meta_keywords -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_meta_keywords)); $resources -> meta_keywords -> language[0][0] = $n_meta_keywords; $resources -> meta_keywords -> language[0][0]['id'] = $n_l_id; $resources -> meta_keywords -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $resources->associations = ''; $resources->active = 1; $resources->id_shop_default = 1; $resources->is_root_category = 0; $resources->name->language[0][0] = $cat_name; $xml = $webservice->add( array( 'resource' => 'categories', 'postXml' => $xml->asXML() ) ); $ps_category_id=$xml->children()->children()->{'id'}; $categories[]=$cat_name; $sql = "INSERT INTO "._DB_PREFIX_."import_category(prestashop_id, import_id, import_name, import_reference) VALUES(".$ps_category_id.",'".$impid."','".$cat_name."','".$ref."')"; Db::getInstance()->Execute($sql); } any idea what is going on?edit: i noticed one more thing it gets slower over time, went from 20sec per category to almost minute and half Edited January 8, 2017 by horejsja (see edit history) 1 Link to comment Share on other sites More sharing options...
Daniel Patilea Posted October 7, 2016 Share Posted October 7, 2016 Hi, im trying to make import script for my shop. since this is my first try to code something for prestashop i basically glued together code snippets i found here on forums anyways, i managed to import first xml feed without problem(5000 products in 200 categories within an hour), but second one is importing incredibly slow. it takes one category about a minute. Both scripts are basically the same, only difference is one more DB query. my code: function addCategory($cat_name, $ref, $pid = 0) { global $webservice; $xml = $webservice->get(array('resource' => 'categories?schema=blank')); $resources = $xml->children()->children(); $n_is_root_category = 0; $n_id_parent=(int)Db::getInstance()->getValue("SELECT prestashop_id FROM "._DB_PREFIX_."import_category WHERE import_id ='".$pid."'"); if($n_id_parent==0 || $pid<=0)$n_id_parent = 2; $n_active = 1; $n_l_id = 1; $n_name = $cat_name; $n_desc = "description"; $n_link_rewrite = Tools::link_rewrite($n_name); $n_meta_title = "metatitle"; $n_meta_description = "metadesc"; $n_meta_keywords = "metakeywords"; unset($resources -> id); unset($resources -> level_depth); unset($resources -> position); unset($resources -> id_shop_default); unset($resources -> date_add); unset($resources -> date_upd); $resources -> active = $n_active; $resources -> id_parent = $n_id_parent; $resources -> id_parent['xlink:href'] = PS_SHOP_PATH . '/api/categories/' . $n_id_parent; $resources -> is_root_category = $n_is_root_category; $node = dom_import_simplexml($resources -> name -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_name)); $resources -> name -> language[0][0] = $n_name; $resources -> name -> language[0][0]['id'] = $n_l_id; $resources -> name -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; // $node = dom_import_simplexml($resources -> description -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_desc)); $resources -> description -> language[0][0] = $n_desc; $resources -> description -> language[0][0]['id'] = $n_l_id; $resources -> description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> link_rewrite -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_link_rewrite)); $resources -> link_rewrite -> language[0][0] = $n_link_rewrite; $resources -> link_rewrite -> language[0][0]['id'] = $n_l_id; $resources -> link_rewrite -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> meta_title -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_meta_title)); $resources -> meta_title -> language[0][0] = $n_meta_title; $resources -> meta_title -> language[0][0]['id'] = $n_l_id; $resources -> meta_title -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $resources -> meta_description -> language[0][0] = $n_meta_description; $resources -> meta_description -> language[0][0]['id'] = $n_l_id; $resources -> meta_description -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $node = dom_import_simplexml($resources -> meta_keywords -> language[0][0]); $no = $node -> ownerDocument; $node -> appendChild($no -> createCDATASection($n_meta_keywords)); $resources -> meta_keywords -> language[0][0] = $n_meta_keywords; $resources -> meta_keywords -> language[0][0]['id'] = $n_l_id; $resources -> meta_keywords -> language[0][0]['xlink:href'] = PS_SHOP_PATH . '/api/languages/' . $n_l_id; $resources->associations = ''; $resources->active = 1; $resources->id_shop_default = 1; $resources->is_root_category = 0; $resources->name->language[0][0] = $cat_name; $xml = $webservice->add( array( 'resource' => 'categories', 'postXml' => $xml->asXML() ) ); $ps_category_id=$xml->children()->children()->{'id'}; $categories[]=$cat_name; $sql = "INSERT INTO "._DB_PREFIX_."import_category(prestashop_id, import_id, import_name, import_reference) VALUES(".$ps_category_id.",'".$impid."','".$cat_name."','".$ref."')"; Db::getInstance()->Execute($sql); } any idea what is going on? edit: i noticed one more thing it gets slower over time, went from 20sec per category to almost minute and half Maybe it's server related. By the way, your script is fantastic, I will save it in case I will need it 1 Link to comment Share on other sites More sharing options...
horejsja Posted January 8, 2017 Author Share Posted January 8, 2017 Hi Daniel! thanks for response!i may be little late, but being little inactive fuc*er i am i came back only when i needed to post another question anyways i think it wasnt server related after all. it had to do something with category tree rebuilding every time i added new category. i cant remember exactly the function that rebuilds the category tree, i dropped development of that module some time ago. but when i commented it out execution time went back to 20 sec.thanks again and i promise to be more productive member of this wonderful community in the future Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now