你好我想创建模块将产品数据从xml文件导入数据库xml文件我从ftp服务器下载它并读取并导入产品,如果产品存在相同的物品,我会对产品进行更新有相同的参考。 请提出一些想法 我在一些论坛中找到了这段代码
require_once('PSWebServiceLibrary.php');
$webService = new PrestaShopWebservice(Configuration::get('DKSYNCHRONE_LINK'), Configuration::get('DKSYNCHRONE_KEY'), false);
//try{
$product = $webService->get(array('resource' => 'products', 'id' => (int) $id_product ));
$sql = 'select * from `' . _DB_PREFIX_ . 'synchrone_products` where id_product = '.$id_product;
$results = Db::getInstance()->ExecuteS($sql);
if(count($results) > 0)
{
$result = $results[0];
$return["infos"]["product_id"] = $result["id_product_sync"];
if($result["id_product_sync"] == "")
{
if(in_array("sync_add", $params))
{
$infos = $this->insertNewProduct($id_product, $product, $result["id_category_sync"], null, $params);
$return["infos"] = $infos;
}
else
$return["synchron"] = false;
$return["success"] = true;
}
else
{
$sql = 'select * from `' . _DB_PREFIX_ . 'product` where id_product = '.$result["id_product_sync"];
$results = Db::getInstance()->ExecuteS($sql);
if(count($results) == 0)
{
if(in_array("sync_add", $params))
{
$infos = $this->insertNewProduct($id_product, $product, $result["id_category_sync"],null,$params);
$return["infos"] = $infos;
}
else
$return["synchron"] = false;
$return["success"] = true;
}
else
{
if(in_array("sync_edit", $params))
{
$infos = $this->insertNewProduct($id_product, $product, $result["id_category_sync"], $result["id_product_sync"],$params);
$return["infos"] = $infos;
}
else
$return["synchron"] = false;
$return["success"] = true;
}
}
}
/*}
catch(Exception $e) {
$return["synchron"] = false;
}*/
die(json_encode($return));
}
public function insertNewProduct($id_product, $product, $id_category, $product_edited = null, $params = array())
{
require_once('PSWebServiceLibrary.php');
$webService = new PrestaShopWebservice(Configuration::get('DKSYNCHRONE_LINK'), Configuration::get('DKSYNCHRONE_KEY'), false);
$object = new Product($product_edited);
if(!$product_edited)
$new_product = true;
else
$new_product = false;
$elem_sync = array();
//Synchronisation des informations générales ------------------------------------------------------------------------
if($new_product || (!$new_product && in_array("sync_titre",$params)))
{
$object->name = array((int)(Configuration::get('PS_LANG_DEFAULT')) => (string)$product->product->name->language);
$elem_sync[] = "sync_titre";
}
if($new_product || (!$new_product && in_array("sync_description",$params)))
{
$object->description = array((int)(Configuration::get('PS_LANG_DEFAULT')) => (string)$product->product->description->language);
$object->description_short = array((int)(Configuration::get('PS_LANG_DEFAULT')) => (string)$product->product->description_short->language);
$elem_sync[] = "sync_description";
}
$object->reference = $product->product->reference;
$object->on_sale = $product->product->on_sale;
$object->available_for_order = $product->product->available_for_order;
$object->condition = $product->product->condition;
if($new_product)
$object->active = false;
else
$object->active = $product->product->active;
$object->ecotax = $product->product->ecotax;
$object->width = $product->product->width;
$object->height = $product->product->height;
$object->depth = $product->product->depth;
$object->weight = $product->product->weight;
if($new_product || (!$new_product && in_array("sync_qte",$params)))
{
$object->quantity = (int)$product->product->quantity;
$object->minimal_quantity = $product->product->minimal_quantity;
$elem_sync[] = "sync_qte";
}
if($new_product || (!$new_product && in_array("sync_manufact",$params)))
{
$object->id_manufacturer = $product->product->id_manufacturer;
$elem_sync[] = "sync_manufact";
}
if($new_product || (!$new_product && in_array("sync_suplier",$params)))
{
$object->id_supplier = $product->product->id_supplier;
$elem_sync[] = "sync_suplier";
}
//--------------------------------------------------------------------------------------------------------------------
//Synchronisation SEO ------------------------------------------------------------------------
$object->link_rewrite = array((int)(Configuration::get('PS_LANG_DEFAULT')) => (string)$product->product->link_rewrite->language);
if($new_product || (!$new_product && in_array("sync_metas_title",$params)))
{
$object->meta_title = array((int)(Configuration::get('PS_LANG_DEFAULT')) => (string)$product->product->meta_title->language);
$elem_sync[] = "sync_metas_title";
}
if($new_product || (!$new_product && in_array("sync_metas_description",$params)))
{
$object->meta_description = array((int)(Configuration::get('PS_LANG_DEFAULT')) => (string)$product->product->meta_description->language);
$elem_sync[] = "sync_metas_description";
}
if($new_product || (!$new_product && in_array("sync_metas_keys",$params)))
{
$object->meta_title = array((int)(Configuration::get('PS_LANG_DEFAULT')) => (string)$product->product->meta_title->language);
$elem_sync[] = "sync_metas_keys";
}
//--------------------------------------------------------------------------------------------------------------------
$object->save();
$product_edited = $object->id;
//Synchronisation combinations ------------------------------------------------------------------------
if($new_product || (!$new_product && in_array("sync_attributes",$params)))
{
$combines = array();
$sql = 'select * from `' . _DB_PREFIX_ . 'synchrone_products_attributes`';
$results = Db::getInstance()->ExecuteS($sql);
foreach($results as $result)
{
$combines[$result["id_attribute"]] = $result["id_attribute_sync"];
}
$combinations = $product->product->associations->combinations->combinations;
foreach($combinations as $id_combination)
{
$id_combination = $id_combination->id;
$combination = $webService->get(array(
'resource' => 'combinations',
'filter[id]' => (int)$id_combination,
"display" => "full"
));
if((int)$product->product->id_default_combination == (int)$id_combination)
$default = true;
else
$default = false;
$array_values = array();
$product_option_values = $combination->combinations->combination->associations->product_option_values->product_option_value;
foreach($product_option_values as $product_option_value)
{
$array_values[] = $combines[(int)$product_option_value->id];
}
if(!$object->productAttributeExists($array_values))
{
$stock = $webService->get(array(
'resource' => 'stock_availables',
'filter[id_product_attribute]' => (int)$id_combination,
'filter[id_product]' => $id_product,
"display" => "full"
));
$quantity = $stock->stock_availables->stock_available->quantity;
$idProductAttribute = $object->addProductAttribute(
(float)$combination->combinations->combination->price,
(float)$combination->combinations->combination->weight,
$combination->combinations->combination->unit_price_impact,
(float)$combination->combinations->combination->ecotax,
(int)$quantity,
"",
strval($combination->combinations->combination->reference),
strval($combination->combinations->combination->supplier_reference),
strval($combination->combinations->combination->ean13),
$default,
NULL,
NULL
);
$object->addAttributeCombinaison($idProductAttribute, $array_values);
}
}
$elem_sync[] = "sync_attributes";
}
//--------------------------------------------------------------------------------------------------------------------
//Synchronisation du prix ----------------------------------------------------------------------------------------------
if($new_product || (!$new_product && in_array("sync_prix",$params)))
{
$specific_pricesResources = $webService->get(array(
'resource' => 'specific_prices',
'filter[id_product]' => $id_product,
"display" => "full"
));
$price = (float)$product->product->price;
$reduction = (float) $specific_pricesResources->specific_prices->specific_price->reduction;
if($reduction > 0)
$price = $price / (1-$reduction);
$price = round($price / 1.2,6);
$object->price = $price;
$object->show_price = $product->product->show_price;
$elem_sync[] = "sync_prix";
}
//--------------------------------------------------------------------------------------------------------------------
//Synchronisation des promotions ----------------------------------------------------------------------------------------------
if($new_product || (!$new_product && in_array("sync_promotions",$params)))
{
$sql = "DELETE FROM `"._DB_PREFIX_."specific_price` where id_product = ".$product_edited;
//echo $sql;
Db::getInstance()->Execute($sql);
foreach($specific_pricesResources->specific_prices->specific_price as $specific_price_sync)
{
$sql = "INSERT INTO "._DB_PREFIX_."specific_price (id_specific_price, id_specific_price_rule, id_cart, id_product, id_shop, id_shop_group, id_currency, id_country, id_group, id_customer, id_product_attribute, price, from_quantity, reduction, reduction_type, `from`, `to`)
VALUES (null,0,0,".$product_edited.",1,0,0,0,0,0,0,'".$specific_price_sync->price."','".$specific_price_sync->from_quantity."','".$specific_price_sync->reduction."','".$specific_price_sync->reduction_type."','".$specific_price_sync->from."','".$specific_price_sync->to."')";
//echo $sql."<br>";
//echo $id_product." ".$sql.'<br>';
Db::getInstance()->Execute($sql);
}
$elem_sync[] = "sync_promotions";
}
//--------------------------------------------------------------------------------------------------------------------
$object->save();
//Synchronisation de la catégorie ------------------------------------------------------------------------------------
$object->category = array($id_category);
$object->id_category_default = $id_category;
$object->updateCategories($object->category, true);
//--------------------------------------------------------------------------------------------------------------------
if($new_product || (!$new_product && in_array("sync_photo",$params)))
{
$cover = true;
foreach($product->product->associations->images->image as $img)
{
$this->copyImg($object->id, $id_image = null, $img->attributes('http://www.w3.org/1999/xlink')->href, $entity = 'products', $regenerate = true, $cover);
$cover = false;
}
$elem_sync[] = "sync_photo";
}
$sql = 'update `' . _DB_PREFIX_ . 'synchrone_products` set id_product_sync = '.$object->id.' where id_product = '.$id_product;
Db::getInstance()->Execute($sql);
$date_add = (string)$product->product->date_add;
$date_upd = (string)$product->product->date_upd;
$sql = "UPDATE `gitdk_tendanceronde`.`tr_product` SET `date_add` = '".$date_add."', `date_upd` = '".$date_upd."' WHERE `tr_product`.`id_product` =".$product_edited.";";
//echo $sql;
Db::getInstance()->Execute($sql);
return array(
"product_id" => $object->id,
"product_name" => (string)$product->product->name->language,
"new_product" => $new_product,
"synchronisation" => implode(",",$elem_sync),
"active" => $object->active
);
答案 0 :(得分:0)
大家好,我发现解决方案可能会帮助别人
有一个从ftp服务器获取文件的功能
function getFile(){
$local_file = 'file'.time().'.xml';
$server_file = 'file.xml';
$conn_id = ftp_connect($ftpurl);
$login_result = ftp_login($conn_id, 'username', 'password');
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
echo "Successfully written to $local_file\n";
} else {
echo "There was a problem\n";
}
ftp_close($conn_id);
$this->parseFileToArray($local_file);
}
我将文件解析为另一个函数以获取文件内容;
public function parseFileToArray($file){
$xmlstr = file_get_contents($file);
$xmlcont = new SimpleXMLElement($xmlstr);
$arrayProducts = array();
foreach($xmlcont as $url)
{
$arrayProducts[] = array('reference' => $url->kode,
//get the parameters u need
..............
);
}
$this->updateProducts($arrayProducts);
}
现在我将数据传输到函数执行更新
public function updateProducts($arrayProducts){
foreach($arrayProducts as $arrayProduct)
{
$product_id = // sql request to get the product id that match the reference
if($product_id){
$object = new Product($product_id);
$object->wholesale_price = $arrayProduct['wholesale_price'];
// the update
$object->save();
}
}
}