3

I'm trying to get the associated products using the method $p->getTypeInstance(true)->getConfigurableAttributesAsArray($p) which results in an array like this:

Array
(
    [0] => Array
        (
            [id] => 13
            [label] => Size
            [use_default] => 0
            [position] => 0
            [values] => Array
                (
                    [0] => Array
                        (
                            [product_super_attribute_id] => 13
                            [value_index] => 7
                            [label] => S
                            [default_label] => S
                            [store_label] => S
                            [is_percent] => 0
                            [pricing_value] => 
                            [use_default_value] => 1
                        )
[...]

What I'm trying to get is the product that's representing [value_index] => 7 which is an associated simple product.

Any ideas?

3 Answers 3

4

I've had a similar problem and I needed to fetch the price settings on a configurable product using fixed price.

The following method will return an array with the index as the simple product ID and the value as the pricing value.

Hope it will be useful to someone looking for the same answer as I did!

/**
 * Fetch price settings on configurable products
 *
 * @param $configurableProduct
 *
 * @return array
 */
public function getChildrenPricing($configurableProduct)
{
    /**
     * Setup return array
     */
    $return = array();

    /**
     * Setup our price array
     */
    $prices = array();

    /**
     * Fetch all attributes of the configurable product
     */
    $attributes = $configurableProduct
        ->getTypeInstance(true)
        ->getConfigurableAttributes($configurableProduct);

    /**
     * Loop all attributes and find out the pricing value
     * Be aware that this could be percentage
     */
    foreach ($attributes as $attribute) {
        foreach ($attribute->getPrices() as $attributePrice) {
            $prices[$attributePrice['value_index']] = $attributePrice['pricing_value'];
        }
    }

    /**
     * Fetch all children
     */
    $simpleProducts = $configurableProduct
        ->getTypeInstance(true)
        ->getUsedProductCollection($configurableProduct);

    /**
     * Loop all children
     */
    foreach ($simpleProducts as $simpleProduct) {

        /**
         * Look at the attributes
         */
        foreach ($attributes as $attribute) {

            /**
             * Load simple product
             * This will allow all attributes to be fetched from the product
             */
            $simpleProduct->load($simpleProduct->getId());

            /**
             * Fetch the value from the simple product
             */
            $value = $simpleProduct->getData($attribute->getProductAttribute()->getAttributeCode());

            /**
             * Loop all prices
             */
            foreach ($prices as $priceKey => $priceValue) {
                if ($priceKey == $value) {
                    $return[$simpleProduct->getId()] = $priceValue;
                }
            }
        }
    }

    return $return;
}
0
1

Is there a reason why you can't use $collection = $p->getTypeInstance(true)->getUsedProductCollection($p) and then filter further?

1
  • Well, it's an extension which I need to extend, but I'll have a look at the method, thanks!
    – oschloebe
    Oct 26, 2013 at 4:03
1

I know this is an old question but maybe this will help someone.

  • $product is the configurable product
  • $attributeData[0]['attribute_code'] is the attribute code which you get from calling getConfigurableAttributesAsArray().

            $childProducts = $product->getTypeInstance(true)->getUsedProducts(null,$product);
    
            foreach ($childProducts as $child) {
                $childProductsArray[$child->getData($attributeData[0]['attribute_code'])] = $child->getPrice();
            }  
    

$child->getData($attributeData[0]['attribute_code']) is the value_index you see there from calling getConfigurableAttributesAsArray()

Printing $childProductsArray you get something like this:

Array
(
    [799] => 749.0000
    [798] => 789.0000
    [797] => 755.0000
    [796] => 809.0000
    [795] => 875.0000
    [794] => 949.0000
)

in my case I needed the price but you should get the product id by replacing $child->getPrice() with $child->getId()

Your Answer

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

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