EmicoEcommerce / Magento2Tweakwise-archived

Magento 2 module for Tweakwise integration
Other
9 stars 25 forks source link

A null pointer of a product inside a foreach loop #265

Closed epolish closed 1 year ago

epolish commented 1 year ago

Invalid null pointer

To fix a null pointer of a product inside a foreach loop

Environment

Steps to reproduce

  1. Install Magento from develop branch.
  2. Add some products to Tweakwise
  3. Remove the products in magento
  4. Enter that product names in a quck search on a magento frontend
  5. Catch the 500 http issue

Actual result

  1. The ajax quick search popup request returns 500 error

Expected result

To see valid filtered results

How to fix

Found the issue in vendor/emico/tweakwise/Model/Autocomplete/DataProviderHelper.php::getProductItems() line 147 The foreach loop should be like this

foreach ($response->getProductData() as $item) {
    $product = $productCollection->getItemById($item['id']);

     if (!$product) {
        continue;
    }

    $product->setData('tweakwise_price', $item['tweakwise_price']);

    $result[] = $this->productItemFactory->create(['product' => $product]);
}
Because a product can be null

$product = $productCollection->getItemById($item['id']);
$product->setData('tweakwise_price', $item['tweakwise_price']);