cancel
Showing results for 
Search instead for 
Did you mean: 

Cant get Products from some Categories, others work

Cant get Products from some Categories, others work

Setup:

  • Magento Community Edition
  • Magento ver. 2.1.7

I can get the products from a category with the code below, but when I enter some specific id´s it returns no Products.

example:

  • When I choose the products from category with id 2 I get 2 products -> everything fine
  • When I choose the id 156 (a category that exists and 2 products belong to this category) nothing is returned...

I kinda become desperate. Hope you understand my Problem.

<?php
    namespace MyApp\Links\Block;
    /**
    * Products block
    */
    class Products extends \Magento\Framework\View\Element\Template
    {
        protected $categoryFactory;

            public function __construct(                    \Magento\Framework\View\Element\Template\Context $context,                    \Magento\Catalog\Model\CategoryFactory $categoryFactory
            ) {                    $this->categoryFactory = $categoryFactory;                    parent::__construct($context);
            }

            public function getCategoryProduct($categoryId)
            {                    $category = $this->categoryFactory->create()->load($categoryId)->getProductCollection()->addAttributeToSelect('*');
                    return $category;
            }
}
?>

edit: i can make the products in the category visible by adding a line in SQL directly... i can insert a line in catalog_category_product_index with the right category_id. Then the product shows up. But when i run an reindex with Magento it "fixed" this and my products are gone. No idea what to do


edit2 i just removed all categories. still the index table is generated wrong... catalog_category_productcatalog_category_product_index

When i understand those tables right product 7 is in category 2 and product 8 in category 3 (pic1).

But why is the index table trying to tell me both belongs to category 2 (pic2)? Already reindex in Magento2... did i mess up sth really bad?

 

Hey,

i just copied the text from stackexchange (https://magento.stackexchange.com/questions/200579/cant-get-products-from-some-categories-others-wor...).

I cant find an solution there, maybe someone here can help me .-)