How to set a custom option of type "file" when adding a product to the cart in Magento?

How to set a custom option of type "file" when adding a product to the cart in Magento?

October 5, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to set a custom option of type "file" when adding a product to the cart in Magento?

Setting Custom File Options in Magento

When adding products to the cart programmatically in Magento using your own controller, you may encounter challenges when setting custom options, especially those of type “file”. This guide will walk you through the process of correctly configuring a file option for a product being added to the shopping cart.

Understanding the Cart Addition Process

Let’s start by examining the current code provided:

// obtain the shopping cart
$cart = Mage::getSingleton('checkout/cart');

// load the product
$product = Mage::getModel('catalog/product')
    ->load($productId);

// do some magic to obtain the select ids for color and size ($selectedSize and $selectedColor)
// ...

// define the buy request params
$params = array(
    'product'       => $productId,
    'qty'           => $quantity,
    'options'       => array(
        $customOptionSize->getId()  => $selectedSize,
        $customOptionColor->getId() => $selectedColor,

        // set the file option, but how?
    ),
);

// add this configuration to cart
$cart->addProduct($product, $paramObject);
$cart->save();

// set the cart as updated
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

The code correctly loads the product and sets up the color and size options. However, setting a file option is not straightforward.

Setting a Custom File Option

To attach a certain file to the design option, you need to provide the correct data in the options array. Here’s how to do it:

// define the buy request params
$params = array(
    'product'       => $productId,
    'qty'           => $quantity,
    'options'       => array(
        $customOptionSize->getId()  => $selectedSize,
        $customOptionColor->getId() => $selectedColor,

        // set the file option
        $fileOption->getId() => array('option_id' => $fileOption->getId(), 'value' => $uploadedFileName),
    ),
);

In this updated version, I’ve added the fileOption to the options array. The value for the file option must be an associative array with two keys:

  1. option_id: This is the ID of your custom file option.
  2. value: This should be the name or path of the uploaded file.

Uploading and Managing Files

While not directly part of setting the cart, it’s important to note that you’ll need to handle the actual file upload and storage on the server side before adding the product to the cart. You can use Magento’s built-in functionality for file uploads or your own custom logic based on your specific needs.

Conclusion

Setting a custom “file” option when adding products to the cart in Magento requires a slightly different approach compared to typical options like dropdown and text fields. By using an associative array with option_id and value, you can successfully attach files to products within your shopping cart.

Remember, before setting these options, ensure that:

  • You have properly uploaded the file to your server.
  • The file is correctly named or referenced in the value parameter of the options array.

For more detailed information on customizing Magento’s checkout process and handling file uploads, I recommend exploring our comprehensive guide on extending Magento checkout functionality.

Checkout Catalog Configuration

Our network