I have 2 codes for a “download images button”:
- to create the zip files:
function create_product_images_zip($product_id) {
// Load the WordPress Filesystem API
if ( ! defined('ABSPATH') ) {
exit; // Exit if accessed directly
}
require_once ABSPATH . 'wp-admin/includes/file.php';
WP_Filesystem();
global $product;
$product = wc_get_product($product_id);
if (!$product) {
return;
}
// Get the product images
$featured_image_id = $product->get_image_id();
$gallery_image_ids = $product->get_gallery_image_ids();
// Get the upload directory
$upload_dir = wp_upload_dir();
$zip_file = $upload_dir['basedir'] . '/product-images-' . $product_id . '.zip';
// Initialize the ZIP archive
$zip = new ZipArchive();
if ($zip->open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
return;
}
// Add the featured image to the ZIP
if ($featured_image_id) {
$featured_image_url = wp_get_attachment_url($featured_image_id);
$featured_image_path = get_attached_file($featured_image_id);
$zip->addFile($featured_image_path, basename($featured_image_path));
}
// Add the gallery images to the ZIP
if (!empty($gallery_image_ids)) {
foreach ($gallery_image_ids as $gallery_image_id) {
$gallery_image_url = wp_get_attachment_url($gallery_image_id);
$gallery_image_path = get_attached_file($gallery_image_id);
$zip->addFile($gallery_image_path, basename($gallery_image_path));
}
}
// Close the ZIP archive
$zip->close();
return $upload_dir['baseurl'] . '/product-images-' . $product_id . '.zip';
}
And 2) to create the download button:
add_action('woocommerce_single_product_summary', 'add_download_all_images_button', 25);
function add_download_all_images_button() {
global $product;
if ($product) {
$product_id = $product->get_id();
$zip_url = create_product_images_zip($product_id);
if ($zip_url) {
echo '<a href="' . esc_url($zip_url) . '" class="custom-download-button" download>Download All Images</a>';
}
}
}
This button is linked to the product description on WooCommerce. I want to linked it with the image gallery, so it appear under the photos. i tried this for the zip file part:
function create_product_images_zip($product_id) {
// Create a new ZIP file
$zip = new ZipArchive();
$upload_dir = wp_upload_dir();
$zip_file = tempnam($upload_dir['path'], 'product_images_') . '.zip';
if ($zip->open($zip_file, ZipArchive::CREATE) !== TRUE) {
return false;
}
// Get all the product images (including gallery images)
$attachment_ids = array();
$attachment_ids[] = get_post_thumbnail_id($product_id); // Featured image
$attachment_ids = array_merge($attachment_ids, $product->get_gallery_image_ids()); // Gallery images
// Add each image to the ZIP file
foreach ($attachment_ids as $attachment_id) {
$image_url = wp_get_attachment_url($attachment_id);
$image_path = get_attached_file($attachment_id);
if (file_exists($image_path)) {
$zip->addFile($image_path, basename($image_path));
}
}
// Close the ZIP file
$zip->close();
// Return the URL to the ZIP file
$zip_url = $upload_dir['url'] . '/' . basename($zip_file);
return $zip_url;
}
And this for the download button:
add_action('woocommerce_product_thumbnails', 'add_download_all_images_button', 20);
function add_download_all_images_button() {
global $product;
if ($product) {
$product_id = $product->get_id();
$zip_url = create_product_images_zip($product_id);
if ($zip_url) {
echo '<a href="' . esc_url($zip_url) . '" class="custom-download-button" download>Download All Images</a>';
}
}
}
And it is breaking the website. Where are the errors?
I tried to change the code as previously explained, but it breaks the product page completely.
Rodrigo UX is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1