-1

How do I get the SKU no to change once the the product attribute of an item is selected.

enter image description here

2 Answers2

0

To get sku of selected associated product on configurable product page, please paste the following code in app/design/frontend///template/catalog/product/view/type/options/configurable.phtml in side the script:

spConfig.getIdOfSelectedProduct = function()
  {
    var existingProducts = new Object();
    for(var i=this.settings.length-1;i>=0;i--)
    {
     var selected = this.settings[i].options[this.settings[i].selectedIndex];
     if(selected.config)
     {
     for(var iproducts=0;iproducts<selected.config.products.length;iproducts++)
     {
      var usedAsKey = selected.config.products[iproducts]+"";
      if(existingProducts[usedAsKey]==undefined)
      {
       existingProducts[usedAsKey]=1;
      }
      else
      {
       existingProducts[usedAsKey]=existingProducts[usedAsKey]+1;
      }
      }
     }
    }
    for (var keyValue in existingProducts)
    {
    for ( var keyValueInner in existingProducts)
     {
     if(Number(existingProducts[keyValueInner])<Number(existingProducts[keyValue]))
     {
      delete existingProducts[keyValueInner];
     }
     }
    }
    var sizeOfExistingProducts=0;
    var currentSimpleProductId = "";
    for ( var keyValue in existingProducts)
    {
    currentSimpleProductId = keyValue;
    sizeOfExistingProducts=sizeOfExistingProducts+1
    }
    if(sizeOfExistingProducts==1)
    {
    alert("Selected product is: "+currentSimpleProductId)
    }

  }

Now add onchange event to your dropdown in same page:

onchange="spConfig.getIdOfSelectedProduct()"

First code will alert simple associated product id. You can use it in below code now.

jQuery.ajax({
    type: "POST",
    url: "<?php echo $this->getBaseUrl()?><module_front_name>/<controller_name>/<action_name>/",
    data:"id="+currentSimpleProductId,
    success: function(msg)
    {
        alert(msg);
        //var data = JSON.parse(msg);
        //alert(data.id);
    }
});

Now, go to controller file which you have used above and create a new action with name Action and put below code in it:

public function <action_name>Action()
    {
        $productId = $_REQUEST['id'];
        /*$productId is your selected product id. do what ever you want to do here.*/
        $product = Mage::getModel('catalog/product')->load($productId);
        $productsku = $product->getSku();

        $arraygroup = array("sku"=>$productsku);
        echo json_encode($arraygroup);
    }

Now you can use this information in your phtml file to show sku.

Please let me know if anything is unclear.

Mohit Kumar Arora
  • 2,204
  • 2
  • 21
  • 29
  • This answer is not correct and the code above does not follow Magento coding practices - e.g. using getBaseUrl instead of getUrl, $_REQUEST in controller. Do not mess around with Magento Configurable JS scripts unless you really have to! – Bery Jun 22 '16 at 14:16
0
  1. Go to Magento admin
  2. In catalog > Manage attribues find sku and look for similar to "Visible on Product View Page on Front-end"
  3. This should remove the sku from product page
  4. Find the configurable product template and insert where you would like to have your SKU

    echo $product->getSku()

Bery
  • 1,094
  • 1
  • 10
  • 23