a2dgroup Posted May 5, 2014 Share Posted May 5, 2014 Hi, I'm trying to create a module in prestashop for insert products in my shop by programmatically. Now, I have a button on my module which call the following function: private function _saveContent() { $message = ''; $defaultLanguage = new Language((int)(Configuration::get('PS_LANG_DEFAULT'))); $object = new Product(); $object->price = 22; $object->id_tax_rules_group = 0; $object->name = 'prova'; $object->id_manufacturer = 0; $object->id_supplier = 0; $object->quantity = 1; $object->minimal_quantity = 1; $object->additional_shipping_cost = 0; $object->wholesale_price = 0; $object->ecotax = 0; $object->width = 0; $object->height = 0; $object->depth = 0; $object->weight = 0; $object->out_of_stock = 0; $object->active = 1; $object->id_category_default = 22; $object->category=array(22); $object->available_for_order = 0; $object->show_price = 1; $object->on_sale = 0; $object->online_only = 1; $object->meta_keywords = 'test'; $object->description_short = array((int)(Configuration::get('PS_LANG_DEFAULT')) => 'RRR'); $object->link_rewrite = array((int)(Configuration::get('PS_LANG_DEFAULT')) => 'RRR'); $object->add(); $message = $this->displayConfirmation($this->l('I prodotti sono stati aggiornati correttamente')); return $message; } The function insert the product in the database table PREFIX_product; I also see the product in the list on backoffice, but after that the page reply with these errors: [PrestaShopException] Property Product->link_rewrite is empty at line 878 in file classes/ObjectModel.php 872. 873. $message = $this->validateField($field, $value, $id_lang); 874. if ($message !== true) 875. { 876. if ($die) 877. throw new PrestaShopException($message); 878. return $error_return ? $message : false; 879. } 880. } 881. } 882. ObjectModelCore->validateFieldsLang - [line 744 - classes/Product.php] - [2 Arguments] 738. $limit = (int)Configuration::get('PS_PRODUCT_SHORT_DESC_LIMIT'); 739. if ($limit <= 0) 740. $limit = 800; 741. $this->def['fields']['description_short']['size'] = $limit; 742. 743. return parent::validateFieldsLang($die, $error_return); 744. } 745. 746. /** 747. * @see ObjectModel::validateField() 748. */ ProductCore->validateFieldsLang - [line 296 - classes/ObjectModel.php] - [0 Argument] 290. { 291. // Retrocompatibility 292. if (method_exists($this, 'getTranslationsFieldsChild')) 293. return $this->getTranslationsFieldsChild(); 294. 295. $this->validateFieldsLang(); 296. $is_lang_multishop = $this->isLangMultishop(); 297. 298. $fields = array(); 299. if ($this->id_lang === null) 300. foreach (Language::getLanguages(false) as $language) ObjectModelCore->getFieldsLang - [line 480 - classes/ObjectModel.php] - [0 Argument] 474. return false; 475. 476. // Database insertion for multilingual fields related to the object 477. if (!empty($this->def['multilang'])) 478. { 479. $fields = $this->getFieldsLang(); 480. if ($fields && is_array($fields)) 481. { 482. $shops = Shop::getCompleteListOfShopsID(); 483. $asso = Shop::getAssoTable($this->def['table'].'_lang'); 484. foreach ($fields as $field) ObjectModelCore->add - [line 499 - classes/Product.php] - [2 Arguments] 493. return $fields; 494. } 495. 496. public function add($autodate = true, $null_values = false) 497. { 498. if (!parent::add($autodate, $null_values)) 499. return false; 500. 501. if ($this->getType() == Product::PTYPE_VIRTUAL) 502. StockAvailable::setProductOutOfStock((int)$this->id, 1); 503. else ProductCore->add - [line 166 - modules/skeleton/skeleton.php] - [0 Argument] 160. $object->on_sale = 0; 161. $object->online_only = 1; 162. $object->meta_keywords = 'test'; 163. $object->description_short = array((int)(Configuration::get('PS_LANG_DEFAULT')) => ''); 164. $object->link_rewrite = array((int)(Configuration::get('PS_LANG_DEFAULT')) => ''); 165. $object->add(); 166. 167. $message = $this->displayConfirmation($this->l('I prodotti sono stati aggiornati correttamente')); 168. return $message; 169. } 170. Skeleton->_saveContent - [line 95 - modules/skeleton/skeleton.php] - [0 Argument] 89. public function getContent() 90. { 91. $message = ''; 92. 93. if (Tools::isSubmit('submit_'.$this->name)) 94. $message = $this->_saveContent(); 95. 96. $this->_displayContent($message); 97. 98. return $this->display(__FILE__, 'settings.tpl'); 99. } Skeleton->getContent - [line 685 - controllers/admin/AdminModulesController.php] - [0 Argument] 679. // We check if method of module exists 680. if (!method_exists($module, $method)) 681. throw new PrestaShopException('Method of module can\'t be found'); 682. 683. // Get the return value of current method 684. $echo = $module->{$method}(); 685. 686. // After a successful install of a single module that has a configuration method, to the configuration page 687. if ($key == 'install' && $echo === true && strpos(Tools::getValue('install'), '|') === false && method_exists($module, 'getContent')) 688. Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token.'&configure='.$module->name.'&conf=12'); 689. } AdminModulesControllerCore->postProcessCallback - [line 811 - controllers/admin/AdminModulesController.php] - [0 Argument] 805. $ppmReturn = $this->$ppm(); 806. } 807. 808. // Call appropriate module callback 809. if (!isset($ppmReturn)) 810. $this->postProcessCallback(); 811. 812. if ($back = Tools::getValue('back')) 813. Tools::redirectAdmin($back); 814. } 815. AdminModulesControllerCore->postProcess - [line 158 - classes/controller/Controller.php] - [0 Argument] 152. // setMedia MUST be called before postProcess 153. if (!$this->content_only && ($this->display_header || (isset($this->className) && $this->className))) 154. $this->setMedia(); 155. 156. // postProcess handles ajaxProcess 157. $this->postProcess(); 158. 159. if (!empty($this->redirect_after)) 160. $this->redirect(); 161. 162. if (!$this->content_only && ($this->display_header || (isset($this->className) && $this->className))) ControllerCore->run - [line 348 - classes/Dispatcher.php] - [0 Argument] 342. // Execute hook dispatcher 343. if (isset($params_hook_action_dispatcher)) 344. Hook::exec('actionDispatcher', $params_hook_action_dispatcher); 345. 346. // Running controller 347. $controller->run(); 348. } 349. catch (PrestaShopException $e) 350. { 351. $e->displayMessage(); 352. } DispatcherCore->dispatch - [line 53 - admin9746/index.php] - [0 Argument] 47. $_POST['controller'] = strtolower($_POST['tab']); 48. if (!isset($_REQUEST['controller']) && isset($_REQUEST['tab'])) 49. $_REQUEST['controller'] = strtolower($_REQUEST['tab']); 50. 51. // Prepare and trigger admin dispatcher 52. Dispatcher::getInstance()->dispatch(); I think the problems are linked to multilanguage support, but I don't know what to do. Please help me Link to comment Share on other sites More sharing options...
davidgortega Posted September 25, 2014 Share Posted September 25, 2014 (edited) Hi, I had the same error. I had to follow the code a bit to discover it. $object = new Product(); $object->price = 1; $object->multilang = true; $languages = Language::getLanguages(); foreach ($languages as $language) { $object->name[ $language['id_lang'] ] = 'test'; $object->meta_keywords[ $language['id_lang'] ] = 'test'; $object->description[ $language['id_lang'] ] = 'test'; $object->description_short[ $language['id_lang'] ] = 'test'; } $object->modifierWsLinkRewrite(); if( $object->save() ) $object->add(); Note modifierWsLinkRewrite() call. Edited September 26, 2014 by davidgortega (see edit history) Link to comment Share on other sites More sharing options...
LucianoNapoli Posted September 30, 2014 Share Posted September 30, 2014 The problem is that you have two languages defined. One language, let's say english with id 1 is deactivated and the other language with id 2 is active and is default language. The correct assignment for the name property is: $object->name[Configuration::get('PS_LANG_DEFAULT')] 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