1

I am able to add the simple associated product of a configurable product using the code below.

                        <?php
                        $purchasableProductId = $product->getId();
                        if($product->getTypeId() == "configurable"):
                            $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($product);
                            $simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect('*')->addFilterByRequiredOptions();
                            foreach($simple_collection as $simple_product) {
                                $purchasableProductId = $simple_product->getId();
                                break; //Break after first iteration since most configurable products have the first product as the most popular choice
                                //echo $simple_product->getId() . " - " . $simple_product->getName() . " - " . Mage::helper('core')->currency($simple_product->getPrice()) . "\n";
                            }
                        endif;
                        ?>
                        <form action="/checkout/cart/add/product/<?php echo $purchasableProductId ?>" method="post">
                            <input type="hidden" name="form_key" value="<?php echo Mage::getSingleton('core/session')->getFormKey(); ?>" />
                            <input type="hidden" name="qty" value="1" /><!--onclick="setLocation('<?php echo Mage::helper('checkout/cart')->getAddUrl($product); ?>')" --> 
                            <button type="submit" style="position:absolute; bottom:0px; width:99%;" title="<?php echo $this->__('Add to Cart') . ' - $' . number_format($product->getPrice(), 2) ?>" class="button btn-cart"  ><span><span><?php echo $this->__('Add to Cart') . ' - $' . number_format($product->getPrice(), 2) ?></span></span></button>
                        </form> 
                    </div>

My problem is that it is not added as an option, so the product does not show the parent configurable product's image in the cart as it would had I used the product view page to add the option to the cart.

How can I modify the form above to add the product to cart as an option rather than an independent product?

1

iUseMagentoNow

you need to get configurable product varien attribute and send it send to cart simple product attribute to cart

get configurable attribute of product configurable

$AllowAttributes=$conf->getTypeInstance(true)
            ->getConfigurableAttributes($conf);
$_attributes = Mage::helper('core')->decorateArray($AllowAttributes);

then create a dynamic form input fields

   <?php
    foreach($_attributes as $_attribute):
    $productAttribute   = $attribute->getProductAttribute();
/* here varien value for simple products */
    $attributeValue     = $simple_product->getData($productAttribute->getAttributeCode());
    ?>

            <input type="hidden"  name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" id="attribute<?php echo $_attribute->getAttributeId() ?>" value="<?php echo $attributeValue;  ?>" />
        <?php endforeach; ?>

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.