Mapping the attribute options in the dropdowns to actual products can be done by using the spConfig
javascript variable printed on the product view page of every configurable product.
var spConfig = new Product.Config({
"attributes":{
"502":{
"id":"502",
"code":"shoe_size",
"label":"Shoe Size",
"options":[{
"id":"46",
"label":"3",
"price":"0",
"oldPrice":"0",
"products":["32"]
},{
"id":"45",
[...]
}]
}},
[...]});
Notice the products
keys that have an array of product ids. Using some javascript that observes the onchange
event of your option dropdowns you should be able to work your way through the array ending up with the product the user has chosen.
Now for mapping that to the product SKU, you're probably best of retrieving them from a custom javascript array which you can create by using the code from this article. So now for some untested code
$simple_collection = Mage::getModel('catalog/product_type_configurable')->setProduct(Mage::registry('current_product'))
->getUsedProductCollection()
->addAttributeToSelect('sku')
->addFilterByRequiredOptions();
$id_to_sku = array();
foreach ($simple_collection as $_item)
{
$id_to_sku['p'+$_item->getId()] = $_item->getSku();
}
echo '<script type="text/javascript">
var confIdToSku = '.json_encode($id_to_sku).';
</script>';
Which would output an array like
var confIdToSku = {"p32":"pr-sku-1", "p35":"pr-sku-2"}
That would enable you to map the product ID to the SKU you need.
I'd suggest creating a new PHTML file for this in your template directory, using block type core/template
and calling that in the template/catalog/product/view.phtml
.
I'm no hero with Javascript so I didn't include any javascript code examples in my answer, I hope you can figure that part out for yourself.
load config product:
$currentProduct = Mage::getModel('catalog/product_type_configurable')->setProduct($this->getProduct());;
$products = array();
$skipSaleableCheck = Mage::helper('catalog/product')->getSkipSaleableCheck();
getting all all simple products
$allProducts = $conf->getTypeInstance(true)
->getUsedProducts(null, $conf);
foreach ($allProducts as $product) {
$products[] = $product;
}
getting all configurable attribute of that products
$AllowAttributes=$conf->getTypeInstance(true)
->getConfigurableAttributes($conf);
Getting config attribute for this product by attribute value id and label
foreach ($products as $product) {
foreach ($AllowAttributes as $attribute) {
$productAttribute = $attribute->getProductAttribute();
$productAttributeId = $productAttribute->getId();
$attributeValue = $product->getData($productAttribute->getAttributeCode());
/* Simple Product data */
/* getting option text value */
if ($productAttribute->usesSource()) {
$MYabel = $productAttribute->getSource()->getOptionText($attributeValue );
}else{
$MYabel='';
}
}
}
Best Answer
See: $product->getTypeInstance()->getProductByAttributes($attributesInfo, $product); from
Mage_Catalog_Model_Product_Type_Configurable
.Edit:
This is a question like the catchy ones from job interviews. Do you need this on configurable product page on frontend ?
In
app/code/base/default/template/catalog/product/type/options/configurable.phtml
there is:You need to extend this object. Either add your function in the class or extend an existing function that you'll need. http://prototypejs.org/learn/class-inheritance
In the JSON config passed to spConfig by default there is the
options
key and a deeperproducts
key. You can use this config in your JS function. On any configurable product page copy paste the JSON object and format it with a tool for ease of understanding. The JSON is built inMage_Catalog_Block_Product_View_Type_Configurable::getJsonConfig()
.In the JS class there is:
In the
configureElement
function there is the call:In function
reloadPrice
an iteration on the dropdowns is made. Your code should mimic this iteration to check if all available dropdowns were selected. On success your custom code comes in.Is it an Ajax call to fetch dynamic content ? I would not recommend it. Instead try to have the dynamic content/data of associated products available in the rendered page in a JSON object handled by you custom JS class.