0

I am trying to retrieve the SKU of the first simple product of configurable product as soon as a certain SKU type is found to be "configurable" type. The code below helps find out if the main SKU is configurable or not. I need to find out the SKU of the first simple product of this configurable product. Please advice.

if($_product->getTypeId() == 'configurable'){
 echo 'This is a configurable product';
 // I need to retrieve the SKU of the first simple product for this configurable product and return the SKU
}else{
 echo 'This is not a configurable product';
}

1 Answer 1

2

You should be able to use this:

$_product->getTypeInstance()->getUsedProductCollection($_product)->getFirstItem()->getSku()
6
  • let me try and let you know. Apr 9, 2020 at 11:52
  • Hi, getting this error when used your code: Magento\ConfigurableProduct\Model\Product\Type\Configurable\Interceptor::getUsedProductsCollection() Apr 9, 2020 at 12:07
  • sorry, typo. method should be: ->getUsedProductCollection() (without "s") Apr 9, 2020 at 12:13
  • No problem, I think we are getting close to the solution. This error comes up. Uncaught ArgumentCountError: Too few arguments to function Magento\ConfigurableProduct\Model\Product\Type\Configurable\Interceptor::getUsedProductCollection(), 0 passed in Apr 9, 2020 at 12:46
  • edited my answer Apr 9, 2020 at 12:53

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.