Prestashop – 用于向产品添加附件的PHP脚本

huangapple go评论55阅读模式
英文:

Prestashop - php script to add attachements to product

问题

我已经创建了一个PHP脚本,允许我为已导入的产品添加附件(请参见下文)。
一切都正常,3个表ps_attachemnt、lang...都已正确填充。
在后台,在产品文件的“选项”选项卡中,我可以看到我的附件已被选中。

问题:
我在前端(产品文件)看不到它,除非我点击:预览和/或保存。

已采取的措施:

  1. 清空缓存
  2. 删除var/cache文件夹中的缓存文件(dev/prod)
  3. 创建一个使用save()函数的PHP脚本;($product->save();)

有人有线索吗?

谢谢 Prestashop – 用于向产品添加附件的PHP脚本

<?php
$csvFile = "xxxx.csv";
if (($handle = fopen($csvFile, "r")) !== FALSE) {
// Parcours des lignes du fichier CSV
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

   $reference = $data[0]; // 产品引用
   $files = explode(",", $data[1]); // 文件路径(用逗号分隔)

   // 获取产品ID
   $sql = "SELECT id_product FROM ps_product WHERE reference = '$reference'";
   $result = $conn->query($sql);

   echo "$reference";

   if ($result->num_rows > 0) {
       $row = $result->fetch_assoc();
       $productId = $row["id_product"];

       echo "$productId";

       // 遍历附加文件
       foreach ($files as $file) {
           // 获取文件大小(以字节为单位)
           $fileSize = filesize($file);
           $fileName = basename($file); // 文件名

           // 检查文件是否已存在于给定的产品中
           $sqlCheck = "SELECT id_attachment FROM ps_product_attachment WHERE id_product = '$productId'";
           $resultCheck = $conn->query($sqlCheck);

           $attachmentId = null;

           if ($resultCheck->num_rows > 0) {
               // 遍历结果以检查文件是否已存在
               while ($rowCheck = $resultCheck->fetch_assoc()) {
                   $existingAttachmentId = $rowCheck["id_attachment"];

                   // 检查文件是否已与产品关联
                   $sqlFileCheck = "SELECT id_attachment FROM ps_attachment WHERE id_attachment = '$existingAttachmentId' AND file = '$file'";
                   $resultFileCheck = $conn->query($sqlFileCheck);

                   if ($resultFileCheck->num_rows > 0) {
                       // 文件已经存在于给定的产品中
                       $attachmentId = $existingAttachmentId;
                       break;
                   }
               }
           }

           if ($attachmentId === null) {
               // 产品中尚不存在该文件,将其插入到ps_attachment中
               $sqlInsertAttachment = "INSERT INTO ps_attachment (file, file_name, file_size, mime) VALUES ('$file', '$fileName', '$fileSize', 'application/pdf')";
               if ($conn->query($sqlInsertAttachment) === TRUE) {
                   $attachmentId = $conn->insert_id;
               } else {
                   echo "添加附加文件时出错:" . $conn->error . "\n";
               }
           }

           if ($attachmentId !== null) {
               // 在ps_product_attachment中将附件关联到产品
               $sqlInsertProductAttachment = "INSERT INTO ps_product_attachment (id_product, id_attachment) VALUES ('$productId', '$attachmentId')";
               if ($conn->query($sqlInsertProductAttachment) === TRUE) {
                   echo "成功为产品 $reference 添加附件。\n";

                   // 插入后获取附件ID
                   $idAttachment = $attachmentId;

                   // 遍历支持的语言
                   $idLang = 1;
                   $name = $data[$idLang]; // 获取指定语言的附件名称
                   $description = $data[$idLang]; // 获取指定语言的附件描述

                   // 在ps_attachment_lang表中插入语言信息
                   $sqlInsertAttachmentLang = "INSERT INTO ps_attachment_lang (id_attachment, id_lang, name, description) VALUES ('$idAttachment', '$idLang', '$name', '$description')";
                   if ($conn->query($sqlInsertAttachmentLang) === TRUE) {
                       echo "为附件 $idAttachment 和语言 $idLang 成功添加语言信息。\n";
                   } else {
                       echo "添加语言信息时出错:" . $conn->error . "\n";
                   }
               } else {
                   echo "将附件关联到产品 $reference 时出错:" . $conn->error . "\n";
               }
           }
       }
   } else {
       echo "未找到产品的引用:$reference。\n";
   }
}
fclose($handle);
}

