jval Posted July 15, 2014 Share Posted July 15, 2014 Hi, I need create product via php. My code $product = new Product(); $product->ean13 = 9999999999999; $product->name = 'New product'; $product->link_rewrite = 'new-product'; $product->id_category = 64; $product->id_category_default = 64; $product->add(); not working. It is possible create product with class Product? Link to comment Share on other sites More sharing options...
Ittu Posted July 15, 2014 Share Posted July 15, 2014 Have you included prestashop files $site_base_path = "DIRECTORY_PATH"; include($site_base_path . 'config/config.inc.php'); require_once($site_base_path . 'init.php'); Link to comment Share on other sites More sharing options...
vekia Posted July 15, 2014 Share Posted July 15, 2014 Hi, I need create product via php. My code $product = new Product(); $product->ean13 = 9999999999999; $product->name = 'New product'; $product->link_rewrite = 'new-product'; $product->id_category = 64; $product->id_category_default = 64; $product->add(); not working. It is possible create product with class Product? this is whole code? nothing more? no libraries included to this php file? Link to comment Share on other sites More sharing options...
jval Posted July 15, 2014 Author Share Posted July 15, 2014 (edited) Full code is: <?php $import = 'eshop.csv'; require(dirname(__FILE__) . '/../config/config.inc.php'); $connect = mysql_connect(_DB_SERVER_, _DB_USER_, _DB_PASSWD_) or die('nelze se pripojit'); mysql_select_db(_DB_NAME_, $connect); mysql_query("Set names 'utf8'"); $ids = array(); $query = mysql_query('SELECT ean13,id_product FROM ' . _DB_PREFIX_ . 'product'); while ($row = mysql_fetch_array($query)) { $ids[(string) $row['ean13']] = $row['id_product']; } $x = 0; if (file_exists($import)) { $file = file_get_contents($import); $lines = explode(PHP_EOL, $file); foreach ($lines as $line) { $data = explode(';', $line); $ean = (string) $data[0]; $price = (float) $data[1]; $quantity = (int) $data[2]; $tax = (int) $data[3]; if ($tax == 0 or $tax == '') { $taxid = 0; } else { $taxid = ($tax == 21 ? 1 : 2); } if ($quantity < 0) { $quantity = 0; } if (array_key_exists($ean, $ids)) { $id = (int) $ids[$ean]; $product = new Product($id); $product->price = (float) number_format($price / (1 + $tax / 100), 6, '.', ''); $sql = 'UPDATE `' . _DB_PREFIX_ . 'product` SET price = "' . $product->price . '",id_tax_rules_group = ' . $taxid . ' WHERE id_product="' . $id . '"'; mysql_query($sql); $sql = 'UPDATE `' . _DB_PREFIX_ . 'product_shop` SET price = "' . $product->price . '",id_tax_rules_group = ' . $taxid . ' WHERE id_product="' . $id . '"'; mysql_query($sql); $sql = 'UPDATE `' . _DB_PREFIX_ . 'stock_available` SET quantity = "' . $quantity . '" WHERE id_product="' . $id . '"'; mysql_query($sql); $x++; } else { $product = new Product(); $product->ean13 = 9999999999999; $product->name = 'Test importu'; $product->link_rewrite = 'test-importu'; $product->id_category = 64; $product->id_category_default = 64; $product->redirect_type = '404'; $product->add(true, true); } } mysql_query('UPDATE `' . _DB_PREFIX_ . 'product_shop` SET active=1'); mysql_query('UPDATE `' . _DB_PREFIX_ . 'product` SET active=1'); mysql_query('UPDATE `' . _DB_PREFIX_ . 'product_shop` SET active=0 WHERE id_product IN (SELECT id_product FROM `' . _DB_PREFIX_ . 'stock_available` WHERE quantity<=0) OR id_product IN (SELECT id_product FROM `' . _DB_PREFIX_ . 'product` WHERE price<=0) OR id_category_default IN (64,66)'); mysql_query('UPDATE `' . _DB_PREFIX_ . 'product` SET active=0 WHERE id_product IN (SELECT id_product FROM `' . _DB_PREFIX_ . 'stock_available` WHERE quantity<=0) OR price<=0 OR id_category_default IN (64,66)'); //unlink($import); } ?> Running daily, update prices and quantity. I need if product is not in eshop, then create new. Sorry for my bad English. Edited July 15, 2014 by jval (see edit history) Link to comment Share on other sites More sharing options...
vekia Posted July 15, 2014 Share Posted July 15, 2014 it spawns some errors? are you able to verify it, please? Link to comment Share on other sites More sharing options...
jval Posted July 15, 2014 Author Share Posted July 15, 2014 (edited) This code: <?php $site_base_path = "../"; include($site_base_path . 'config/config.inc.php'); require_once($site_base_path . 'init.php'); $product = new Product(); $product->ean13 = 9999999999999; $product->name = 'Test importu'; $product->link_rewrite = 'test-importu'; $product->id_category = 6; $product->id_category_default = 6; $product->add(); ?> generated this error: Fatal error: Uncaught exception 'PrestaShopException' with message 'Property Product->link_rewrite is empty' in D:\xampp\htdocs\_tests\prestashop\classes\ObjectModel.php:878 Stack trace: #0 D:\xampp\htdocs\_tests\prestashop\classes\Product.php(744): ObjectModelCore->validateFieldsLang(true, false) #1 D:\xampp\htdocs\_tests\prestashop\classes\ObjectModel.php(296): ProductCore->validateFieldsLang() #2 D:\xampp\htdocs\_tests\prestashop\classes\ObjectModel.php(480): ObjectModelCore->getFieldsLang() #3 D:\xampp\htdocs\_tests\prestashop\classes\Product.php(499): ObjectModelCore->add(true, false) #4 D:\xampp\htdocs\_tests\prestashop\jv_cron\cron.php(14): ProductCore->add() #5 {main} thrown in D:\xampp\htdocs\_tests\prestashop\classes\ObjectModel.php on line 878 Prestashop version is 1.5.6.1 Edited July 15, 2014 by jval (see edit history) Link to comment Share on other sites More sharing options...
jval Posted July 15, 2014 Author Share Posted July 15, 2014 (edited) OK, founded solution: This code: <?php $site_base_path = "../"; include($site_base_path . 'config/config.inc.php'); require_once($site_base_path . 'init.php'); $product = new Product(); $product->ean13 = 9999999999999; $product->name = array((int)Configuration::get('PS_LANG_DEFAULT') => 'Test importu');; $product->link_rewrite = array((int)Configuration::get('PS_LANG_DEFAULT') => 'test-importu'); $product->id_category = 6; $product->id_category_default = 6; $product->redirect_type = '404'; $product->add(); $product->addToCategories(array(6)); ?> working for me. Thank you Edited July 15, 2014 by jval (see edit history) Link to comment Share on other sites More sharing options...
vekia Posted July 16, 2014 Share Posted July 16, 2014 there were just missing fields thank you for posting solution! topic marked as solved, Milos Link to comment Share on other sites More sharing options...
laurenamick Posted July 16, 2014 Share Posted July 16, 2014 Nice site, I just dig this keep up the good work!. Link to comment Share on other sites More sharing options...
GOKULAN R Posted October 6, 2014 Share Posted October 6, 2014 (edited) how to created download product (software product) by php code ? example (This is not working) $download = new ProductDownload();$download->id_product = (int)$product = $productid; $download->display_filename = $filename; $download->filename = $filename; $download->nb_days_accessible = 60; $download->nb_downloadable = 5; $download->active = 1; //$download->is_shareable = (int)$is_shareable; $download->save(); Edited October 6, 2014 by gokulan (see edit history) Link to comment Share on other sites More sharing options...
Totti Posted October 6, 2014 Share Posted October 6, 2014 What are the classes used for the import of csv in PS ? Link to comment Share on other sites More sharing options...
Recommended Posts