0

Hi I have got the product collection here but am trying to push $sku value in $item, but seems like its not working, anything wrong am doing, please help

$items = array(); // trying to push sku data in this array
foreach($productCollection as $productCust){
 $sku = $productCust->getSku();// this is working fine
 array_push($items, $productCust->getSku());
}

Update

$items = [];
$productCollection = $this->collectionFactory->create();
$Test = $productCollection->getItems();
foreach($Test as $productCust){
 $sku = $productCust->getSku();// this is working fine
 $items[] = $sku;
}

2 Answers 2

0
$items = [];
$products = $productCollection->getItems();
foreach($products as $product){
    $sku = $product->getSku();
    $items[] =  $sku;
}
3
  • not working bro, check the update code i have pasted after using you ans as a reference
    – Pramod
    Mar 8, 2022 at 14:38
  • Hi, I suggest using a debugger such as xdebug to step through your code. Or add loggers (devdocs.magento.com/guides/v2.4/config-guide/log/…) to your code. If not, at least try doing a plain var_dump on your $items Pretty sure there's a typo somewhere or you are trying to view some other piece of code/variable.
    – Hackinet
    Mar 8, 2022 at 14:44
  • i have used logger, but removed that before posting the code
    – Pramod
    Mar 8, 2022 at 14:46
0
$items = [];
$productCollection = $this->collectionFactory->create();
$products = $productCollection->getItems();
foreach($products as $product){
 $sku = $product->getSku();
 $items[] = $sku;
}

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.