Totti Posted November 15, 2015 Share Posted November 15, 2015 Hello I would like bees through internal sku recover through a product or its attribute and set the amount in stock.Thank you. Link to comment Share on other sites More sharing options...
NemoPS Posted November 16, 2015 Share Posted November 16, 2015 How do you want to do it, via csv? Custom importer? Link to comment Share on other sites More sharing options...
Tung at RockPOS.com Posted November 17, 2015 Share Posted November 17, 2015 As far as I understand, there is no way to do Link to comment Share on other sites More sharing options...
Totti Posted November 17, 2015 Author Share Posted November 17, 2015 I would use the internal objects of prestashop. Link to comment Share on other sites More sharing options...
razaro Posted November 17, 2015 Share Posted November 17, 2015 Maybe this snippet could help, it is part of module I built but some variables are changed and some code removed. if ($products = Product::searchByName((int)$this->context->language->id, $sku)) { foreach ($products as &$product) { $productObj = new Product((int)$id_product, false, (int)$this->context->language->id); $attributes = $productObj->getProductAttributesIds((int)$id_product, true); if (isset($attributes) && $attributes) { foreach ($attributes as $attribute){ $combinations = $productObj->getAttributeCombinationsById($attribute['id_product_attribute'], $this->context->language->id); foreach ($combinations as $key => $combination) if($sku == $combination['reference']) { StockAvailable::setQuantity((int)$id_product, $attribute['id_product_attribute'], (int) $stock_qty); } } } else { if(isset($productObj) && $productObj){ StockAvailable::setQuantity((int)$id_product, 0,(int) $stock_qty); } } } } 1 Link to comment Share on other sites More sharing options...
Totti Posted November 18, 2015 Author Share Posted November 18, 2015 As far as I understand, there is no way to do Is possible 1 Link to comment Share on other sites More sharing options...
NemoPS Posted November 18, 2015 Share Posted November 18, 2015 Are you using advanced stock management? I'd run a custom query, which depends of course on the item type (if it has combos or not) Link to comment Share on other sites More sharing options...
Totti Posted November 18, 2015 Author Share Posted November 18, 2015 No normal Stock. Link to comment Share on other sites More sharing options...
Totti Posted December 22, 2015 Author Share Posted December 22, 2015 is impossible search by reference ? Link to comment Share on other sites More sharing options...
Totti Posted December 22, 2015 Author Share Posted December 22, 2015 Maybe this snippet could help, it is part of module I built but some variables are changed and some code removed. if ($products = Product::searchByName((int)$this->context->language->id, $sku)) { foreach ($products as &$product) { $productObj = new Product((int)$id_product, false, (int)$this->context->language->id); $attributes = $productObj->getProductAttributesIds((int)$id_product, true); if (isset($attributes) && $attributes) { foreach ($attributes as $attribute){ $combinations = $productObj->getAttributeCombinationsById($attribute['id_product_attribute'], $this->context->language->id); foreach ($combinations as $key => $combination) if($sku == $combination['reference']) { StockAvailable::setQuantity((int)$id_product, $attribute['id_product_attribute'], (int) $stock_qty); } } } else { if(isset($productObj) && $productObj){ StockAvailable::setQuantity((int)$id_product, 0,(int) $stock_qty); } } } } <?php error_reporting(E_ALL | E_STRICT); ini_set("display_errors", '1'); ini_set('display_startup_errors', 1); require_once($_SERVER['DOCUMENT_ROOT']."/config/config.inc.php"); try{ $sku="xxxxx"; $products = Product::searchByName(6, $sku); var_dump($products); }catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } ?> Fatal error. if active debug: Array ( [0] => Array ( [file] => /var/www/vhosts/xxxxxx.com/httpdocs/classes/Product.php [line] => 2814 [function] => displayError [class] => ToolsCore [type] => :: [args] => Array ( ) ) [1] => Array ( [file] => /var/www/vhosts/xxxxxx.com/httpdocs/classes/Product.php [line] => 3794 [function] => getPriceStatic [class] => ProductCore [type] => :: [args] => Array ( [0] => 1685 [1] => 1 [2] => [3] => 2 ) ) [2] => Array ( [file] => /var/www/vhosts/xxxxx.com/httpdocs/xxxx/xx_xxx_xx.php [line] => 12 [function] => searchByName [class] => ProductCore [type] => :: [args] => Array ( [0] => 6 [1] => xxxx ) ) ) Link to comment Share on other sites More sharing options...
Totti Posted December 28, 2015 Author Share Posted December 28, 2015 help me ? Link to comment Share on other sites More sharing options...
razaro Posted December 29, 2015 Share Posted December 29, 2015 Ok, if you do not need prices and you just wanted stock create this override of product class override/classes/Product.php <?php class Product extends ProductCore { public static function searchByNameMod($id_lang, $query, Context $context = null) { if (!$context) { $context = Context::getContext(); } $sql = new DbQuery(); $sql->select('p.`id_product`, pl.`name`, p.`ean13`, p.`upc`, p.`active`, p.`reference`, m.`name` AS manufacturer_name, stock.`quantity`, product_shop.advanced_stock_management, p.`customizable`'); $sql->from('product', 'p'); $sql->join(Shop::addSqlAssociation('product', 'p')); $sql->leftJoin('product_lang', 'pl', ' p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl') ); $sql->leftJoin('manufacturer', 'm', 'm.`id_manufacturer` = p.`id_manufacturer`'); $where = 'pl.`name` LIKE \'%'.pSQL($query).'%\' OR p.`ean13` LIKE \'%'.pSQL($query).'%\' OR p.`upc` LIKE \'%'.pSQL($query).'%\' OR p.`reference` LIKE \'%'.pSQL($query).'%\' OR p.`supplier_reference` LIKE \'%'.pSQL($query).'%\' OR EXISTS(SELECT * FROM `'._DB_PREFIX_.'product_supplier` sp WHERE sp.`id_product` = p.`id_product` AND `product_supplier_reference` LIKE \'%'.pSQL($query).'%\')'; $sql->orderBy('pl.`name` ASC'); if (Combination::isFeatureActive()) { $where .= ' OR EXISTS(SELECT * FROM `'._DB_PREFIX_.'product_attribute` `pa` WHERE pa.`id_product` = p.`id_product` AND (pa.`reference` LIKE \'%'.pSQL($query).'%\' OR pa.`supplier_reference` LIKE \'%'.pSQL($query).'%\' OR pa.`ean13` LIKE \'%'.pSQL($query).'%\' OR pa.`upc` LIKE \'%'.pSQL($query).'%\'))'; } $sql->where($where); $sql->join(Product::sqlStock('p', 0)); $result = Db::getInstance()->executeS($sql); if (!$result) { return false; } $results_array = array(); foreach ($result as $row) { //$row['price_tax_incl'] = Product::getPriceStatic($row['id_product'], true, null, 2); //$row['price_tax_excl'] = Product::getPriceStatic($row['id_product'], false, null, 2); $results_array[] = $row; } return $results_array; } } Then use it in your code <?php error_reporting(E_ALL | E_STRICT); ini_set("display_errors", '1'); ini_set('display_startup_errors', 1); require_once($_SERVER['DOCUMENT_ROOT']."/config/config.inc.php"); try{ $sku="xxxxx"; $products = Product::searchByNameMod(6, $sku); var_dump($products); }catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } ?> Clear cache/delete file cache/class_index.php. 1 Link to comment Share on other sites More sharing options...
Totti Posted February 2, 2016 Author Share Posted February 2, 2016 If I enable a module I do not receive this error only: [0] => Array ( [file] => /var/www/vhosts/xxxxx.com/httpdocs/classes/Product.php [line] => 2814 [function] => displayError [class] => ToolsCore [type] => :: [args] => Array ( ) ) [1] => Array ( [file] => /var/www/vhosts/xxxxxx.com/httpdocs/classes/Product.php [line] => 499 [function] => getPriceStatic [class] => ProductCore [type] => :: [args] => Array ( [0] => 1590 [1] => [2] => [3] => 6 [4] => [5] => [6] => 1 [7] => 1 [8] => [9] => [10] => [11] => [12] => 0 ) ) Link to comment Share on other sites More sharing options...
NemoPS Posted February 3, 2016 Share Posted February 3, 2016 What module exactly? Any? It would seem there is an error in the getPriceStatic method of the cart class Link to comment Share on other sites More sharing options...
razaro Posted February 3, 2016 Share Posted February 3, 2016 Did you commented out two lines in override of Product.php ? Link to comment Share on other sites More sharing options...
Totti Posted February 4, 2016 Author Share Posted February 4, 2016 After losing a lot of time I found the solution.I had to import the file in my script init.php. 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