How To

How to Assign Product Position for a Specific Category in Magento 2?

Hello Magento Friends,

In this blog, we will learn about How to Assign Product Position for a Specific Category in Magento 2?

As you know, a category is a collection of products of a similar type. The admin can determine how the products can be displayed on the category page. In Magento 2, if you want to change the position of products for a specific category, you can accomplish it using the below-given steps.

Steps to Assign Product Position for a Specific Category in Magento 2:

Step 1: Create a file in your Magento root directory and add the code as given below

<?php 

use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
 
$bootstraps = Bootstrap::create(BP, $_SERVER);
$object_Manager = $bootstraps->getObjectManager();
 
$app_state = $object_Manager->get('\Magento\Framework\App\State');
$app_state->setAreaCode('frontend');
 
try
{
    $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
    $objectManager = $bootstrap->getObjectManager();
    $appState = $objectManager->get('\Magento\Framework\App\State');
    $appState->setAreaCode('frontend');

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $categoryId = 16;
    $category = $objectManager->get('\Magento\Catalog\Model\CategoryFactory')->create()->load($categoryId);
    $products = $category->getProductsPosition();
       
    // change position for specific product :
    $productId = 72;
    $newposition = 45;
    
    foreach($products as $id=>$value)
    {
            if($id == $productId)
            {
                $products[$productId] = $newposition;
            }
    }

    // change position for all products 
    $newposition = 45;
    foreach($products as $id=>$value)
    {
                $products[$id] = $newposition;
    }

    $category->setPostedProducts($products);    
    $category->save();

}
catch(\Exception $e)
{
    print_r($e->getMessage());
}

Output:

Using this, you can assign positions to a category using a specific product id. You can also assign positions to more than one product with this code. Based on your requirement, you can assign value for batch products or collections.  

Conclusion:

This way, you can Assign Product Positions for a Specific Category in Magento 2. Check out How to Load Products by Product ID and SKU in Magento 2. If you face any hardship in assigning the product position programmatically, you can freely ask me through the comment part. Share the article with your Magento friends and hold on with us to not miss out on our latest tutorials.

Happy Coding!

Click to rate this post!
[Total: 7 Average: 3.3]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

6 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago