Magento – Auto Select simple products for configurable product

ce-1.8.1.0configurable-productmagento-1.8

Let say I sell green, black, and white T-Shirts inside a configurable product with one option (color).
So indeed there are 3 simple products selected. But what if I have 3 simple products of white T-Shirts which are changing in price? How can I automatic choose the cheapest white T-Shirt product to sell in the configurable product?

I am thinking about a select statement, which selects all possible products for a configurable product, and then make some MIN() statements. But how is the products linked to the conf. product in the database?

Do you have any clues?

(Magento CE 1.8.1)

Best Answer

Here is an idea.
In order to have something preselected for a configurable product you must rewrite Mage_Catalog_Block_Product_View_Type_Configurable::getJsonConfig.
More exactly this part.

    $preconfiguredFlag = $currentProduct->hasPreconfiguredValues();
    if ($preconfiguredFlag) {
        $preconfiguredValues = $currentProduct->getPreconfiguredValues();
        $defaultValues       = array();
    }

Or this part

        if ($preconfiguredFlag) {
            $configValue = $preconfiguredValues->getData('super_attribute/' . $attributeId);
            if ($configValue) {
                $defaultValues[$attributeId] = $configValue;
            }
        }

You can add your custom logic around these lines of code.
I would go with this approach.
If after executing the code above defaultValues is an empty array add your logic. If it's not empty it means that the use clicked on the 'edit' button from the cart and you shouldn't interfere with that.

Now the second part.
How to get the minimum price.
Here is an explanation about how to get the min and max price from the configurable product combinations.

Other than this, I can recommend you this extension. Among other features it allows you to set from the backend the default configuration for a configurable product just by selecting a simple product associated to the configurable one.
The downside of it is that you have to edit every configurable product to set this.

Related Topic