1

I have started working with Magento, and I'm trying to get all custom options associated with a given product. I've found a solution to that, however, I ran into issues.

My PHP-code:

foreach ($_product->getOptions() as $optionInfo) :
    $values = $optionInfo->getValues(); 

    foreach ($values as $values) :
        $valuesArray[$values['option_type_id']] = array("option_type_id" => $values['option_type_id'], "option_id" => $values['option_id'], "title" => $values['title']);
    endforeach;

    $option = array("id" => $optionInfo->getId(), "type" => $optionInfo->getType(), "title" => $optionInfo->getTitle(), "values" => $valuesArray);
    $options[$optionInfo->getId()]= $option;
endforeach;

It sure do return the correct information. Atleast in the first iteration:

[2] => Array
    (
        [id] => 2
        [type] => drop_down
        [title] => Custom option 1
        [values] => Array
            (
                [4] => Array
                    (
                        [option_type_id] => 4
                        [option_id] => 2
                        [title] => Flaphack 1
                    )

                [5] => Array
                    (
                        [option_type_id] => 5
                        [option_id] => 2
                        [title] => Flaphack 2
                    )

                [6] => Array
                    (
                        [option_type_id] => 6
                        [option_id] => 2
                        [title] => Flaphack 3
                    )

            )

    )

However, during the second iteration (and perhaps even the third and forth and so on), I'm having duplicates of the values. In the second iteration, I'm getting the same values as i got in the first iteration PLUS the correct values for the second iteration:

[1] => Array
    (
        [id] => 1
        [type] => drop_down
        [title] => Custom option 2
        [values] => Array
            (
                [4] => Array
                    (
                        [option_type_id] => 4
                        [option_id] => 2
                        [title] => Flaphack 1
                    )

                [5] => Array
                    (
                        [option_type_id] => 5
                        [option_id] => 2
                        [title] => Flaphack 2
                    )

                [6] => Array
                    (
                        [option_type_id] => 6
                        [option_id] => 2
                        [title] => Flaphack 3
                    )

                [1] => Array
                    (
                        [option_type_id] => 1
                        [option_id] => 1
                        [title] => Flaphack 1.1
                    )

                [2] => Array
                    (
                        [option_type_id] => 2
                        [option_id] => 1
                        [title] => Flaphack 1.2
                    )

                [3] => Array
                    (
                        [option_type_id] => 3
                        [option_id] => 1
                        [title] => Flaphack 1.3
                    )

            )

    )

Do you guys have any idea what's going on? Would be greatly appriciated.

Best, Nikolaj

1 Answer 1

1

Try this code,

foreach ($_product->getOptions() as $optionInfo) :
$values = $optionInfo->getValues(); 
$valuesArray = array(); // added line
foreach ($values as $values) :
    $valuesArray[$values['option_type_id']] = array("option_type_id" => $values['option_type_id'], "option_id" => $values['option_id'], "title" => $values['title']);
endforeach;

$option = array("id" => $optionInfo->getId(), "type" => $optionInfo->getType(), "title" =>    $optionInfo->getTitle(), "values" => $valuesArray);
$options[$optionInfo->getId()]= $option;
endforeach;

The $valuesArray is getting values in each iteration and you never cleared it. So when the outer foreach gets into second loop the $valuesArray gets values in incremental fashion. If you clear $valuesArray in each iteration of outer foreach you will get what you wanted.

Sign up to request clarification or add additional context in comments.

2 Comments

First of all, thanks. It worked. Second: Can you explain to me why the declation of $valuesArray being an array before the foreach sorted the issue??
Ah, I see. Cheers bud. Unfortunately, I can't do that just yet. In need of more reputation in order to do so. :-/

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.