0

If I pull a list of products with the repository I am unable to get any custom attributes.

    $searchCriteria = $this->searchCriteriaBuilder->addFilter('type_id', 'simple', 'eq')->create();
    $simpleProdSearch = $this->productRepository->getList($searchCriteria);
    $simpleProds = $simpleProdSearch->getItems();

    foreach($simpleProds as $product)
    {
        $cats = $product->getCustomAttribute('category_ids'); // null
    }

If I load the product by ID then it works:

    foreach($simpleProds as $product)
    {
        $fullProduct = $this->productRepository->getById( $product->getId() );
        $cats = $fullProduct->getCustomAttribute('category_ids');
    }

But this loads the product twice. I am performing action on all products so I want this to be performant as possible. Can I add the category IDs to the full collection of products?

1 Answer 1

0

try below code

$cats= $product->getResource()->getAttribute('category_ids')->getFrontend()->getValue($product);
3
  • Isn't that doing the same thing and loading product again?
    – Guerrilla
    Commented Aug 17, 2017 at 2:18
  • @Guerrilla can you elaborate what you want.. you need to get product custom attribute value right? Commented Aug 17, 2017 at 2:24
  • Im trying to get the custom attribute data in the collection so I can loop over it quickly without database calls on each loop. Loading product again is a waste as I have already loaded it in intial collection.
    – Guerrilla
    Commented Aug 17, 2017 at 3:29

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.