I have csv file with following structure. I want to update product category path from this file. How I can do this.
sku,category_ids
0001,"1,2,3"
0002,"1,2,4"
I using flowing script to update prices
$mageFilename = '../app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('admin');
Mage::register('isSecureArea', 1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
set_time_limit(0);
ini_set('memory_limit','1024M');
/***************** UTILITY FUNCTIONS ********************/
function _getConnection($type = 'core_read'){
return Mage::getSingleton('core/resource')->getConnection($type);
}
function _getTableName($tableName){
return Mage::getSingleton('core/resource')->getTableName($tableName);
}
function _getAttributeId($attribute_code = 'category_ids'){
$connection = _getConnection('core_read');
$sql = "SELECT attribute_id
FROM " . _getTableName('eav_attribute') . "
WHERE
entity_type_id = ?
AND attribute_code = ?";
$entity_type_id = _getEntityTypeId();
return $connection->fetchOne($sql, array($entity_type_id, $attribute_code));
}
function _getEntityTypeId($entity_type_code = 'catalog_product'){
$connection = _getConnection('core_read');
$sql = "SELECT entity_type_id FROM " . _getTableName('eav_entity_type') . " WHERE entity_type_code = ?";
return $connection->fetchOne($sql, array($entity_type_code));
}
function _getIdFromSku($sku){
$connection = _getConnection('core_read');
$sql = "SELECT entity_id FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
return $connection->fetchOne($sql, array($sku));
}
function _checkIfSkuExists($sku){
$connection = _getConnection('core_read');
$sql = "SELECT COUNT(*) AS count_no FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
$count = $connection->fetchOne($sql, array($sku));
if($count > 0){
return true;
}else{
return false;
}
}
function _updatePrices($data){
$connection = _getConnection('core_write');
$sku = $data[0];
$newPrice = $data[1];
$productId = _getIdFromSku($sku);
$attributeId = _getAttributeId();
$sql = "UPDATE " . _getTableName('catalog_product_entity_decimal') . " cped
SET cped.value = ?
WHERE cped.attribute_id = ?
AND cped.entity_id = ?";
$connection->query($sql, array($newPrice, $attributeId, $productId));
}
/***************** UTILITY FUNCTIONS ********************/
$csv = new Varien_File_Csv();
$data = $csv->getData('prices.csv'); //path to csv
array_shift($data);
$message = '';
$count = 1;
foreach($data as $_data){
if(_checkIfSkuExists($_data[0])){
try{
_updatePrices($_data);
$message .= $count . '> Success:: While Updating Price (' . $_data[1] . ') of Sku (' . $_data[0] . '). <br />';
}catch(Exception $e){
$message .= $count .'> Error:: While Upating Price (' . $_data[1] . ') of Sku (' . $_data[0] . ') => '.$e->getMessage().'<br />';
}
}else{
$message .= $count .'> Error:: Product with Sku (' . $_data[0] . ') does\'t exist.<br />';
}
$count++;
}
echo $message;
I don't know which table to be updated. What needs to change in this code to work update category_ids.
Unless you have a specific reason for using a custom script, you can always use the normal System -> Import/Export -> Dataflow: Profiles -> Import All Products using the file you described.
If you are truly looking for a script I can amend my answer for you.
Related
Can anybody help me to understand why my query update dosen't update my data in my database.
This my code php :
<?php
$code = $_GET['code'];
$n1= $_GET['n1'];
$n2= $_GET['n2'];
$n3 = $_GET['n3'];
try {
$connexion= new PDO('mysql:host=localhost;dbname=data','mydata','password');
$sql_update = "UPDATE data.check SET numb_1='".$n1."',numb_2='".$n2."','numb_3'='".n3."' WHERE 'code_product' =".$code;
$query = $connexion-> prepare($sql_update);
$query -> execute();
$data_update= $query -> fetchAll(PDO::FETCH_ASSOC);
}
catch(PDOException $e)
{
echo "<br>" . $e->getMessage();
}
Thanks for any help.
1) Change
$sql_update = "UPDATE data.check SET numb_1='" . $n1 . "',numb_2='" . $n2 . "','numb_3'='" . n3 . "' WHERE 'code_product' =" . $code;
To
$sql_update = "UPDATE data.check SET numb_1='" . $n1 . "',numb_2='" . $n2 . "','numb_3'='" . $n3 . "' WHERE `code_product` =" . $code;
=> In n3 you forgot to add $. And, replace single quotes with backtick to enclose column name.
Updated Code
<?php
$code = $_GET['code'];
$n1 = $_GET['n1'];
$n2 = $_GET['n2'];
$n3 = $_GET['n3'];
try {
$connexion = new PDO('mysql:host=localhost;dbname=data', 'mydata', 'password');
$sql_update = $connexion->prepare("UPDATE `data`.`check` SET numb_1 = :numb_1 , numb_2 = :numb_2, numb_3 = :numb_3 WHERE `code_product` = :code_product");
$sql_update->execute(array(':numb_1' => $n1,':numb_2'=>$n2, ':numb_3'=>$n3,':code_product'=>$code));
$stmt = $connexion->prepare("SELECT * FROM `data`.`check` WHERE code_product=:code_product");
$stmt->execute(array(':code_product'=>$code));
$data_update= $stmt -> fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo "<br>" . $e->getMessage();
}
?>
After your update execution you need to query again for fetching result like,
$sql_update = "UPDATE data.check SET numb_1='".$n1."',numb_2='".$n2."','numb_3'='".$n3."' WHERE 'code_product' =".$code;
$query = $connexion-> prepare($sql_update);
$query -> execute();
$query = $dbh->prepare("SELECT * FROM data.check");
$query->execute();
$data_update= $query -> fetchAll(PDO::FETCH_ASSOC);// now it will get records
I am new to joomla. I am using RSDirectory Component. In this i have to customize validation. i have a file named rsdirectory.php which is located at: administrator>components>com_resdirectory>helpers>rsdirectory.php.
I have a table, table1. In which a unique code is stored. Now i want to fill a form using that code if exist then query will execute otherwise my validation code will be execute. i have done this successfully. Now i have an another table, table2 in which my data is storing when i am filling a form with unique code. i just want to check my unique code whether it is exist in table2 or not. if exist validation will execute.i want to use same function for both.
Here is my code. Thanks in advance.
public static function uniquestacksfield($value, $column,$column1,$id1=null, $id = null){
// Get DBO.
$column = 'uni_code';
$db = JFactory::getDBO();
$query = $db->getQuery(true)
->select($db->qn('id'))
->from($db->qn('#_table1', 'e'))
->where($db->qn($column) . ' = ' . $db->q($value));
$db->setQuery($query);
$entry = $db->loadObject();
if ($id) {
$query->where($db->qn('e.id') . ' = ' . $db->q($id));
}
$db->setQuery($query, 0, 1);
return $db->loadResult();
/------------------another query-----------------------/
$column1 = 'uni_code2';
$db1 = JFactory::getDBO();
$query1 = $db1->getQuery(true)
->select($db1->qn('entry_id'))
->from($db1->qn('#_table2', 'c'))
->where($db1->qn($column1) . ' = ' . $db1->q($value));
$db1->setQuery($query1);
$entry1 = $db1->loadObject();
if ($id1) {
$query1->where($db1->qn('c_entry_id') . ' = ' . $db1->q($id1));
}
$db1->setQuery($query1, 0, 1);
return $db1->loadResult();
}`
I think below code is may be use.you can try it now. it is not good explain more to your criteria
public static function uniquestacksfield($value, $column,$column1,$id1=null, $id = null){
// Get DBO.
$db = JFactory::getDBO();
$column = 'uni_code';
$column1 = 'uni_code2';
if($column == 'uni_code') {
$query = $db->getQuery(true)
->select($db->qn('id'))
->from($db->qn('#_table1', 'e'))
->where($db->qn($column) . ' = ' . $db->q($value));
$db->setQuery($query);
$entry = $db->loadObject();
if ($id) {
$query->where($db->qn('e.id') . ' = ' . $db->q($id));
}
} else {
$query = $db->getQuery(true)
->select($db->qn('entry_id'))
->from($db->qn('#_table2', 'c'))
->where($db->qn($column1) . ' = ' . $db->q($value));
$db->setQuery($query);
$entry = $db1->loadObject();
if ($id) {
$query->where($db->qn('c_entry_id') . ' = ' . $db->q($id1));
}
}
$db->setQuery($query, 0, 1);
$item = $db->loadResult();
return $item;
}
I have this script, it works, i can make an update for the quantities . The only problem i have is the fact that the csv is too big...
I tried to set the memory limit but when i'm loading the page the server says "The waiting time is exceeded. The server at www.auto-univers.fr takes too long to respond. "
What can i do ?
is that it is possible to update the line from 1 to 1000 for example? I do like his two update in 2 different pages, line 1 to 1000 and 1000 to the end line.
<?php
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
umask(0);
Mage::app('admin');
Mage::register('isSecureArea', 1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
set_time_limit(0);
ini_set('memory_limit','2048M');
function _getConnection($type = 'core_read'){
return Mage::getSingleton('core/resource')->getConnection($type);
}
function _getTableName($tableName){
return Mage::getSingleton('core/resource')->getTableName($tableName);
}
function _getAttributeId($attribute_code = 'price'){
$connection = _getConnection('core_read');
$sql = "SELECT attribute_id
FROM " . _getTableName('eav_attribute') . "
WHERE
entity_type_id = ?
AND attribute_code = ?";
$entity_type_id = _getEntityTypeId();
return $connection->fetchOne($sql, array($entity_type_id, $attribute_code));
}
function _getEntityTypeId($entity_type_code = 'catalog_product'){
$connection = _getConnection('core_read');
$sql = "SELECT entity_type_id FROM " . _getTableName('eav_entity_type') . " WHERE entity_type_code = ?";
return $connection->fetchOne($sql, array($entity_type_code));
}
function _checkIfSkuExists($sku){
$connection = _getConnection('core_read');
$sql = "SELECT COUNT(*) AS count_no FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
$count = $connection->fetchOne($sql, array($sku));
if($count > 0){
return true;
}else{
return false;
}
}
function _getIdFromSku($sku){
$connection = _getConnection('core_read');
$sql = "SELECT entity_id FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
return $connection->fetchOne($sql, array($sku));
}
function _updateStocks($data){
$connection = _getConnection('core_write');
$sku = $data[0];
$newQty = $data[1];
$productId = _getIdFromSku($sku);
$attributeId = _getAttributeId();
$sql = "UPDATE " . _getTableName('cataloginventory_stock_item') . " csi,
" . _getTableName('cataloginventory_stock_status') . " css
SET
csi.qty = ?,
csi.is_in_stock = ?,
css.qty = ?,
css.stock_status = ?
WHERE
csi.product_id = ?
AND csi.product_id = css.product_id";
$isInStock = $newQty > 0 ? 1 : 0;
$stockStatus = $newQty > 0 ? 1 : 0;
$connection->query($sql, array($newQty, $isInStock, $newQty, $stockStatus, $productId));
}
$csv = new Varien_File_Csv();
$csv->setDelimiter(';');
$data = $csv->getData('supply.csv'); //path to csv
array_shift($data);
$message = '';
$count = 1;
foreach($data as $_data){
if(_checkIfSkuExists($_data[0])){
try{
_updateStocks($_data);
$message .= $count . '> Success:: Quantité (' . $_data[1] . ') de la référence (' . $_data[0] . ') a été mis à jour. <br />';
}catch(Exception $e){
$message .= $count .'> Erreur:: Lors de l\'update de la quantité (' . $_data[1] . ') de la référence (' . $_data[0] . ') => '.$e->getMessage().'<br />';
}
}else{
$message .= $count .'> Erreur:: Produit avec la référence (' . $_data[0] . ') n\'existe pas.<br />';
}
$count++;
}
echo $message;
I'm skeptical this script would exceed max execution time, but to answer your question, you could always keep track of how many lines have been processed and then stop execution after 10000. You could also use an offset and limit passed in from the $_GET parameters to make it more controllable and dynamic.
you can either split the csv or use array_slice method to split the results from csv.
http://www.php.net/manual/en/function.array-slice.php
need to update price list in magento if a condition is true, if it is false skip.
The issue is that don't know why the function _checkIfSkuInStock is returning 1 for all.
I set one product to be in stock and one to be out of stock.
Below is the code that is updating the price list if the sku exist, and i want to add also the the condition if product is in stock, else skip.
function _getConnection($type = 'core_read'){
return Mage::getSingleton('core/resource')->getConnection($type);
}
function _getTableName($tableName){
return Mage::getSingleton('core/resource')->getTableName($tableName);
}
function _getAttributeId($attribute_code = 'special_price'){
$connection = _getConnection('core_read');
$sql = "SELECT attribute_id
FROM " . _getTableName('eav_attribute') . "
WHERE
entity_type_id = ?
AND attribute_code = ?";
$entity_type_id = _getEntityTypeId();
return $connection->fetchOne($sql, array($entity_type_id, $attribute_code));
}
function _getEntityTypeId($entity_type_code = 'catalog_product'){
$connection = _getConnection('core_read');
$sql = "SELECT entity_type_id FROM " . _getTableName('eav_entity_type') . " WHERE entity_type_code = ?";
return $connection->fetchOne($sql, array($entity_type_code));
}
function _getIdFromSku($sku){
$connection = _getConnection('core_read');
$sql = "SELECT entity_id FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
return $connection->fetchOne($sql, array($sku));
}
function _checkIfSkuExists($sku){
$connection = _getConnection('core_read');
$sql = "SELECT COUNT(*) AS count_no FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
$count = $connection->fetchOne($sql, array($sku));
if($count > 0){
return true;
}else{
return false;
}
}
function _checkIfSkuInStock($sku){
$connection = _getConnection('core_read');
$sql = "SELECT COUNT(*) AS count_no FROM " . _getTableName('cataloginventory_stock_item') . " WHERE is_in_stock = 1";
$count = $connection->fetchOne($sql, array($sku));
if($count > 0){
return true;
}else{
return false;
}
}
function _updatePrices($data){
$connection = _getConnection('core_write');
$sku = $data[0];
$newspecial_price = $data[1];
$productId = _getIdFromSku($sku);
$attributeId = _getAttributeId();
$sql = "UPDATE " . _getTableName('catalog_product_entity_decimal') . " cped
SET cped.value = ?
WHERE cped.attribute_id = ?
AND cped.entity_id = ?";
$connection->query($sql, array($newspecial_price, $attributeId, $productId));
}
$csv = new Varien_File_Csv();
$data = $csv->getData('special_price.csv'); //path to csv
array_shift($data);
$message = '';
$count = 1;
foreach($data as $_data){
if(_checkIfSkuExists($_data[0]) and _checkIfSkuInStock($_data[0]) == 0){
try{
_updatePrices($_data);
$message .= $count . '> Success:: (' . $_data[1] . ') product code (' . $_data[0] . '). <br />';
}catch(Exception $e){
$message .= $count .'> Error:: While Upating Price (' . $_data[1] . ') of Sku (' . $_data[0] . ') => '.$e->getMessage().'<br />';
}
}else{
$message .= $count .'> Error:: Product code (' . $_data[0] . ') not in database<br />';
}
$count++;
}
echo $message;
Any help is appreciated.
In this code :
function _checkIfSkuInStock($sku){
$connection = _getConnection('core_read');
$sql = "SELECT COUNT(*) AS count_no FROM " . _getTableName('cataloginventory_stock_item') . " WHERE is_in_stock = 1";
$count = $connection->fetchOne($sql, array($sku));
if($count > 0){
return true;
}else{
return false;
}
}
You try to populate with sku : $connection->fetchOne($sql, array($sku));
But you don't have any prepared field (?) in your request.
Add something like this :
"SELECT COUNT(*) AS count_no FROM " . _getTableName('cataloginventory_stock_item') . " WHERE is_in_stock = 1 AND product_id=?"
And then use
$connection->fetchOne($sql, array($this->_getIdFromSku($sku)));
It should work better ;)
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Check if value exist in mysql
I have a small script to upload data to mysql database from a csv file and I want to check the list of values that are inside of the csv file.
This is the CSV File to check if values from csv exist in db:
code,alert_quantity
12345,10
This my proeject PHP File:
<?php
$link_id = mysql_connect("localhost", "root", "")
or die("Could not connect.");
if(!mysql_select_db("database",$link_id))
die("database was not selected.");
function _checkIfCodeExists($code){
$sql = "SELECT COUNT(*) AS count_no FROM products WHERE code = ?";
$count = $sql;
if($count > 0){
return true;
}else{
return false;
}
}
function _updateData($line_of_data){
$code = $line_of_data[0];
$newAlert = $line_of_data[1];
$sql = "UPDATE ";
}
$file_handle = fopen("file.csv", "r");
while (($line_of_data = fgetcsv($file_handle, 1000, ",")) !== FALSE) {
$query = mysql_query("SELECT * FROM products WHERE code ='$line_of_data[0]'") or die(mysql_error());
$message = '';
$count = 1;
if(_checkIfCodeExists($line_of_data[0])){
try{
_updateData($_data);
$message .= $count . '> Success:: Product with code (' . $line_of_data[1] . ') Exist (' . $line_of_data[0] . '). <br />';
}catch(Exception $e){
$message .= $count .'> Error:: While updating alert (' . $line_of_data[1] . ') of code (' . $line_of_data[0] . ') => '.$e->getMessage().'<br />';
}
}else{
$message .= $count .'> Error:: Product code (' . $line_of_data[0] . ') Doesnt exist<br />';
}
$count++;
echo $message;
}
?>
I don't know what you're doing here, but it isn't going to work:
$sql = "SELECT COUNT(*) AS count_no FROM products WHERE code = ?";
$count = $sql;
if($count > 0){
return true;
This is equivalent to:
if ("SELECT COUNT(*)..." > 0)
That will never be true.