I am using Magento 2. I would like to display child product SKU in Main product page while user clicks on configurable Swatches.

I am trying to gather information from below file.


like below

<div class="swatch-opt"></div>
    require(["jquery", "jquery/ui", "swatchRenderer"], function ($) {
            jsonConfig: <?php /* @escapeNotVerified */ echo $swatchOptions = $block->getJsonConfig(); ?>,
            jsonSwatchConfig: <?php /* @escapeNotVerified */ echo $swatchOptions = $block->getJsonSwatchConfig(); ?>,
            mediaCallback: '<?php /* @escapeNotVerified */ echo $block->getMediaCallback() ?>'


    console.log('<?php echo $block->getJsonSwatchConfig(); ?>');

I am getting below output in console.


But I could not find SKU there. I would like to get SKU to display that in Main product Page.

2 Answers 2


If you use version 2.1.9 and below. open file vendor/magento/module-swatches/Helper/Data.php find private function getAllSizeImages(ModelProduct $product, $imageFile) add:

'sku' => $product->getSku()

all function code looke like:

private function getAllSizeImages(ModelProduct $product, $imageFile) {
return [
 'large' => $this->imageHelper->init($product, 'product_page_image_large')
 'medium' => $this->imageHelper->init($product, 'product_page_image_medium')
 'small' => $this->imageHelper->init($product, 'product_page_image_small')
     'sku' => $product->getSku(),

well be callback SKU.

more information: https://forum.magentochina.org/t/sku/33


Out of the box, when choosing options, e.g. the images and the price will change, but not SKU.

In order to display the SKU of the selected product, you can create a Plugin (Interceptor) for Magento\ConfigurableProduct\Block\Product\View\Type\Configurable::getJsonConfig() where you need to add missing SKU for each simple product.

Switching SKU when selecting an option can be implemented in vendor/magento/module-swatches/view/base/web/js/swatch-renderer.js and vendor/magento/module-configurable-product/view/frontend/web/js/configurable.js depending on the type of attribute.

I made free VCT Change SKU Dynamically module that solve this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.