1

I am facing some weird issue in the magento2 collection.

function to get item collection

public function getNewItems()
{
    if ($this->_productsCache === null)
    {
        $items = $this->promoRegistry->getLimits();

        $groups = $items['_groups'];
        unset($items['_groups']);

        if (!$items && !$groups) {
            $this->_productsCache = false;

            return false;
        }

        $allowedSku = array_keys($items);
        foreach ($groups as $rule) {
            $allowedSku = array_merge($allowedSku, $rule['sku']);
        }

        $products = $this->_productFactory->create()->getCollection()
            ->addAttributeToSelect(['name', 'small_image', 'status', 'visibility'])
            ->addFieldToFilter('sku', ['in' => $allowedSku])
        ;

        foreach ($products as $key => $product) {
            if (!in_array($product->getTypeId(), $this->_allowedTypes)) {
                $this->promoMessagesHelper->showMessage(__(
                    "We apologize, but products of type <strong>%1</strong> are not supported",
                    $product->getTypeId()
                ));

                $products->removeItemByKey($key);
            }

            if ($product->getTypeId() == 'simple' && (!$product->isInStock() || !$product->isSalable()
                || !$this->promoCartHelper->checkAvailableQty($product, 1))
            ) {
                $this->promoMessagesHelper->addAvailabilityError($product);

                $products->removeItemByKey($key);
            }

            foreach ($product->getProductOptionsCollection() as $option) {
                $option->setProduct($product);
                $product->addOption($option);
            }
        }
        $allowedSku = "'" . str_replace(",", "','", implode(',',$allowedSku)) . "'";
        $products->getSelect()->order(new \Zend_Db_Expr("FIELD(sku,$allowedSku)"));

        if (sizeof($products) > 0) {
            $this->_productsCache = $products;
        }
        else {
            $this->_productsCache = false;
        }
    }

    return $this->_productsCache;
}

Here the issue is that when i print collection data it showing in right ordering. But when i try to render it render in different order.

when $products->getData

 Array
(
    [0] => Array
        (
            [entity_id] => 3695
            [attribute_set_id] => 4
            [type_id] => simple
            [sku] => 1059005
            [has_options] => 0
            [required_options] => 0
            [created_at] => 2018-01-17 16:06:33
            [updated_at] => 2019-06-26 13:58:53
            [is_salable] => 1
        )

    [1] => Array
        (
            [entity_id] => 4026
            [attribute_set_id] => 4
            [type_id] => simple
            [sku] => 1059010
            [has_options] => 0
            [required_options] => 0
            [created_at] => 2018-01-17 16:13:17
            [updated_at] => 2019-06-26 13:59:55
            [is_salable] => 1
        )

    [2] => Array
        (
            [entity_id] => 6283
            [attribute_set_id] => 4
            [type_id] => simple
            [sku] => 1049046
            [has_options] => 0
            [required_options] => 0
            [created_at] => 2019-04-23 13:05:24
            [updated_at] => 2019-06-26 14:02:46
            [is_salable] => 1
        )

    [3] => Array
        (
            [entity_id] => 3897
            [attribute_set_id] => 4
            [type_id] => simple
            [sku] => 1059008
            [has_options] => 0
            [required_options] => 0
            [created_at] => 2018-01-17 16:10:32
            [updated_at] => 2019-06-26 13:59:32
            [is_salable] => 1
        )
)

when foreach through each item

    $products = $block->getItems();
foreach ($products as $product) {
    echo $product->getId().'<br>';
}

4026
3695
3897
6283

Notice ordering of Id Here in foreach printing

4
  • did you find any solution for this? even I am facing same issue
    – Hiren Soni
    Jun 27, 2019 at 9:54
  • yes for now i did it loop through $collection->getData() and create object in loop.Not the right way to do Jun 28, 2019 at 7:18
  • 1
    Thank you for response, even I tried same way. Actually I was using helper object to get collection. So tried directly with block and it works. However its weird issue!!
    – Hiren Soni
    Jul 3, 2019 at 15:20
  • Please post solution if you find any.May many people facing this issue. Jul 5, 2019 at 5:44

2 Answers 2

4
  1. $collection->getData() will give you all the products.
  2. $collection->getItems() or foreach($collection as $items){...} will give you those products which have is_salable true.
  3. Let us check the above two points practically. I have written a custom script in which both $collection->getData() and $collection->getItems() are compared. The collection data which are not available in collection items are separated and exported in CSV. Also separated the available items in another CSV. In the CSVs, sku and is_salable values will be exported. You can compare both CSV and conclude. Please check the Git Link
  4. For sort order the collection, you can use setOrder(...) or addAttributeToSort(...) enter image description here enter image description here
2

I did face a similar issue with the Collections.
I fixed it by changing Collection to CollectionFactory.
Earlier I was using Anshu\Custom\Model\ResourceModel\CustomData\Collection in Constructor DI
To fix the issue, I switched to Anshu\Custom\Model\ResourceModel\CustomData\CollectionFactory
This may be because the Magento2 is loading the collection from the earlier loaded collection and when we use Factory, Magento2 creates a new collection object.

Your Answer

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

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