如何在Magento的高级搜索中包含已链接的类别

时间:2014-09-24 13:53:16

标签: magento magento-1.9

我想在高级搜索中使用链式选择来包含类别及其所有子类别。链式选择只是一个建议。

如果有任何其他方法可以在另一个下拉菜单中加载类别和子类别,也欢迎使用这些方法。

1 个答案:

答案 0 :(得分:0)

我在magento中使用Ajax实现了上述功能。 应用程序/设计/前端/碱/默认/模板/ catalogsearch /先进/ form.phtml

<li>
       <label for="section_search_field">Select Vehicle</label><br>
       <select name="category" id="vehicle">
       <option selected="selected">--Select Vehicle--</option>
           <?php
      $sql=mysql_query("select name,entity_id from catalog_category_flat_store_1 where parent_id=8");
      while($row=mysql_fetch_array($sql))
      {
      $id=$row['entity_id'];
      $data=$row['name'];
      echo '<option value="'.$id.'">'.$data.'</option>';
      } ?>
       </select>
    </li>
    <li>
        <label for="category_search_field">Select Manufacturere</label><br>

        <select name="category" id="manufacturere" class="manufacturere" >
            <option value="">--Choose Manufacturere--</option>

                       </select>
    </li>

    <li>
    <label for="subcategory_search_field">Select Type</label><br>
    <select name="sub" id="vehicle_type" class="vehicle_type">
      <option value="">--Choose type--</option>
    </select>
    </li>
    <li>
        <label for="category_search_field">Select Model</label><br> 
        <select name="category" id="vehicle_model" class="vehicle_model">
            <option value="">--Choose Model--</option>
        </select></li>
        <?php foreach ($this->getSearchableAttributes() as $_attribute): ?>
        <?php $_code = $_attribute->getAttributeCode() ?>
          <li>
            <label for="<?php echo $_code ?>"><?php echo $this->getAttributeLabel($_attribute) ?></label>
            <?php switch($this->getAttributeInputType($_attribute)):
                case 'number': ?>
                <div class="input-range">
                    <input type="text" name="<?php echo $_code ?>[from]" value="<?php echo $this->escapeHtml($this->getAttributeValue($_attribute, 'from')) ?>" id="<?php echo $_code ?>" title="<?php echo $this->escapeHtml($this->getAttributeLabel($_attribute)) ?>" class="input-text validate-number" maxlength="<?php echo $maxQueryLength;?>" />
                    <span class="separator">-</span>
                    <input type="text" name="<?php echo $_code ?>[to]" value="<?php echo $this->escapeHtml($this->getAttributeValue($_attribute, 'to')) ?>" id="<?php echo $_code ?>_to" title="<?php echo $this->escapeHtml($this->getAttributeLabel($_attribute)) ?>" class="input-text validate-number" maxlength="<?php echo $maxQueryLength;?>" />
                </div>
                <?php break;
                case 'price': ?>
                <div class="input-range">
                    <input name="<?php echo $_code ?>[from]" value="<?php echo $this->escapeHtml($this->getAttributeValue($_attribute, 'from')) ?>" id="<?php echo $_code ?>" title="<?php echo $this->escapeHtml($this->getAttributeLabel($_attribute)) ?>"  class="input-text validate-number" type="text" maxlength="<?php echo $maxQueryLength;?>" />
                    <span class="separator">-</span>
                    <input name="<?php echo $_code ?>[to]" value="<?php echo $this->escapeHtml($this->getAttributeValue($_attribute, 'to')) ?>" id="<?php echo $_code ?>_to" title="<?php echo $this->escapeHtml($this->getAttributeLabel($_attribute)) ?>"  class="input-text validate-number" type="text" maxlength="<?php echo $maxQueryLength;?>" />
                    <small>(<?php echo $this->getCurrency($_attribute); ?>)</small>
                </div>
                <?php break;
                case 'select': ?>
                    <div class="input-box">
                       <?php echo $this->getAttributeSelectElement($_attribute) ?>
                    </div>
                <?php break;
                case 'yesno': ?>
                    <?php echo $this->getAttributeYesNoElement($_attribute) ?>
                <?php break;
                case 'date': ?>
                <div class="input-range">
                    <?php echo $this->getDateInput($_attribute, 'from') ?>
                    <span class="separator">-</span>
                    <?php echo $this->getDateInput($_attribute, 'to') ?>
                </div>
                <?php break;
                default: ?>
                <div class="input-box">
                    <input type="text" name="<?php echo $_code ?>" id="<?php echo $_code ?>" value="<?php echo $this->escapeHtml($this->getAttributeValue($_attribute)) ?>" title="<?php echo $this->escapeHtml($this->getAttributeLabel($_attribute)) ?>"  class="input-text <?php echo $this->getAttributeValidationClass($_attribute) ?>" maxlength="<?php echo $maxQueryLength;?>" />
                </div>
            <?php endswitch; ?>

        </li>

        <?php endforeach; ?>

    </ul>

