Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.6 kB
1
Indexable
Never

// Hook into WooCommerce to modify the download URLs
function update_product_downloadable_files($product_id) {
    // Get the current downloadable files meta
    $downloadable_files = get_post_meta($product_id, '_downloadable_files', true);

    // Check if there are downloadable files
    if (!is_array($downloadable_files)) {
        return; // No downloadable files, exit the function
    }

    // Flag to check if updates are needed
    $update_needed = false;

    // Iterate over all files
    foreach ($downloadable_files as $key => $file_data) {
        // Use regex to extract the PID from the file URL
        $matches = [];
        if (preg_match('/\?pid=(\d+)/', $file_data['file'], $matches)) {
            $attachment_id = $matches[1]; // The PID is the post ID of the attachment

            // Retrieve the S3 info meta
            $s3_info = get_post_meta($attachment_id, 'ilab_s3_info', true);

            // If S3 info is available and contains a URL, update the download URL
            if (!empty($s3_info) && isset($s3_info['url'])) {
				var_dump($s3_info['url']);
                $downloadable_files[$key]['file'] = $s3_info['url'];
                $update_needed = true;
            }
        }
    }

    // If updates are needed, save the modified downloads back to the product
    if ($update_needed) {
        update_post_meta($product_id, '_downloadable_files', $downloadable_files);
    }
}

add_action('marketking_after_save_product', function($product_id, $vendor_id){
	update_product_downloadable_files($product_id); 
}, 10, 2);


Leave a Comment