7

Assuming I have a configurable product on the frontend with two dropdowns:

Size

Color

How can I get the final simple product id once the user has made their selections.

In other words: once I know the selections from all the super attributes how can I use this to get the simple product.

6 Answers 6

8

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:

var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);

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 deeper products 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 in Mage_Catalog_Block_Product_View_Type_Configurable::getJsonConfig().

In the JS class there is:

this.settings.each(function(element){
    Event.observe(element, 'change', this.configure.bind(this))
}.bind(this));

In the configureElement function there is the call:

this.reloadPrice();

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.

1
  • that's exactly what I'm looking for, but $childProduct returns null on getFinalPrice()! How can I get the final, discounted price? Oct 19, 2017 at 10:10
4

The product which you have added to cart, you can get its simple product by following code

$_item is instance of Mage_Sales_Model_Quote_Item

$simpleProduct =  $_item->getOptionByCode('simple_product')->getProduct()



$simpleProductId = $simpleProduct->getId()

Hope it helps!

1
  • 1
    This only works in the cart, and I don't think that's what pzirkind was asking for. However, it's still good to know.
    – Tyler V.
    Oct 2, 2014 at 20:51
3

Try this to get the final simple product id once the user has made their selections.

In your custom module phtml file you can get configure productId and super attributes value like Size and Color by following code

<?php
    $_product = $this->getProduct();
    $productId = $_product->getId();
    $AllowAttributes=$_product->getTypeInstance(true)->getConfigurableAttributes($_product);
    $attr1 = 0;
    foreach($AllowAttributes as $attribute) {
        $productAttribute = $attribute->getProductAttribute();
        $attr2 = $productAttribute->getId();
        if($attr1 == 0){
            $attr1 = $attr2;
        }
    }
?>

By using the productId, color and size call to custom module Api function (Ex. getProductInfo()) from your phtml file using jQuery and in your Api place the code

function getProductInfo($productId,$color,$size){
    $nextarr = array();
    $productData = array();
    $product = Mage::getModel('catalog/product')->load($productId);
    $configurable= Mage::getModel('catalog/product_type_configurable')->setProduct($product);
    $simpleCollection = $configurable->getUsedProductCollection()
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('color',$color)
                ->addAttributeToFilter('size',$size)
                ->addFilterByRequiredOptions();
    if(!empty($simpleCollection)){
        $data = array();
        foreach($simpleCollection as $simple){

            $simpleProductId = $simple->getId();

        }
    }

    return $simpleProductId;
}
2

if you have the super attributes as an array, where the keys are the option ( e.g. color, size ) IDs and the values the selected option ( e.g. red, xl ), for example:

$superAttributes = array(
    140 => 3310
);

then you can easily get the simple product like this :

/* @var $configurable Mage_Catalog_Model_Product_Type_Configurable */
$childProduct = $configurable->getProductByAttributes($superAttribute);
1

if you want to the simple Product details,then try the below code

$order=Mage::getModel("sales/order")->load(5);

    foreach ($order->getAllItems() as $item) {
         if (!$item->isDeleted() && !$item->getParentItemId()) {
        echo $item->getName()."-".$item->getSku()."<br/>";

            $Options=$item->getData('product_options');
           echo "<pre>";
           print_r(unserialize($Options));
           echo "</pre>";
           }}

Explain: $Options gives simple product super_attribute value and variance text value etc.if you want to get simple product id then need to load product by $item->getSku(); Sku is give simple product sku

$product=Mage::getModel("catalog/product")->loadByAttribute('sku',$item->getSku());

enter image description here

If want to for quote then just change

$quote = Mage::getSingleton('checkout/session')->getQuote();
        $cartItems = $quote->getAllVisibleItems();
        foreach ($cartItems as $item)
-1

Used Following code for Magento 2

$superAttribute = [$size_id => $sel_propduct_size, $touch_id => $sel_propduct_touch, $color_id => $sel_propduct_color];

$childProduct   = $product->getTypeInstance()->getProductByAttributes($superAttribute, $product);

$child_product_id = $childProduct->getID();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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