Magento 2.1 – Why SKU Does Not Change Dynamically in Configurable Product View

configurable-productmagento-2.1sku

When you change an attribute in a configurable product the product image changes dynamically but the SKU doesn't change.

Also, if you add a configured product to your basket it gives you the image of the configurable product (and not of the simple product) which could be very confusion for customers.

Is there any fix for this issue(s)?

Best Answer

I did that once for Magento 2.0, don't have the code available but can show you where you need to make changes:

  • Write a Plugin for Magento\ConfigurableProduct\Block\Product\View\Type\Configurable::getJsonConfig() and add the SKUs to the result:

    $config['skus'] = [];
    foreach ($subject->getAllowProducts() as $simpleProduct) {
        $config['skus'][$simpleProduct->getId()] = $simpleProduct->getSku();
    }
    
  • Extend Product.Config from app/code/Magento/ConfigurableProduct/view/adminhtml/web/js/configurable.js using RequireJS. A good place to add the code to change the displayed SKU is reloadPrice(). You will have access to the SKUs via this.config.skus