How to access custom options of a product in magento from observer event

How to access custom options of a product in magento from observer event

October 3, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to access custom options of a product in magento from observer event

Accessing Custom Product Options in Magento from an Observer Event

In Magento, you can access custom options of a product within an observer event using the getOptions() method provided by the Mage_Catalog_Model_Product class. In this post, I’ll guide you through accessing and storing location coordinates associated with custom options when adding a product to the cart.

Retrieving Custom Options from the Product Object

When dealing with observer events in Magento, the first step is to retrieve the relevant data from the event object passed to your method. In this case, we need to get access to the custom options of the product being added to the cart.

public function getLocationCoords(Varien_Event_Observer $observer)
{
    // Retrieve the product being updated from the event observer
    $product = $observer->getEvent()->getProduct();

    foreach ($product->getOptions() as $option) {
        // Access custom option data here
    }
}

Accessing Custom Option Values

To access the values of your custom options, you can use the getTitle() method for text fields and other relevant methods specific to each option type. In your case, since you have two text fields (Location To and Location From), you can retrieve their titles using:

foreach ($product->getOptions() as $option) {
    if ($option->getType() === 'field' && in_array($option->getTitle(), ['Location To', 'Location From'])) {
        $location = $option->getTitle();
        Mage::log("Custom Option Title: " . $location);
    }
}

Reverse Geocoding Custom Option Values

Once you have retrieved the location values, you can proceed to reverse geocode them to obtain latitude and longitude coordinates. There are various APIs available for this purpose, such as the Google Maps Geocoding API or OpenStreetMap Nominatim service.

To use these services in your Magento module, you would need to make an external HTTP request from within your observer method after extracting the location values:

foreach ($product->getOptions() as $option) {
    if ($option->getType() === 'field' && in_array($option->getTitle(), ['Location To', 'Location From'])) {
        $location = $option->getTitle();
        // Reverse geocode the location using a third-party API
        // Example: Use Guzzle or cURL to send an HTTP request and parse the JSON response
    }
}

Storing Custom Option Coordinates with the Product

To store the coordinates obtained from reverse geocoding alongside the product, you can use Magento’s resource model to update custom fields or add attributes specific to your module. For simplicity, let’s assume you have created two new text attributes in your module: latitude and longitude.

foreach ($product->getOptions() as $option) {
    if ($option->getType() === 'field' && in_array($option->getTitle(), ['Location To', 'Location From'])) {
        $location = $option->getTitle();
        // Reverse geocode the location using a third-party API
        // ...

        // Store coordinates as custom fields or attributes
        if ($location == 'Location To') {
            $product->setLatitude($latitude);
        } else {
            $product->setLongitude($longitude);
        }
    }
}

Saving Custom Fields and Attributes

To ensure the updated location coordinates are saved with the product, you need to call the save() method on the $product object:

foreach ($product->getOptions() as $option) {
    // ...
}

$product->save();

Conclusion

In this post, we’ve explored how to access custom options of a product within an observer event in Magento. By retrieving these options from the product object and reverse geocoding their values, you can store location coordinates associated with your products.

Don’t forget that working with third-party APIs for reverse geocoding requires proper error handling and consideration of rate limits and API usage policies. To learn more about Magento development best practices, check out Laravel Company, where we provide comprehensive guides on Magento customization and extension development.

Happy coding!

Checkout Catalog Extensions

Our network