1

On product listing page I have $_productCollection = $block->getLoadedProductCollection(); if I call getData on $_productCollection I get different array than I get in foreach loop. Does anyone have any idea about this.

Before foreach loop data looks like

Array
(
    [0] => Array
        (
            [row_id] => 1026
            [entity_id] => 1026
            [created_in] => 1
            [updated_in] => 2147483647
            [attribute_set_id] => 4
            [type_id] => simple
            [sku] => 47-62-532-11
            [has_options] => 0
            [required_options] => 0
            [created_at] => 2015-08-08 21:43:58
            [updated_at] => 2020-05-11 17:37:25
            [pim_updated_at] => 2019-11-02 22:29:43
            [status] => 1
            [price] => 3.5700
            [tax_class_id] => 5
            [final_price] => 3.5700
            [minimal_price] => 3.5700
            [min_price] => 3.5700
            [max_price] => 3.5700
            [tier_price] => 
            [most_view] => 9
            [is_salable] => 1
        )

    [1] => Array
        (
            [row_id] => 1028
            [entity_id] => 1028
            [created_in] => 1
            [updated_in] => 2147483647
            [attribute_set_id] => 4
            [type_id] => simple
            [sku] => 47-62-534-10
            [has_options] => 0
            [required_options] => 0
            [created_at] => 2015-08-08 21:44:08
            [updated_at] => 2020-05-11 05:18:23
            [pim_updated_at] => 2019-11-02 22:29:43
            [status] => 1
            [price] => 3.5700
            [tax_class_id] => 5
            [final_price] => 3.5700
            [minimal_price] => 3.5700
            [min_price] => 3.5700
            [max_price] => 3.5700
            [tier_price] => 
            [most_view] => 1
            [is_salable] => 1
        )

    [2] => Array
        (
            [row_id] => 1027
            [entity_id] => 1027
            [created_in] => 1
            [updated_in] => 2147483647
            [attribute_set_id] => 4
            [type_id] => simple
            [sku] => 47-62-532-14
            [has_options] => 0
            [required_options] => 0
            [created_at] => 2015-08-08 09:44:03
            [updated_at] => 2020-05-11 06:42:05
            [pim_updated_at] => 2019-11-02 22:29:43
            [status] => 1
            [price] => 3.4400
            [tax_class_id] => 5
            [final_price] => 3.4400
            [minimal_price] => 3.4400
            [min_price] => 3.4400
            [max_price] => 3.4400
            [tier_price] => 
            [most_view] => 1
            [is_salable] => 1
        )

    [3] => Array
        (
            [row_id] => 1025
            [entity_id] => 1025
            [created_in] => 1
            [updated_in] => 2147483647
            [attribute_set_id] => 4
            [type_id] => simple
            [sku] => 47-62-532-10
            [has_options] => 0
            [required_options] => 0
            [created_at] => 2015-08-08 13:43:53
            [updated_at] => 2020-05-11 06:11:17
            [pim_updated_at] => 2019-11-02 22:29:43
            [status] => 1
            [price] => 3.5700
            [tax_class_id] => 5
            [final_price] => 3.5700
            [minimal_price] => 3.5700
            [min_price] => 3.5700
            [max_price] => 3.5700
            [tier_price] => 
            [most_view] => 1
            [is_salable] => 1
        )

)

So, inside for each first product should be '47-62-532-11' but I am getting '47-62-534-10' SKU as the first element.

1 Answer 1

0

When calling getData() you will call\Magento\Framework\Data\Collection\AbstractDb::getData and this is how it looks:

/**
 * Get all data array for collection
 *
 * @return array
 */
public function getData()
{
    if ($this->_data === null) {
        $this->_renderFilters()->_renderOrders()->_renderLimit();
        $select = $this->getSelect();
        $this->_data = $this->_fetchAll($select);
        $this->_afterLoadData();
    }
    return $this->_data;
}

It will set filters, order and limit. Then fetch the data based on the current select.

When you foreach through the collection it will use \Magento\Framework\Data\Collection::getIterator which will create an \ArrayIterator based on$_items`.

So you use two different properties of the collection and they might be sorted differently. If you want the same data as in the foreach you can use $_productCollection->getItems() which will return $_items.

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.