Search code examples
restwoocommercefile-upload

Update URL of uploaded file in Woocommerce line item


I have a woocommerce site (WP) where the user uploads a number of images when they place their order. I interact with these orders via a custom backend application that accesses the order/items using the REST API.

I would like to be able to accomplish 2 tasks. First I need to be able to delete the uploaded file from the backend storage area (using the files URL I would assume) then I need to update the uploaded file record so that I can replace the URL of the uploaded file to a new URL of my creation.

I know there are a few plugins that work in this area but for a number of reasons they are not suitable for my use case.


Solution

  • I think you can do something like this.

    // Initialize WP Filesystem
    WP_Filesystem();
    global $wp_filesystem;
    
    // Copy the file to a new location
    $wp_filesystem->copy($original_file_path, $new_file_path);
    
     // Update the database record with the new file URL
     global $wpdb;
     $wpdb->update(
     $table_name,
     array('file_url' => $new_file_url),
     array('order_id' => $order_id)
    );
    
    // Delete the original file
    $wp_filesystem->delete($original_file_path);
    

    Edit:

    Here, the original file is deleted from Amazon S3, a new one is uploaded, and the WooCommerce line item metadata is updated using the REST API.

    <?php
    require 'vendor/autoload.php';  // Required if using Composer for AWS SDK
    
    use Aws\S3\S3Client;
    
    // Initialize AWS S3 client
    $s3 = new S3Client([
        'version' => 'latest',
        'region'  => 'your_aws_region'
    ]);
    
    // Delete the original file from S3
    $s3->deleteObject([
        'Bucket' => 'your_bucket_name',
        'Key'    => 'your_original_file_key'
    ]);
    
    // Upload the new file to S3
    $result = $s3->putObject([
        'Bucket' => 'your_bucket_name',
        'Key'    => 'your_new_file_key',
        'SourceFile' => 'path/to/your/new/file'
    ]);
    
    $new_file_url = $result['ObjectURL'];
    
    // Define the order ID and line item ID
    $order_id = 'your_order_id_here';
    $line_item_id = 'your_line_item_id_here';
    
    // API endpoint
    $api_endpoint = "https://yourwebsite.com/wp-json/wc/v3/orders/{$order_id}";
    
    // Authentication
    $consumer_key = 'your_consumer_key_here';
    $consumer_secret = 'your_consumer_secret_here';
    
    // Data payload
    $data = array(
    'line_items' => array(
        array(
        'id' => $line_item_id,
        'meta_data' => array(
            array(
            'key' => 'file_url',
            'value' => $new_file_url,
            ),
        ),
        ),
    ),
    );
    
    // API request arguments
    $args = array(
    'method' => 'PUT',
    'headers' => array(
        'Authorization' => 'Basic ' . base64_encode($consumer_key . ':' . $consumer_secret),
    ),
    'body' => json_encode($data),
    );
    
    // Execute the API request
    $response = wp_remote_post($api_endpoint, $args);
    
    // Check for success
    if (is_wp_error($response)) {
    $error_message = $response->get_error_message();
    echo "Something went wrong: $error_message";
    } else {
    echo 'Successfully updated line item metadata.';
    }
    ?>