V8x Problem with prestashop addon. Extra field

alfrederty

Member
XNullUser
Joined
Feb 26, 2022
Messages
56
Reaction score
0
Points
6
NullCash
15
Hello! I have a issue with a product extrafield module. I create a extra field to the product to add, Image title description as foreach. But the Image wont uploaded.

public function hookActionProductUpdate($params)
{
$id_product = (int)$params['id_product'];

Db::getInstance()->delete('product_extra_fields', 'id_product = ' . (int)$id_product);

foreach ($_POST['extra_field_title'] as $index => $title) {
$description = $_POST['extra_field_description'][$index];
$image_existing = isset($_POST['extra_field_image_existing'][$index]) ? $_POST['extra_field_image_existing'][$index] : '';
$image = '';

// Log the index and file details
PrestaShopLogger::addLog('Processing index: ' . $index, 1);
if (isset($_FILES['extra_image']) && isset($_FILES['extra_image']['tmp_name'][$index]) && $_FILES['extra_image']['tmp_name'][$index] != '') {
PrestaShopLogger::addLog('Uploading image at index: ' . $index, 1);
$image = $this->uploadImage($_FILES['extra_image'], $index);
PrestaShopLogger::addLog('Uploaded image: ' . $image, 1);
} else {
PrestaShopLogger::addLog('Using existing image: ' . $image_existing, 1);
$image = "none";
}

PrestaShopLogger::addLog('Inserting into database: title=' . $title . ', description=' . $description . ', image=' . $image, 1);

$result = Db::getInstance()->insert('product_extra_fields', array(
'id_product' => (int)$id_product,
'title' => pSQL($title),
'description' => pSQL($description),
'image' => pSQL($image),
));

// Log the result of the insertion
if ($result) {
PrestaShopLogger::addLog('Successfully inserted into database', 1);
} else {
PrestaShopLogger::addLog('Failed to insert into database', 3);
}
}
}
protected function uploadImage($file, $index)
{
$allowedExtensions = array('jpg', 'jpeg', 'png', 'gif');
$extension = pathinfo($file['name'][$index], PATHINFO_EXTENSION);
if (!in_array(strtolower($extension), $allowedExtensions)) {
PrestaShopLogger::addLog('Invalid file extension: ' . $extension, 3);
return '';
}
$uploadDir = _PS_IMG_DIR_ . 'extra_fields/inner/';
if (!file_exists($uploadDir)) {
mkdir($uploadDir, 0755, true);
}
$filename = uniqid() . '.' . $extension;
$destination = $uploadDir . $filename;
if (move_uploaded_file($file['tmp_name'][$index], $destination)) {
PrestaShopLogger::addLog('File uploaded: ' . $filename, 1);
return $filename;
}
PrestaShopLogger::addLog('Failed to upload file: ' . $file['name'][$index], 3);
return '';
}
 

Rigers

Member
XNullUser
Joined
Jan 12, 2022
Messages
177
Reaction score
1
Points
18
NullCash
2
prestashop how versions ? mysql import code ?
 

FloverLion

Well-known member
Diamond
Elite
XNullUser
Joined
Sep 30, 2022
Messages
888
Reaction score
250
Points
63
NullCash
607
Please post your questions and thoughts in the recuers section.!!!
 
Top