You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress+Themify搭建WooCommerce图片库:自动填充可变产品下载链接咨询

Solution: Auto-Populate Digital/Print Downloads from Professional Print High-Res Images

Great question! This is a super useful workflow for WooCommerce image libraries, and we can build it using WooCommerce's native hooks and WordPress media tools. Here's a step-by-step implementation tailored to your setup:

Step 1: Confirm Your Media Settings

First, make sure WordPress is generating the image sizes you need for the Digital/Print variation:

  • Head to Dashboard > Settings > Media
  • Double-check that "Medium" and "Large" sizes are configured with your preferred dimensions (these are the auto-resized versions that will populate your Digital/Print download area)
  • Note the size slugs: medium and large (we’ll reference these in the code later)

Step 2: Add Custom Code

Paste this code into your child theme's functions.php file, or use a custom plugin (using a plugin is safer to avoid losing changes when your theme updates):

add_action('woocommerce_admin_process_product_object', 'auto_populate_digital_print_downloads', 10, 1);

function auto_populate_digital_print_downloads($product) {
    // Skip if this isn't a variable product
    if (!$product->is_type('variable')) return;

    // --- EDIT THESE VALUES TO MATCH YOUR STORE ---
    $attribute_slug = 'pa_size'; // Replace with your variation attribute slug (e.g., 'pa_size')
    $pro_print_term = 'professional-print'; // Slug for your "Professional Print" variation
    $digital_print_term = 'digital-print'; // Slug for your "Digital/Print" variation
    $target_sizes = ['large', 'medium']; // Image sizes to add to Digital/Print
    // --- END OF EDITABLE SECTION ---

    $pro_variation = null;
    $digital_variation = null;

    // Find our two target variations
    foreach ($product->get_children() as $variation_id) {
        $variation = wc_get_product($variation_id);
        $attribute_value = $variation->get_attribute($attribute_slug);
        
        if ($attribute_value === $pro_print_term) {
            $pro_variation = $variation;
        } elseif ($attribute_value === $digital_print_term) {
            $digital_variation = $variation;
        }
    }

    // Exit if either variation is missing
    if (!$pro_variation || !$digital_variation) return;

    // Get downloadable images from the Professional Print variation
    $pro_downloads = $pro_variation->get_downloads();
    $new_digital_downloads = [];

    foreach ($pro_downloads as $download) {
        $file_url = $download->get_file();
        $attachment_id = attachment_url_to_postid($file_url);
        
        // Skip if the file isn't a WordPress-hosted image
        if (!$attachment_id || !wp_attachment_is_image($attachment_id)) continue;

        // Grab the auto-generated image sizes
        foreach ($target_sizes as $size) {
            $image_data = wp_get_attachment_image_src($attachment_id, $size);
            if (!$image_data) continue;

            $image_url = $image_data[0];
            $image_filename = basename($image_url);

            // Create a new downloadable file entry
            $new_download = new WC_Download();
            $new_download->set_file($image_url);
            $new_download->set_name(ucfirst($size) . ' - ' . $download->get_name());
            $new_download->set_id(uniqid()); // Unique ID to avoid duplicate entries

            $new_digital_downloads[] = $new_download;
        }
    }

    // Update the Digital/Print variation if we have new files to add
    if (!empty($new_digital_downloads)) {
        // Optional: Uncomment below to replace existing downloads instead of adding to them
        // $digital_variation->set_downloads([]);
        
        $existing_downloads = $digital_variation->get_downloads();
        $combined_downloads = array_merge($existing_downloads, $new_digital_downloads);
        
        $digital_variation->set_downloads($combined_downloads);
        $digital_variation->save();
    }
}

Key Customization Tips

  • Match your variation setup: Replace the editable values with your actual attribute and term slugs. To find these:
    • Go to Products > Attributes
    • Locate your variation attribute (like "Size") and copy its slug
    • Click "Terms" to get the slugs for "Digital/Print" and "Professional Print"
  • Adjust image sizes: Modify $target_sizes if you only want one size (e.g., ['large'] instead of both)
  • Control duplicates: If you want to overwrite existing auto-generated downloads each time you save, uncomment the line that clears existing downloads before adding new ones

How It Works

  1. When you save a variable product in the admin, the code scans for your two target variations
  2. It pulls all image downloads from the Professional Print variation
  3. For each high-res image, it fetches the auto-resized large/medium versions from WordPress' media library
  4. It adds these smaller images as downloadable files to the Digital/Print variation
  5. The updated variation is saved automatically, so you don’t have to do manual file uploads

内容的提问来源于stack exchange,提问作者Anthony Cdf Whitefield

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:33:58