I wanna sort the child products of configurable according to display swatches. I wanna sort the child products in product page.

Product page swatches display like this Green,White,Black When I use my below code its fetching child products but not sort according to swatches. How swatches sorted and display, and what should I change in my below code ?

$childItems = [];
        $_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $_product = $_objectManager->get('Magento\Framework\Registry')->registry('current_product');
            if($_product->getTypeId() == "configurable"){
            $imageBuilder= $_objectManager->get('Magento\Catalog\Model\Product\Image\UrlBuilder');
                $allProducts = $_product->getTypeInstance()->getUsedProducts($_product, null);
                foreach ($allProducts as $product) {
                    $productStockObj =  $objectManager->get('Magento\CatalogInventory\Api\StockRegistryInterface')->getStockItem($product->getId());
                   if ($productStockObj->getIsInStock()){
                    $childItems[] = $product->getSku();

When I run above code it displays the child array like this sku-white,sku-black,sku-green

I want sort the child items according display swatches


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.