在同一个文件中,我在脚本标签中使用了ajax调用,如下所示:

<script type="text/javascript">
    var $j = jQuery.noConflict();
    $j(document).ready(function($) {      

    $j('#vehicle').change(function()
    {

         var optionCheckVechile = $j('select#vehicle option:selected').text(); 
         var optionCheckManufacturer = $j('select#manufacturere option:selected').text(); 
         var optionCheckType = $j('select#vehicle_type option:selected').text(); 
         var optionCheckModel = $j('select#vehicle_model option:selected').text(); 


          if(optionCheckType != '--Select type--'){
                  $('select#vehicle_type option:eq(0)').attr("selected", true);
                  }
                   if(optionCheckModel != '--Select Model--'){
                  $('select#vehicle_model option:eq(0)').attr("selected", true);
                  }


          if((optionCheckVechile == 'Car')|| (optionCheckVechile == 'Bus')){
          $j("#vehicle_type").prop('disabled',false);
          $j('#vehicle_type').css('background-color','#fff');
          $j("#vehicle_model").prop('disabled',false);
          $j('#vehicle_model').css('background-color','#fff');
          }
          else if((optionCheckVechile == 'Bike') || (optionCheckVechile == 'Truck')|| (optionCheckVechile == 'Auto')){
          $j("#vehicle_model").prop('disabled',true);
          $j('#vehicle_model').css('background-color','#ccc');
          }
          else{
          $j("#vehicle_type").prop('disabled',true);
          $j('#vehicle_type').css('background-color','#ccc');
          $j("#vehicle_model").prop('disabled',true);
          $j('#vehicle_model').css('background-color','#ccc');
          }


    var id=$j(this).val();
          var dataString = 'id='+ id;
          $j.ajax
          ({
          type: "POST",
          url: "<?php echo $this->getUrl('catalogsearch/ajax/vehicle') ?>",
          data: dataString,
          cache: false,
          success: function(html)
          { 

          $j("#manufacturere").html(html);
          } 
          });
          $j("#manufacturer").load();
          $j("#vehicle_model").load();
          $j("#vehicle_type").load();
    });
    $j('#manufacturere').change(function()
    {
    var optionCheckModel = $j('select#vehicle_model option:selected').text();
    if(optionCheckModel != '--Select Model--'){
                  $('select#vehicle_model option:eq(0)').attr("selected", true);
                  }
    var id=$j(this).val();
          var dataString = 'id='+ id;

          $j.ajax
          ({
          type: "POST",
          url: "<?php echo $this->getUrl('catalogsearch/ajax/manufacturer') ?>",
          data: dataString,
          cache: false,
          success: function(html)
          { 
          $j("#vehicle_type").html(html);
          } 
          });
    });
    $j('#vehicle_type').change(function()
    {
    var id=$j(this).val();
          var dataString = 'id='+ id;

          $j.ajax
          ({
          type: "POST",
          url: "<?php echo $this->getUrl('catalogsearch/ajax/type') ?>",
          data: dataString,
          cache: false,
          success: function(html)
          { 
          $j("#vehicle_model").html(html);
          } 
          });
    });
}); 

接下来在app / code / core / Mage / CatalogSearch / controllers / AjaxController.php中添加以下代码。

class Mage_CatalogSearch_AjaxController extends Mage_Core_Controller_Front_Action
{

 public function vehicleAction()
{  
   $id=$_POST['id'];
   $sql=mysql_query("select name,entity_id from catalog_category_flat_store_1 where parent_id='$id'");
   echo '<option value=0>--Select Manufacturere--</option>';
while($row=mysql_fetch_array($sql))
  {
  $id=$row['entity_id'];
  $data=$row['name']; 
  echo '<option value="'.$id.'">'.$data.'</option>';
  }   
}
 public function manufacturerAction()
{
 $id=$_POST['id'];
   $sql=mysql_query("select name,entity_id from catalog_category_flat_store_1 where parent_id='$id'");
   echo '<option value=0>--Select type--</option>';
while($row=mysql_fetch_array($sql))
  {
  $id=$row['entity_id'];
  $data=$row['name'];
  echo $id;
  echo '<option value="'.$id.'">'.$data.'</option>';    
  }   

}
 public function typeAction()
{
 $id=$_POST['id'];
   $sql=mysql_query("select name,entity_id from catalog_category_flat_store_1 where parent_id='$id'");
   echo '<option value=0>--Select Model--</option>';
while($row=mysql_fetch_array($sql))
  {
  $id=$row['entity_id'];
  $data=$row['name'];
  echo $id;
  echo '<option value="'.$id.'">'.$data.'</option>';
  }   

}

}

然后刷新页面并运行高级搜索。链式选择将适用于根据父ID加载的类别。

注意:这仅用于加载特定类别及其子类别。