Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8849915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:46:37+00:00 2026-06-14T12:46:37+00:00

I have this script to show the dynamic sku on select option, but i

  • 0

I have this script to show the dynamic sku on select option, but i cannot get working.

Is loading the correct sku, but on select nothing happen.

This code get list of sku on Javascript and update a div on select option for product on configurable product view.

<?php
$_product    = $this->getProduct();
$_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());
?>
<?php if ($_product->isSaleable() && count($_attributes)):?>
    <dl>
    <?php foreach($_attributes as $_attribute): ?>
        <dt><label class="required"><em>*</em><?php echo $_attribute->getLabel() ?></label></dt>
        <dd<?php if ($_attribute->decoratedIsLast){?> class="last"<?php }?>>
            <div class="input-box">
                <select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select" onchange="return changeSku(this);">
                    <option><?php echo $this->__('Choose an Option...') ?></option>
                  </select>
              </div>
        </dd>
    <?php endforeach; ?>
    </dl>
    <script type="text/javascript">
        var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);
    </script>

<?php endif;?>


<?php
$conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
$col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();

echo '<script type="text/javascript">';

echo '
document.observe("dom:loaded", function() {
  $("sku-container").update("<strong>Product Id: </strong> Select an option to display Product Id");
});
';
echo ' function changeSku(sel){';       

$itemId = array();           
foreach($col as $simple_product){
$itemId[] = array($simple_product->getSelectLabel() => $simple_product->getSku());
} 

//echo "<pre>";
//print_r($itemId);
//echo "</pre>";

foreach($itemId as $val){
 foreach($val as $k => $v){
echo "\n".'if(sel.options[sel.selectedIndex].value == "'.$k.'"){'."\n";
echo '$("sku-container").update("<strong>Product Id: </strong>'.$v.'");'. "\n";
echo '}';
    }
}

echo "\n".'if(sel.options[sel.selectedIndex].value == ""){'."\n";
echo '$("sku-container").update("<strong>Product Id: </strong> Select an option to display Product Id");'. "\n";
echo '}'; 

echo "}";
echo "\n</script>";
?>

I appreciate any help.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:46:38+00:00Added an answer on June 14, 2026 at 12:46 pm

    The script is almost correct except for $simple_product->getSelectLabel() being a wrong key. No such method/property exists in a simple product model. In order to make the script work, this key should be replaced with a right one – a Product Id. Utilizing product id, it is possible to find the sku of the product being selected.

    So, first of all you need to reorganize itemId array to make it a “productId => productSku” map:

    $productMap = array();
    foreach($col as $simpleProduct){
        $productMap[$simpleProduct->getId()] = $simpleProduct->getSku();
    }
    

    Then you need to change the “onchange” function call to pass Configurable’s attribute id to the changeSku() function. Thus the underlying logic is able to search appropriate simple product’s attributes.

    onchange="return changeSku(<?php echo $_attribute->getAttributeId() ?>, this);">
    

    And after that you need utilize configurable’s config in order to map selected simple product’s attribute id to the product id selected:

    function changeSku(confAttributeId, sel) {
        var productMap = <?php echo Mage::helper('core')->jsonEncode($productMap);?>;
        var selectedAttributeId = sel.options[sel.selectedIndex].value;
        if (selectedAttributeId) {
            var options = spConfig.config.attributes[confAttributeId].options;
            var productId = options.find(function (option) {return option.id == selectedAttributeId}).products[0]
            $("sku-container").update("<strong>Product Id: </strong>" + productMap[productId]);
        } else {
            $("sku-container").update("<strong>Product Id: </strong> Select an option to display Product Id");
        }
    }
    

    For your reference, below is the summary of how the whole template looks like (I’ve beautified it a little):

    <?php
    $_product    = $this->getProduct();
    $_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes());
    ?>
    <?php if ($_product->isSaleable() && count($_attributes)):?>
    <dl>
        <?php foreach($_attributes as $_attribute): ?>
        <dt><label class="required"><em>*</em><?php echo $_attribute->getLabel() ?></label></dt>
        <dd<?php if ($_attribute->decoratedIsLast){?> class="last"<?php }?>>
            <div class="input-box">
                <select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select"
                        onchange="return changeSku(<?php echo $_attribute->getAttributeId() ?>, this);">
                    <option><?php echo $this->__('Choose an Option...') ?></option>
                </select>
            </div>
        </dd>
        <?php endforeach; ?>
    </dl>
    <script type="text/javascript">
        var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);
    </script>
    
        <?php endif;?>
    
    <div id="sku-container"></div>
    
    <?php
    $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
    $col = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
    
    $productMap = array();
    foreach($col as $simpleProduct){
        $productMap[$simpleProduct->getId()] = $simpleProduct->getSku();
    }
    ?>
    
    <script type="text/javascript">
    
    document.observe("dom:loaded", function() {
      $("sku-container").update("<strong>Product Id: </strong> Select an option to display Product Id");
    });
    
    function changeSku(confAttributeId, sel) {
        var productMap = <?php echo Mage::helper('core')->jsonEncode($productMap);?>;
        var selectedAttributeId = sel.options[sel.selectedIndex].value;
        if (selectedAttributeId) {
            var options = spConfig.config.attributes[confAttributeId].options;
            var productId = options.find(function (option) {return option.id == selectedAttributeId}).products[0]
            $("sku-container").update("<strong>Product Id: </strong>" + productMap[productId]);
        } else {
            $("sku-container").update("<strong>Product Id: </strong> Select an option to display Product Id");
        }
    }
    </script>
    

    This will do the task you originally needed.


    Also note the following

    1. Your approach won’t work for a configurable product with two or more configurable attributes. For that product a final simple product is not known until a user selects values for all the select inputs. So an approach should be changed to check all the selects before outputting SKU.
    2. The code doesn’t consider a case, when user edits product configuration, rather than specifying configuration for a new product. You can go to editing mode from the Shopping Cart clicking the Edit link. In such a case all the select inputs will be pre-filled with previously chosen values. But the text will say “Select an option to display Product Id”. The script might also produce other Javascript errors in Edit mode. The code should be slightly modified in order to support Edit mode as well.
    3. The template is overfilled with logic. A Magento template should have only simple prints and foreach-iterations. All the methods like $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions() are better to be moved to block. This reduces code complexity.
      Hope it helps.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this jquery script which suppose to hide/show div element base on the
I have this code to show a map using the Virtual Earth API: <script
I have dynamic page. There are multiple checkboxes & I am using this script
I have this JQuery script: $('#searchinput').keypress(function() { $('#realadstable tr').show().not(':contains(' + this.value + ')').hide(); });
I have this script: <? $query = mysql_query(SELECT * FROM azzdk) or die(mysql_error()); $row
Okay, so I have this script which works fine with IE but won't work
I have a drop down list (select) populated from SQL. This is dynamic so
I have this below Literal to show my dynamic table <div id=Print> <asp:Literal ID=Literal1
I have this script that run to fix my menu bar to the browser
I have this script and need to be able to call the $play variable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.