How do I load a Magento product and find stores it's assigned to

How do I load a Magento product and find stores it's assigned to

July 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How do I load a Magento product and find stores it's assigned to

Finding Magento Stores for a Product: A Complete Guide

As a seasoned e-commerce developer, I’ve often faced challenges in reverse engineering orders across multiple stores within a single Magento backend. Recently, I had to determine the actual store behind an order from an Ebay Extension. Let me walk you through my approach and share some best practices.

Understanding the Challenge

Our client runs several stores from one Magento backend, selling various products via a single eBay merchant account. The goal was to load orders as they appear on eBay, find the attached items, and then identify which store(s) those items are used in other than the eBay or admin store.

My Initial Attempt

I started by loading the order collection and iterating through each order:

foreach($collection->getItems() as $order):
   // Load correct order information
   $order = Mage::getModel('sales/order')->loadByIncrementID($order->getIncrementId());
   $items = $order->getItemsCollection();
   foreach($items as $item) {
      // Get the actual item
      $item = Mage::getModel('catalog/product')->load($item->getItemId());
      // Do something to get other store IDs
   }
endforeach;

While this gave me a starting point, I realized it only provided the items associated with the current order. To find all stores an item is assigned to, we need to dig deeper into the product and category data.

The Correct Approach

To achieve this, let’s update our strategy:

  1. Load the product using the load method of the catalog model.
  2. Get the category IDs associated with that product.
  3. Iterate through those categories to find all store IDs they’re assigned to.

Here’s a more detailed example:

foreach($collection->getItems() as $order):
   // Load correct order information
   $order = Mage::getModel('sales/order')->loadByIncrementID($order->getIncrementId());
   foreach($order->getAllVisibleItems() as $item) {
      // Get the actual item
      $product = Mage::getModel('catalog/product')->load($item->getProductId());
      // Load category IDs for this product
      $categoryIds = $product->getCategoryIds();
      // Initialize an array to store all store IDs
      $allStoreIds = [];
      foreach($categoryIds as $categoryId) {
         // Load the category model
         $category = Mage::getModel('catalog/category')->load($categoryId);
         // Get all store IDs for this category (including admin and eBay)
         $storeIds = array_merge($category->getStoreId(), [0, MAGE_CORE_MODE_EBAY]);
         // Merge with previous results
         $allStoreIds = array_unique(array_merge($allStoreIds, $storeIds));
      }
      // Now you have all store IDs for this product across your Magento installation
   }
endforeach;

Key Takeaways

  1. Use the getAllVisibleItems() method to retrieve all visible items in an order.
  2. Load the product using the product ID from the item object.
  3. Iterate through category IDs associated with each product.
  4. For each category, get all store IDs (including admin and eBay).
  5. Merge all store IDs into a single array and remove duplicates.

Conclusion

Finding which stores an order or its items are truly connected to in Magento requires a careful approach that involves loading the correct data models and iterating through relevant collections. By following this guide, you should be able to accurately determine the non-eBay/eBay stores associated with any given order or product.

Remember, the key is to load the necessary objects correctly and then extract the required information from them systematically.

Happy coding!

Learn more about advanced Magento development techniques on our blog at LaravelCompany.com

Catalog Extensions Admin Orders

Our network