$conn->close();
?>

更新脚本尝试:

<?php

require(dirname(__FILE__).'/config/config.inc.php');

define('_PS_ROOT_DIR_', dirname(dirname(__FILE__)).'/');
// 包含Product类
require_once _PS_ROOT_DIR_ . '/classes/Product.php';

// 要定位的产品引用列表
$targetReferences = array(
'20052'
);

foreach ($targetReferences as $reference) {
// 根据引用查找产品
$productId = Product::getIdByReference($reference);

if ($productId) {
	$product = new Product($productId);

	// 保存选项而不进行修改
	$product->save();

	echo "产品已更新,引用:" . $reference . 
	"<br>";
} else {
	// 处理产品引用未找到的情况
	echo "未找到引用为:" . $reference . 
	"<br>";
}
}

?>
英文:

I've created a php script that allows me to add attachments to products that have already been imported (see below).
Everything works fine, the 3 tables ps_attachemnt, lang... are filled in correctly.
In the back office, in the product file, in the 'options' tab, I can see that my attachment is ticked.

Problem :
I can't see it in the front end (on the product file) EXCEPT if I click on : Preview and/or Save.

Action taken :

  1. Empty cache
  2. Delete the cache folders in var/cache ( dev / prod)
  3. Create a php script that uses the save() function; ($product->save();)

Does anyone have any clues?

Thank you Prestashop – 用于向产品添加附件的PHP脚本

