[SOLVED] How can I add a button on the front end of the product page to download all product images?
Hello everyone.
Can anyone help or hint with this question..
Any feedback is greatly appreciated..
Version 3.0.3.8
Thank you.
Can anyone help or hint with this question..
Any feedback is greatly appreciated..
Version 3.0.3.8
Thank you.
Last edited by nureddin21 on Wed Mar 23, 2022 4:01 am, edited 1 time in total.
So what, you have all the images in a downloadable zip file or you expect to scrape all the images off the page.
I got this code but it doesn't work..mikeinterserv wrote: ↑Fri Mar 18, 2022 10:55 pmSo what, you have all the images in a downloadable zip file or you expect to scrape all the images off the page.
I want to download all product images (either zip file or download them without compression)
Code: Select all
public function downloadcatalog(){
$this->load->model('catalog/product');
$product_info = $this->model_catalog_product->getProduct($this->request->get['product_id']);
$results = $this->model_catalog_product->getProductImages($this->request->get['product_id']);
foreach ($results as $result) {
$files[] = 'image/'.$result['image'];
}
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.', '');
$zip->open($tmp_file, ZipArchive::CREATE);
# loop through each file
foreach ($files as $file) {
# download file
$download_file = file_get_contents($file);
#add it to the zip
$zip->addFromString(basename($file), $download_file);
}
# close zip
$zip->close();
$file_name = $product_info['name'].'.zip';
# send the file to the browser as a download
/*header('Content-disposition: attachment; filename="'.$product_info['name'].'"');
header('Content-type: application/zip');*/
header("HTTP/1.1 200 OK");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/zip");
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($tmp_file));
//$zip->close();
//readfile($zipname);
readfile($tmp_file);
unlink($tmp_file);
}
Here in bold you are trying to get the FILE CONTENTS and NOT the file - If it were a text file the contents would be the txt.file_get_contents($file);
The problem with this code is that it downloads an empty zip file
FYI : It is true that maybe someone from the “community” will assist, however the question has no valid community benefit and is outside of the scope of free support. The purpose of the forum is not to be abused for personal benefit but to be of general use to the community as a whole. Maybe consider adapting your question to be of community benefit and adapt the answer to your personal use ?
DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.
https://www.youtube.com/watch?v=zXIxDoCRc84
Well I would say this code is not what you want.
There is a LOT wrong with the code you have posted.
There is a LOT wrong with the code you have posted.
You can use Extension https://www.opencart.com/index.php?rout ... n_id=43489nureddin21 wrote: ↑Fri Mar 18, 2022 5:24 pmHello everyone.
Can anyone help or hint with this question..
Any feedback is greatly appreciated..
Version 3.0.3.8
Thank you.
You may like these extension : - https://www.opencart.com/index.php?rout ... r=cmsrooms
Try this mines FREE
Your previous code only attempted to get the additional images.
This gets the main image plus all additional images. I will make it into an Extension and post it here later.
YES it downloads the ORIGINAL IMAGES as uploaded - NOT the cached versions
To download the images WITHOUT FOLDERS so the zip contains JUST the images change line. To:
.
Your previous code only attempted to get the additional images.
This gets the main image plus all additional images. I will make it into an Extension and post it here later.
YES it downloads the ORIGINAL IMAGES as uploaded - NOT the cached versions
To download the images WITHOUT FOLDERS so the zip contains JUST the images change line.
Code: Select all
$zip->addFile($file));
Code: Select all
$zip->addFile($file, basename($file));
Code: Select all
public function downloadcatalog(){
$this->load->model('catalog/product');
$product_info = $this->model_catalog_product->getProduct((int)$this->request->get['product_id']);
$results = $this->model_catalog_product->getProductImages((int)$this->request->get['product_id']);
foreach ($results as $result) {
$files[] ='image/'.$result['image'];
}
$files[] ='image/'.$product_info['image'];
$zip = new ZipArchive;
$zip_name = $product_info['name'].'.zip';
if ($zip->open($zip_name, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) === TRUE){
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
}
if (file_exists($zip_name)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($zip_name) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($zip_name));
readfile($zip_name);
unlink($zip_name);
exit;
}
}
Last edited by mikeinterserv on Wed Mar 23, 2022 4:27 am, edited 3 times in total.
It could also be done with an Event by using a button to download. In addition:
instances should all be changed to:
Code: Select all
$this->request->get['product_id']
Code: Select all
(int)$this->request->get['product_id']
Dedication and passion goes to those who are able to push and merge a project.
Regards,
Straightlight
Programmer / Opencart Tester
Thank you, but I tried the last code and it only exports the main image.. I think Maybe it does not enter to the first "foreach" ..mikeinterserv wrote: ↑Tue Mar 22, 2022 6:23 amThis gets the main image plus all additional images. I will make it into an Extension and post it here later.
Another question, please.. Is it possible to export all product images without compressing them?
Well it does,I think Maybe it does not enter to the first "foreach"
What do you mean - It has been tested and works perfectly for ALL product images.
It means you have NO additional images if you do not see them added to the zip
Yes, you are right, I tested it with the default template and it works perfectly, but with Journal3mikeinterserv wrote: ↑Tue Mar 22, 2022 11:06 pmWell it does,I think Maybe it does not enter to the first "foreach"
What do you mean - It has been tested and works perfectly for ALL product images.
It means you have NO additional images if you do not see them added to the zip

