1

I am using Magento2.2. I want to display all enabled child products SKUs in product page(view.phtml) of parent configurable product like

$SKU = $this->getProduct()->getSku();

Please give some tips to do it.

My View.phtml code

<?php
error_reporting(1);
    $_config = $this->helper('Sm\Theme\Helper\Data');
    $detailStyle = $_config->getProductDetail('detail_style');
    $thumsStyle = $_config->getProductDetail('thumbs_style');

    if($detailStyle == 'detail-1'){
        include (dirname(__FILE__).'/detail-style/detail-1.phtml');
    } else {
        include (dirname(__FILE__).'/detail-style/detail-sidebar.phtml');
    }
    $SKU = $this->getProduct()->getSku();

$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
$productTypeInstance = $product->getTypeInstance();
$usedProducts = $productTypeInstance->getUsedProducts($product);

//echo $product->getId(); //Main configurable product ID
//echo $product->getName(); //Main Configurable Name

foreach ($usedProducts  as $child) {
    echo $child->getSku()."</br>"; //Child Product Sku    
}

?>
2
  • Do you want to show all enabled only virtual product skus in single line comma seperated?
    – Meetanshi
    Nov 22, 2017 at 5:18
  • no may be in a separate array or using loops like this $configurableProduct = Mage::getModel('catalog/product')->load(1); //here 1 is productId $childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$configurableProduct); foreach($childProducts as $child) { echo $child->getSku(); } Nov 22, 2017 at 5:20

1 Answer 1

2

Please use below code and put in view.phtml file and print as per your requirement

    <?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
    $productTypeInstance = $product->getTypeInstance();
    $usedProducts = $productTypeInstance->getUsedProducts($product);

    //echo $product->getId(); //Main configurable product ID
    //echo $product->getName(); //Main Configurable Name

    foreach ($usedProducts  as $child) {
        echo $child->getSku()."</br>"; //Child Product Sku    
    }

Thanks

6
  • Thank you for the Reply. But it is not working. Fatal error: Uncaught Error: Call to a member function get() on null in /opt/lampp/htdocs/website/app/design/frontend/Sm/theme/Magento_Catalog/templates/product/view.phtml:23 Nov 22, 2017 at 5:44
  • can you please share view.phtml code?
    – Meetanshi
    Nov 22, 2017 at 5:45
  • I have updated code. Please check
    – Meetanshi
    Nov 22, 2017 at 5:47
  • please try $PType = $this->getProduct()->getTypeId(); it will return type id and than you need to check $PType == 'simple' or like
    – Meetanshi
    Nov 22, 2017 at 5:57
  • Thank you sooooo much..... Thank you for your support.... I can get product type.. Nov 22, 2017 at 6:02

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.