2

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.

3
  • which options do you want on home page like custom options or custom attribute please tell me so I will help you
    – Keyul Shah
    Commented Jan 12, 2014 at 7:31
  • These are custom options provided by a module. Commented Jan 13, 2014 at 8:57
  • Anyone who can solve the 'add to cart' link problem for the virtual product (with options) is most welcome_g Commented Jan 14, 2014 at 14:12

2 Answers 2

2

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>
0

To get Custom Options

 <?php
    $product = Mage::getModel("catalog/product")->load($this->getProduct()->getId()); //product id
    $j = 0;
    foreach ($product->getOptions() as $_option) {
        $values = $_option->getValues();
        foreach ($values as $v) {
            $j++;
            echo $v->getTitle(); // Displaying Dropdown values
            echo "<br />";
        }
    }
    echo $j;
 ?>

Here $j is displaying the size of dropdown(custom options).

3
  • The custom options from the module that I mentioned are not stored in the usual manner (and therefore not in the table "catalog_product_option"). I have tested this for options that would be and your values are outputted. Of course they would need to be put into the correct input tag format, etc. Although this doesn't help me because of the custom module I will upvote you if you can provide the necessary code for the add to cart button as my attempts have been futile. Good work_g Commented Jan 14, 2014 at 14:10
  • I have to do some RND in that and let you know.
    – Keyul Shah
    Commented Jan 14, 2014 at 14:30
  • NEVER do a $product->load inside a loop. Commented Jun 28, 2015 at 18:15

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.