I have a multi-store website and have more than 30 categories. I have a requirement like updating the product position.

For example, I have a category 'XYZ' which contains more than 500+ products available. this category is enabled for default scope and other store view scopes such as 'A', 'B', and 'C'(I'm taking A, B, and C as an example of different store views).

So I want to update the product position of every product in the 'XYZ' category only for the 'B' store view (while updating, other store view product positions should not be changed).

I have tried using the repository/category model object to update the product position for a specific store view, but it takes so long if we are updating almost 100 products.

I also tried setProductsPosition($productPosition) method to update the product, but this is updating the product position for all the store views.

Does anyone have any idea how to update the product position only for a specific category of a specific store view?

1 Answer 1

    // Get the product object by SKU 
$product = $obj->get('Magento\Catalog\Model\Product')->loadByAttribute('sku', $sku);
    // Check if the product exists if ($product) {
        // Define the position
        $position = your_new_position;
        // Define the category ID
        $categoryId = your_category_id;
        // Get the product's existing category IDs
        $categoryIds = $product->getCategoryIds();
        // Check if the category is already assigned to the product
        if (in_array($categoryId, $categoryIds)) {
            // Update the product's position in the category for a specific store
            $product->setStoreId(your_store_id)->setCategoryIds([$categoryId => $position])->save();
            echo "Product position updated successfully.";
        } else {
            echo "Product is not assigned to the specified category.";
        } } else {
        echo "Product not found."; }

This approach ensures that you update the product position for a specific category and store view correctly within Magento's data structure.

  • In catalog_category_product table, there is no column for store id
    – mage
    Commented Oct 4, 2023 at 14:27
  • check my updated answer Commented Oct 10, 2023 at 10:21
  • Thank you for your response and effort on this. I have checked the database and we can't add product position category-wise for different store views as the Magento architecture goes that way only.
    – mage
    Commented Nov 12, 2023 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.