How to update custom options programatically in magento?

How to update custom options programatically in magento?

September 6, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to update custom options programatically in magento?

Updating Custom Options Programatically in Magento

In Magento, managing custom options for your products is an essential part of offering tailored experiences to your customers. However, updating these options manually can be time-consuming when dealing with a large number of SKUs. This guide will walk you through the process of updating custom options programmatically using PHP and the Magento API.

Understanding Custom Options in Magento

Before diving into code, let’s briefly understand how custom options are structured in Magento:

  • Each product has a collection of catalog_product_entity_varchar records that store option data.
  • The option_id field is used to uniquely identify each option across all products.
  • To update an option for a specific product, you need to locate the corresponding record by matching the product_id, option_id, and store_id.

Setting Up Your Magento Environment

To begin, ensure that your development environment is correctly configured:

  1. Install Xdebug or any other debugging tool to enable error reporting.
  2. Create an index.php file in your project’s root directory for direct API access.

Here’s a simple script to get you started:

<?php
require_once 'app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP_PROD);

Accessing the Magento API

Magento provides a RESTful API that allows you to interact with your store programmatically. To update custom options, you’ll need to use the catalog_product_option resource.

First, generate an access token:

  1. Open a web browser and navigate to http://your_magento_url/index.php/rest/V1/integration/admin/token.
  2. Set the following headers:
    • Content-Type: application/json
    • Authorization: Basic <base64_encoded_username_password>

Replace <base64_encoded_username_password> with your admin username and password encoded in base64 format.

Updating Custom Options via API

Now, let’s create a PHP script to update an existing custom option:

<?php
require_once 'app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP_PROD);

// Load the Magento application
$app = $bootstrap->createApplication('Magento\\Framework\\App');

$token = "your_access_token_here"; // Replace with your access token

$optionId = 123; // Replace with the option ID you want to update
$productId = 456; // Replace with the product ID associated with the option
$storeId = 0; // Store ID (0 for default store)

$data = [
    'title' => 'Updated Option Title',
    'is_require' => 1,
    'sort_order' => 2,
];

// Update the option
$ch = curl_init("http://your_magento_url/index.php/rest/V1/catalog_product_option/{optionId}");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    "Authorization: Bearer {$token}"
]);

$response = curl_exec($ch);

// Check the response
if ($httpCode == 200) {
    echo "Option updated successfully.";
} else {
    echo "Error updating option. Status code: " . $httpCode;
}

Best Practices for Bulk Updates

When dealing with a large number of options, it’s essential to follow these best practices:

  1. Bulk update in chunks: Instead of processing all options at once, divide them into smaller batches and process each batch sequentially.
  2. Use index.php directly: Calling index.php directly allows you to interact with the Magento API without loading the entire application. This approach is more efficient for bulk updates.

Conclusion

Updating custom options programmatically in Magento requires a solid understanding of the product entity structure and the Magento API. By following this guide, you can now automate your custom option management process using PHP scripts and the Magento RESTful API.

Remember to replace placeholders like your_magento_url, your_access_token_here, and option/product IDs with actual values from your store.

For more advanced use cases involving custom options, consider exploring Magento’s extension marketplace (https://laravelcompany.com) for third-party modules that provide enhanced functionality.

Happy coding! Let me know if you have any further questions.

Catalog API Configuration Customers

Our network