2

This looks very simple but I have been pulling my hair on this for the last hour. I just want to load products data from a PHP array that looks like this:

array
  0 => 
    array
      'sku' => string '1HC220'
      'qty' => string '2'
  2 => 
    array
      'sku' => string '4LANT'
      'qty' => string '2'

The piece of code:

foreach ($products_arr as $row) :
    $sku        = trim($row['sku']);

    $product    = Mage::getModel('catalog/product')->setStore($store_id)
        ->getCollection()
        ->joinField('qty',
            'cataloginventory/stock_item',
            'qty',
            'product_id = entity_id',
            '{{table}}.stock_id = 1',
            'left'
        )
        ->addFieldToFilter('sku', array('eq' => $sku))
        ->getFirstItem()
        ;
        (...)
endforeach;

But the first product in the array always returns NULL. If I var_dump $products_arr I have X products, but in the loop the first is never returned.

If I load the product individually, it is loading, but not as a first row of the array.

I know I am missing something, but can't figure out what.

5
  • How your $products_arr looks? Print the result here
    – Priyank
    Commented Nov 20, 2017 at 8:54
  • I updated the code and added the array. Thank you @Priyank
    – feub
    Commented Nov 20, 2017 at 9:02
  • @Priyank no still the same problem. SO strange...
    – feub
    Commented Nov 20, 2017 at 9:31
  • What happens when you remove the ->joinField() from your collection? I ask because maybe the missing item is out of stock. Commented Nov 20, 2017 at 12:42
  • @seanbreeden no, it doesn't change. Whatever the first product is, it is the one that will result in NULL. In my exemple, if in the array I put the row with the SKU '4LANT' as the first in the array, this will be the one not loading. It really is the first row of the array that is 'missing'.
    – feub
    Commented Nov 20, 2017 at 13:10

2 Answers 2

1

Add per your code if I have this data it works fine for me

    $products_arr = [0 => ['sku' => 'Test40', 'qty' => 1], 1 => ['sku' => 'Test39', 'qty' => 1]];
    foreach ($products_arr as $data){
        $product = Mage::getModel('catalog/product')->setStore(1)
            ->getCollection()
            ->joinField('qty',
                'cataloginventory/stock_item',
                'qty',
                'product_id = entity_id',
                '{{table}}.stock_id = 1',
                'left'
            )
            ->addFieldToFilter('sku', array('eq' => $data['sku']))
            ->getFirstItem();
        echo "<pre>";print_r($product->getData());die;
    }

Make sure the store ID you are assigning has the following product.

1
  • It is still the same for me. The first product is NULL. What I didn't precise is that the array is built reading a CSV file line by line. A row of the CSV file consists of: SKU;QUANTITY;PRICE But as you can see the array is well formed, yet I get the first product impossible de load. I tell you, it is very strange. The dirty work around I ended up doing is adding an empty first row in the array that I don't load in the foreach.
    – feub
    Commented Nov 20, 2017 at 12:10
0

It was not Magento related at all.

When building the array from the CSV file, there was some hidden characters in the SKU that I escaped using $field = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $row['sku']); I still don't get why those characters were added only in the first element of the array though.

Working now. Thank you guys

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.