This is the blog for getting Idea regarding PHP, Magento, jQuery and JavaScript for Customization.

Monday 24 December 2012

Magento : Get custom options on category page

 Here, The Code for get Custom Options of Products on category Page, This is useful for reduce the time for customer purchase product and add to cart it, there is another methods are also but in that we can't add product with it's custom options, so using this we can add product to cart with its custom options.


 <?php
    $sandy = "";
    $attVal = $product->getOptions();
    if(sizeof($attVal))
    {
          foreach($attVal as $optionVal)
          {   
        $type = $optionVal->getType();
        if($type != 'field' && $type != 'file' )
         {
                $sandy .= $optionVal->getTitle().": ";
                $sandy .= "<select id='select_".$optionVal->getId()."' name='options[".$optionVal->getId()."]'>";
                foreach($optionVal->getValues() as $valuesKey => $valuesVal)
            {           
                if($valuesVal->getPrice() != "0.00000")
                {
                      $sandy .= "<option price='".$valuesVal->getPrice(true)."' value='".$valuesVal->getId()."'>".$valuesVal->getTitle() .'+'.  $valuesVal->getPrice(true) ."</option>";       
                  }
                  else
                  {
                $sandy .=  "<option price='".$valuesVal->getPrice(true)."' value='".$valuesVal->getId()."'>".$valuesVal->getTitle() ."</option>";
                }
            }
            $sandy .= "</select>";
        }
        else if($type == 'file')
        {
            $sandy .= $optionVal->getTitle().": ";
                $sandy .= "<input type='file' name='options_".$optionVal->getId()."_file'>";               
              }
        else
        {
            $sandy .= $optionVal->getTitle() ."  : ";
                $sandy .= "<input type='text' name='options[".$optionVal->getId()."]' id='options_".$optionVal->getId()."_text' onchange='javascript: getval();'>";                  
        }
        }
    }
    echo $sandy;
  ?>

  <?php echo $this->getPriceHtml($_product, true) ?>
    
  <?php if(!$_product->isGrouped()): ?>
  <label>Qty:</label>
        <input type="text" id="qty_main" maxlength="12" value="<?php echo $this->getMinimalQty($_product)==null?1:$this->getMinimalQty($_product) ?>" onblur="calculatetotal();"/>
        <button type="submit" id="addtocartbt_<?php echo $_product->getSku(); ?>" onclick="productAddToCartForm<?php echo $_product->getId() ?>.submit()">Add to Cart</button>
  <?php endif; ?>  
  </div>
                                        
  </form>
      
<script type="text/javascript">
       
var productAddToCartForm<?php echo $_product->getId() ?> = new VarienForm('product_addtocart_form_<?php echo $_product->getId() ?>');                       
    productAddToCartForm<?php echo $_product->getId() ?>.submit = function()
    {
            if (this.validator.validate())
        {   
            this.form.submit();                           
            }                   
        }.bind(productAddToCartForm<?php echo $_product->getId() ?>);
  </script>
  <?php else: ?>
      <div class="out-of-stock"><?php echo $this->__('Out of stock') ?></div>
  <?php endif; ?>

4 comments:

  1. Nice, but where do i put it? list.phtml? Any help is appreciated Thanks

    ReplyDelete
  2. Yes, you Just put this code in list.phtml

    Here, you have two type Grid and List, you put this in both if....else... condition.

    ReplyDelete
  3. Thanks for the reply. Although it deosnt seem to be showing correctly . I'm adding it line before <button type=".
    Using version 1.7.0.2.

    Any thoughts?
    Your help is appreciated, thanks again, hope to get this working.

    ReplyDelete