Well journal is unsupported and personally I have NO IDEA what journal does thats different.nureddin21 wrote: ↑Wed Mar 23, 2022 2:05 amYes, you are right, I tested it with the default template and it works perfectly, but with Journal3, it does export the main image only... and I don't know why
I would have to see the source code of the product page to see whats different.
Yes, I know that the journal theme is not compatible with the standards and is unsupported here, etc.mikeinterserv wrote: ↑Wed Mar 23, 2022 2:13 amWell journal is unsupported and personally I have NO IDEA what journal does thats different.nureddin21 wrote: ↑Wed Mar 23, 2022 2:05 amYes, you are right, I tested it with the default template and it works perfectly, but with Journal3, it does export the main image only... and I don't know why
I would have to see the source code of the product page to see whats different.
But there is no difference in Controller file between both templates
So where could the problem be?
Of course there is a difference otherwise it would worknureddin21 wrote: ↑Wed Mar 23, 2022 2:17 amBut there is no difference in Controller file between both templates
So where could the problem be?
Give me the product.php as an attachment here.
catalog/controller/product/product.php
Aoo good even though I am not a programmer but I kept trying until I got to the solution and let me post it here ..mikeinterserv wrote: ↑Wed Mar 23, 2022 2:19 amOf course there is a difference otherwise it would worknureddin21 wrote: ↑Wed Mar 23, 2022 2:17 amBut there is no difference in Controller file between both templates
So where could the problem be?
Give me the product.php as an attachment here.
catalog/controller/product/product.php
In addition to what you mentioned, there is a slight modification In the catalog/model/journal3/product.php file
add
Code: Select all
public function getProductImages($product_id) {
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_image WHERE product_id = '" . (int)$product_id . "' ORDER BY sort_order ASC");
return $query->rows;
}
Code: Select all
$this->load->model('journal3/product');
Code: Select all
$results = $this->model_catalog_product->getProductImages((int)$this->request->get['product_id']);
to
Code: Select all
$results = $this->model_journal3_product->getProductImages($this->request->get['product_id']);
Code: Select all
unlink($zip_name);
It works perfectly for me
And just there trying to download the images without having to compress them.
Good I am glad you have it working now.nureddin21 wrote: ↑Wed Mar 23, 2022 3:51 amIt works perfectly for me
And just there trying to download the images without having to compress them.
Sorry I forgot unlink, I have added it now.
Here is an OCmod based on EVENTS for product images download.
Installs as a MODULE in extensions/modules. You can install / uninstall from there.
Gets the ORIGINAL images as uploaded, zips and automatically downloads.
This is the code provided earlier in a Mod.
.
Installs as a MODULE in extensions/modules. You can install / uninstall from there.
Gets the ORIGINAL images as uploaded, zips and automatically downloads.
This is the code provided earlier in a Mod.
.
Who is online
Users browsing this forum: lockiedownunder and 25 guests