&lt;?php
$csvFile = &quot;xxxx.csv&quot;;
if (($handle = fopen($csvFile, &quot;r&quot;)) !== FALSE) {
// Parcours des lignes du fichier CSV
while (($data = fgetcsv($handle, 1000, &quot;,&quot;)) !== FALSE) {
$reference = $data[0]; // R&#233;f&#233;rence du produit
$files = explode(&quot;,&quot;, $data[1]); // Chemins vers les fichiers (s&#233;par&#233;s par une virgule)
// R&#233;cup&#233;ration de l&#39;ID du produit
$sql = &quot;SELECT id_product FROM ps_product WHERE reference = &#39;$reference&#39;&quot;;
$result = $conn-&gt;query($sql);
echo &quot;$reference&quot;;
if ($result-&gt;num_rows &gt; 0) {
$row = $result-&gt;fetch_assoc();
$productId = $row[&quot;id_product&quot;];
echo &quot;$productId&quot;;
// Parcours des fichiers joints
foreach ($files as $file) {
// Obtention de la taille du fichier en octets
$fileSize = filesize($file);
$fileName = basename($file); // Nom du fichier
// V&#233;rification si le fichier existe d&#233;j&#224; pour le produit donn&#233;
$sqlCheck = &quot;SELECT id_attachment FROM ps_product_attachment WHERE id_product = &#39;$productId&#39;&quot;;
$resultCheck = $conn-&gt;query($sqlCheck);
$attachmentId = null;
if ($resultCheck-&gt;num_rows &gt; 0) {
// Parcourir les r&#233;sultats pour v&#233;rifier si le fichier existe d&#233;j&#224;
while ($rowCheck = $resultCheck-&gt;fetch_assoc()) {
$existingAttachmentId = $rowCheck[&quot;id_attachment&quot;];
// V&#233;rifier si le fichier est d&#233;j&#224; associ&#233; au produit
$sqlFileCheck = &quot;SELECT id_attachment FROM ps_attachment WHERE id_attachment = &#39;$existingAttachmentId&#39; AND file = &#39;$file&#39;&quot;;
$resultFileCheck = $conn-&gt;query($sqlFileCheck);
if ($resultFileCheck-&gt;num_rows &gt; 0) {
// Le fichier existe d&#233;j&#224; pour le produit donn&#233;
$attachmentId = $existingAttachmentId;
break;
}
}
}
if ($attachmentId === null) {
// Le fichier n&#39;existe pas encore pour le produit, l&#39;ins&#233;rer dans ps_attachment
$sqlInsertAttachment = &quot;INSERT INTO ps_attachment (file, file_name, file_size, mime) VALUES (&#39;$file&#39;, &#39;$fileName&#39;, &#39;$fileSize&#39;, &#39;application/pdf&#39;)&quot;;
if ($conn-&gt;query($sqlInsertAttachment) === TRUE) {
$attachmentId = $conn-&gt;insert_id;
} else {
echo &quot;Erreur lors de l&#39;ajout du fichier joint : &quot; . $conn-&gt;error . &quot;\n&quot;;
}
}
if ($attachmentId !== null) {
// Associer le fichier joint au produit dans ps_product_attachment
$sqlInsertProductAttachment = &quot;INSERT INTO ps_product_attachment (id_product, id_attachment) VALUES (&#39;$productId&#39;, &#39;$attachmentId&#39;)&quot;;
if ($conn-&gt;query($sqlInsertProductAttachment) === TRUE) {
echo &quot;Fichier joint ajout&#233; avec succ&#232;s pour le produit $reference.\n&quot;;
// R&#233;cup&#233;ration de l&#39;ID de l&#39;attachment apr&#232;s insertion
$idAttachment = $attachmentId;
// Parcours des langues prises en charge
$idLang = 1;
$name = $data[$idLang]; // R&#233;cup&#233;ration du nom du fichier joint dans la langue sp&#233;cifi&#233;e
$description = $data[$idLang]; // R&#233;cup&#233;ration de la description du fichier joint dans la langue sp&#233;cifi&#233;e
// Insertion des informations de langue dans la table ps_attachment_lang
$sqlInsertAttachmentLang = &quot;INSERT INTO ps_attachment_lang (id_attachment, id_lang, name, description) VALUES (&#39;$idAttachment&#39;, &#39;$idLang&#39;, &#39;$name&#39;, &#39;$description&#39;)&quot;;
if ($conn-&gt;query($sqlInsertAttachmentLang) === TRUE) {
echo &quot;Informations de langue ajout&#233;es avec succ&#232;s pour l&#39;attachment $idAttachment et la langue $idLang.\n&quot;;
} else {
echo &quot;Erreur lors de l&#39;ajout des informations de langue : &quot; . $conn-&gt;error . &quot;\n&quot;;
}
} else {
echo &quot;Erreur lors de l&#39;association du fichier joint pour le produit $reference : &quot; . $conn-&gt;error . &quot;\n&quot;;
}
}
}
} else {
echo &quot;Produit non trouv&#233; pour la r&#233;f&#233;rence : $reference.\n&quot;;
}
}
fclose($handle);
}
$conn-&gt;close();
?&gt;

Update script try :

&lt;?php
require(dirname(__FILE__).&#39;/config/config.inc.php&#39;);
define(&#39;_PS_ROOT_DIR_&#39;, dirname(dirname(__FILE__)).&#39;/&#39;);
// Inclusion de la classe Product
require_once _PS_ROOT_DIR_ . &#39;/classes/Product.php&#39;;
// Liste des r&#233;f&#233;rences de produits &#224; cibler
$targetReferences = array(
&#39;20052&#39;
);
foreach ($targetReferences as $reference) {
// Recherche du produit par r&#233;f&#233;rence
$productId = Product::getIdByReference($reference);
if ($productId) {
$product = new Product($productId);
// Enregistrer les options sans modification
$product-&gt;save();
echo &quot;Produit mis &#224; jour pour la r&#233;f&#233;rence : &quot; . $reference . 
&quot;&lt;br&gt;&quot;;
} else {
// G&#233;rer le cas o&#249; la r&#233;f&#233;rence du produit n&#39;est pas trouv&#233;e
echo &quot;Produit non trouv&#233; pour la r&#233;f&#233;rence : &quot; . $reference . 
&quot;&lt;br&gt;&quot;;
}
}
?&gt;

答案1

得分: 0

确保在保存产品之前设置属性:

$product->cache_has_attachments = 1;

我之前也遇到过相同的问题,花了一些时间才解决了它 Prestashop – 用于向产品添加附件的PHP脚本

英文:

Make sure to set the property

$product-&gt;cache_has_attachments = 1;

before saving the product.

I encountered the same issue some time ago, and it took me quite a while to figure it out Prestashop – 用于向产品添加附件的PHP脚本

huangapple
  • 本文由 发表于 2023年7月11日 03:35:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656802.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定