Magento set products category sort number programmatically

Magento set products category sort number programmatically

September 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento set products category sort number programmatically

Setting Magento Product Category Sort Order via Programmatic Import

As you complete your oscommerce-to-magento data migration project for your client, setting the product sort order within each category is the final critical step. In this guide, I’ll walk you through how to achieve this programmatically.

Step 1: Retrieve Sort Order from oscommerce Table

Before we start manipulating Magento’s data model, first extract the desired sort order values from your existing oscommerce database table. This will serve as the basis for setting the display order in Magento:

// Assuming you have a PDO connection $pdo
$sql = "SELECT product_id, category_sort_order FROM oscommerce_products";
$results = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);

Step 2: Load Product and Category Objects

Next, load the corresponding Magento product and category objects using their IDs. Magento uses a repository pattern for object management:

// Get Magento's product repository instance
$productRepository = $this->getProductRepository();

// Get Magento's category repository instance
$categoryRepository = $this->getCategoryRepository();

Step 3: Update Category Product Sort Order

With the required objects loaded, you can now update the category sort order for each mapped product. This is done by manipulating the Magento\Catalog\Model\Category\Product entity:

foreach ($results as $row) {
    // Load the Magento product object
    $product = $productRepository->getById($row['product_id']);

    // Load the category object using oscommerce category ID (map this to Magento's cat_id)
    $category = $categoryRepository->get('2');  // Replace '2' with your actual category ID

    // Create a new association between product and category
    $categoryProduct = $this->productCategoryRelationsFactory->create();
    $categoryProduct->setProductId($row['product_id']);
    $categoryProduct->setCategoryId(2); // Same as above
    $categoryProduct->setPosition($row['category_sort_order']);  // Set the sort order

    // Save the category-product association
    $this->categoryProductRelationsRepository->save($categoryProduct);
}

Remember to replace '2' with your actual Magento category ID and map oscommerce’s category_id field appropriately.

Conclusion: Efficient Data Migration in Magento

Setting product sort order programmatically during data migration is a routine task for experienced developers. By combining database queries, object loading, and manipulation of the product_category_relations table, you can achieve this efficiently within your custom import script.

As you continue building out your Magento solutions at Laravel Company, remember that understanding these core data models is key to creating maintainable and optimized e-commerce platforms for your clients.

Don’t hesitate to reach out if any further assistance is required, or explore our other resources on Magento extensions development to enhance your expertise in the Magento ecosystem.

Catalog Database Configuration Orders

Our network