2

Situation

so I'm working at a tool for my product detail page where you are able to put the product you're looking at AND the product that has been bought together with this one the most into the cart with one click. The loading of the most relevant product works fine. But now I have following problem:

Problem

If I have a configurable product (the original or the most relevant product) I'm not able to display the product options properly. I can show the labels but no options in it.

What I've tried:

So this is what i get (opened bottom drop down, "Option wählen..." equals "Choose an Option...")

http://puu.sh/kza7N.png

I've tried this so far (and some other ways to get the attributes). Atleast it now lists me the labels of the dropdowns. But there arent any options in it

if($_product->isConfigurable())
{
    $_attributes = Mage::helper('core')->decorateArray($_product->getTypeInstance(true)->getConfigurableAttributes($_product));
    echo count($_attributes);
    ?>
    <?php if ($_product->isSaleable() && count($_attributes)):?>
    <dl>
        <?php foreach($_attributes as $_attribute): ?>
            <dt><label class="required"><em>*</em><?php echo $_attribute->getLabel() ?></label></dt>
            <dd<?php if ($_attribute->decoratedIsLast){?> class=""<?php }?>>
                <div class="input-box">
                    <select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select">
                        <option><?php echo $this->__('Choose an Option...') ?></option>
                    </select>
                </div>
            </dd>
        <?php endforeach; ?>
    </dl>
    <?php
    $block1 = Mage::app()->getLayout()->createBlock('catalog/product_view');
    $block1->setProduct($_product);
    $configPrice = $block1->getJsonConfig();
    ?>
    <script type="text/javascript">
        var sp1Config = new Product.Config(<?php echo $block1->getJsonConfig() ?>);
    </script>
<?php endif;

Results so far

Full html output:

        <dl>
            <dt><label class="required"><em>*</em>Farbe</label></dt>
            <dd>
                <div class="input-box">
                    <select name="super_attribute[155]" id="attribute155" class="required-entry super-attribute-select">
                        <option>Option wählen...</option>
                    </select>
                </div>
            </dd>
            <dt><label class="required"><em>*</em>Größe</label></dt>
            <dd class="">
                <div class="input-box">
                    <select name="super_attribute[159]" id="attribute159" class="required-entry super-attribute-select">
                        <option>Option wählen...</option>
                    </select>
                </div>
            </dd>
        </dl>

        <script type="text/javascript">
            var sp1Config = new Product.Config({"productId":"5151","priceFormat":{"pattern":"%s\u00a0\u20ac","precision":2,"requiredPrecision":2,"decimalSymbol":",","groupSymbol":".","groupLength":3,"integerRequired":1},"includeTax":"true","showIncludeTax":true,"showBothPrices":false,"productPrice":44.98,"productOldPrice":44.98,"skipCalculate":1,"defaultTax":19,"currentTax":19,"idSuffix":"_clone","oldPlusDisposition":0,"plusDisposition":0,"oldMinusDisposition":0,"minusDisposition":0});
        </script>

The $block1->getJsonConfig() returns:

{
   "productId":"5151",
   "priceFormat":{
      "pattern":"%s\u00a0\u20ac",
      "precision":2,
      "requiredPrecision":2,
      "decimalSymbol":",",
      "groupSymbol":".",
      "groupLength":3,
      "integerRequired":1
   },
   "includeTax":"true",
   "showIncludeTax":true,
   "showBothPrices":false,
   "productPrice":44.98,
   "productOldPrice":44.98,
   "skipCalculate":1,
   "defaultTax":19,
   "currentTax":19,
   "idSuffix":"_clone",
   "oldPlusDisposition":0,
   "plusDisposition":0,
   "oldMinusDisposition":0,
   "minusDisposition":0
}

Following try results in just a big bunch of empty select dropdowns:

    $_attributes = Mage::helper('core')->decorateArray($_c_product->getAttributes());

    ?>
    <?php if ($_c_product->isSaleable() && count($_attributes)):?>
        <dl>
        <?php foreach($_attributes as $_attribute): ?>
            <dt><label class="required"><em>*</em><?php echo $_attribute->getLabel() ?></label></dt>
            <dd<?php if ($_attribute->decoratedIsLast){?> class=""<?php }?>>
                <div class="input-box">
                    <select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" class="required-entry super-attribute-select">
                        <option><?php echo $this->__('Choose an Option...') ?></option>
                      </select>
                  </div>
            </dd>
        <?php endforeach; ?>
        </dl>
        <script type="text/javascript">
            var spConfig = new Product.Config(<?php echo $this->getJsonConfig() ?>);
        </script>
    <?php endif;

I'm going desperate on this one..

Greetings and thanks for the help

0
+100

Normally to collect all of the related options to a configurable product you could do something like this:

$productAttributeOptions = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
$attributeOptions = array();
foreach ($productAttributeOptions as $productAttribute) {
    foreach ($productAttribute['values'] as $attribute) {
        $attributeOptions[$productAttribute['label']][$attribute['value_index']] = $attribute['store_label'];
    }
} 
zend_debug::dump($attributeOptions);
6
  • Thanks, that worked pretty well. Also thank you for showing me zend_debug::dump(), that one does pretty well. – SidiaStudios Oct 7 '15 at 13:24
  • But is there a way to generate all that with an existing Magento function? I mean the dropdown. I can do it without but it should already exist and i dont like duplicated code – SidiaStudios Oct 7 '15 at 13:25
  • Since its not normal functionality most want out of box, there isn't really a default method to do such. getConfigurableAttributesAsArray can be found being used in the admin portion of the code. goo.gl/6VJB7S It is safe to say this wouldn't fall under code duplication. – B00MER Oct 7 '15 at 15:41
  • But the dropdowns you have at the normal product detail page are generated somewhere, cant i use that function? – SidiaStudios Oct 8 '15 at 7:44
  • 1
    Well i got done what i needed to get done now. Was quite a bunch of work but i got it now. So thank you! Ill reward you in 1h (cause of the 24h limit) – SidiaStudios Oct 8 '15 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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