Magento – How to i get all product options on the home page

custom-optionsproductvirtual-products

I've done quite a lot of searching on this but cannot find what seems the best (standard clean) method for adding a product's details to the homepage. By this I mean a way to add all of the info that is available on the view.phtml product page (e.g. add to cart button, product options, description, etc) to the home page.

I've discounted overriding the index controller (within a module), e.g. –

public function indexAction($coreRoute = null) {
  $this->getResponse()->setRedirect('/catalog/product/view/id/42');
   return;
}

As this can only provide for one product option (when I have 3). But as a compromise solution and to improve my understanding I have tried to create an add to cart button programatically using this – http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/catalog/adding_a_product_to_the_cart_via_querystring – with this code:

<form action="<?php echo Mage::getUrl('checkout/cart/add', array('product'=>1,'qty'=>1,'options'=>array('-5071'=>3))) ?>" method="post">
<input type="hidden" name="product" value="1" />

<fieldset class="add-to-cart-box">
<input type="submit" title="<?php echo $this->__('Add to Cart') ?>" class="addtocart" /> 
</fieldset>
</form>

But this either redirects me to the product page with the error: "Please specify the product's required option(s)" or sends me to an empty shopping cart. I am not sure if this is because this method only works with simple products (when I am using a virtual product) or I have the syntax on the "options" array wrong. The option input tag is:

<input id="options_-5071_2" class="radio validate-one-required-by-name product-custom-option" type="radio" price="0" checked="" value="3" name="options[-5071]" onclick="opConfig.reloadPrice()">

So to clarify:

a) get product options on the home page (e.g. add to cart button, product options, description, etc);

b) an option of programatically adding a (virtual) product to cart (with stipulated product option). Many thanks.

Best Answer

So I've managed to solve the second part of my question - getting the add to cart button programatically.

Just as in submitting a form in the backend (admin) you need a form key otherwise you will be just directed to the shopping cart page with no added item. Getting the post url could probably be done cleaner but here's what I did:

<?php 
$params_arr = array('product'=>1,'qty'=>1,'options[-5071]'=>1);
$url = 'checkout/cart/add?';
foreach($params_arr as $paramKey => $paramVal) $url_arr[] = $paramKey.'='.$paramVal;
$url .= implode('&',$url_arr);
?>

<form action="<?php echo Mage::getUrl($url) ?>" method="post">
<input type="hidden" name="product" value="1" />
<input type="hidden" name="form_key" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" />

<fieldset class="add-to-cart-box">
<input type="submit" title="<?php echo $this->__('Add to Cart') ?>" class="addtocart" /> 
</fieldset>
</form>
Related Topic