2017-09-17 4 views
0

Magento2を複数のカテゴリに絞り込むにはどうすればよいですか?Magento2 Catgory Filter

<?xml version="1.0" encoding="UTF-8"?> 

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"><listingToolbar name="listing_top">  
<filters name="listing_filters"> 
     <argument name="data" xsi:type="array"> 
      <item name="config" xsi:type="array"> 
       <item name="templates" xsi:type="array"> 
        <item name="filters" xsi:type="array"> 
         <item name="select" xsi:type="array"> 
          <item name="component" xsi:type="string">Magento_Ui/js/form/element/ui-select</item> 
          <item name="template" xsi:type="string">ui/grid/filters/elements/ui-select</item> 
         </item> 
        </item> 
       </item> 
      </item> 
     </argument> 
</filters> 

どのように私は最近、私はMagentoのの階層ナビゲーションの標準機能を書き換えるする機会を得ましたカテゴリフィルタ

+0

StackOverflowはチュートリアルサービスではありません。オンラインで検索し、何かを実施してから助けを求めてください。 –

答えて

0

を追加することができ、私を助けてください。顧客は常に「フィルタ」のすべてを見えるようにするため、リクエストは非常に具体的でした。たとえば、カラー(黄色、緑色、赤色、青色、マゼンタ)をフィルタリングする場合は、商品はフィルタリングされますが、レイヤードナビゲーションではすべてのフィルタが表示されます。このようにして、顧客はカテゴリのビューに戻る必要なく、現在のカテゴリの製品を再フィルタリングできます。

レイヤードナビゲーションに使用されるファイルは、/design/frontend/base/default/template/catalog/layer/フォルダにあります。レイヤードナビゲーションに使用されるファイルはview.phtmlです。カテゴリをクリックすると、すべてのフィルタが表示されます。アクティブ状態で使用されるファイルはstate.phtmlです。フィルタの1つをクリックすると結果が返されますので、このファイルを編集します。だからstate.phtmlをベースからパッケージまたはテーマにコピーしてください。

これはstate.phtmlで元のコードです:今、私たちは、「現在」のdivを編集するつもりだ

<?php $_filters = $this->getActiveFilters() ?> 
<?php if(!empty($_filters)): ?> 
<div class="currently"> 
    <p class="block-subtitle"><?php echo $this->__('Currently Shopping by:') ?></p> 
    <ol> 
    <?php foreach ($_filters as $_filter): ?> 
     <li> 
      <a href="<?php echo $_filter->getRemoveUrl() ?>" title="<?php echo $this->__('Remove This Item') ?>" class="btn-remove"><?php echo $this->__('Remove This Item') ?></a> 
      <span class="label"><?php echo $this->__($_filter->getName()) ?>:</span> <?php echo $this->stripTags($_filter->getLabel()) ?> 
     </li> 
    <?php endforeach; ?> 
    </ol> 
    <div class="actions"><a href="<?php echo $this->getClearUrl() ?>"><?php echo $this->__('Clear All') ?></a></div> 
</div> 
<?php endif; ?> 

Since we’re going to need url path of current category add this code before “currently” div block 

<?php $obj = new Mage_Catalog_Block_Navigation(); ?> 
<?php $_current_category=$obj->getCurrentCategory()->getUrlPath(); ?> //getting url path of current category 
<?php $subs = $obj->getCurrentCategory()->getAllChildren(true); ?> //getting ids of subcategories of current category 

それはもうただ現在のフィルタ状態を示していないので、私は、状態に自分の名前を変更し。経験豊富なMagento開発者は、すべてのコードがMagento-wayでプログラムされているわけではないが、Magento CE 1.4.2をプログラミングしたときにMagentoはまだ機能を必要としていないことに気づくだろう。私は彼らが誰かから階層的なナビゲーションをこの方法で使用することを期待していなかったと思います。だからここに行く!

<?php if(!empty($_filters)): ?> 
<div class="state"> 
    <p class="block-subtitle"><?php echo $this->__('Currently Shopping by:') ?></p> 
    <dl> 
    <?php foreach ($_filters as $_filter): ?> 
      <dd> 
       <ol> 
        <?php $attributemodel=$_filter->filter->_data["attribute_model"]; ?> // getting attribute model that has all filter options in it from currently active filter 
        <?php $attroptions=$attributemodel->getSource()->getAllOptions();?> // getting attribute options (filters) from attribute model 
        <?php $_categ= Mage::getModel('catalog/category');?> // object containing all categories and their information 

       <?php foreach($subs as $cat_id): ?> 
         <?php $_categ->load($cat_id)?>   //get the subcategory you need 
         <?php $collection = $_categ->getProductCollection(); ?> //get the product collection (object containing all product information) 
         <?php $collection->addAttributeToSelect('color')?> // get the desired attribute 
       <?php endforeach; ?> 

次に、属性モデルから情報を抽出し、リンクをアセンブルする必要があります。各属性オプション($attroptions)には、属性値(id)と属性ラベルが含まれています。

<?php foreach($attroptions as $attr): ?> // get value and label of each attribute 
         <?php $count=0; ?> 
         <?php if($attr["value"]!=""): ?> 
           <?php $val=$attr["value"] ?> 
           <?php $collection->addFieldToFilter(array(array('attribute'=>'themes','gt'=>10)))?> // collection of attribute values and labels for all values 
//greater then 10 (in this case attribute values range was 18-39) 
           <?php $proddata=$collection->getData() ?> // get product data for all attribute values 
           <?php if($attr["label"]!= $this->stripTags($_filter->getLabel())): ?> // make a nice looking label 
            <?php foreach($proddata as $prod):?> 
             <?php if($prod["type_id"]=="configurable"): ?> // in this store all products were configurable 
              <?php $split=split(",", $prod["color"]);?>  // get the attribute values that correspond with one product (a product may have more 
// then one attribute value and they're separated by commas that's why we split the string with "," as deliminator) 

も、あなたはあなたがまだ唯一の製品count >0を持っている属性値を出力するために、製品をカウントする必要がありFilterable(with results)に自分の属性を設定すると思いました。

          <?php foreach($split as $color): ?> //check out how many products have the same attribute value 
               <?php if($color==$attr["value"]): ?> 
                <?php $count++;?> 
               <?php endif; ?> 
              <?php endforeach; ?> 

             <?php endif;?> 

            <?php endforeach; ?> 

            <?php if($count>0):?> // check if any product has that attribute value 
             <li><a href="<?php echo $this->getUrl('').$_current_category ?>?color=<?php echo $attr["value"]?>" ><?php echo $attr["label"]; ?></a></li> // if not currently active filter make a link 
            <?php endif; ?> 

           <?php else:?> 
            <li class="current"> <?php echo $this->stripTags($_filter->getLabel()); ?></li> // if currently active filter write out the label 
           <?php endif;?> 
         <?php endif; ?> 
<?php endforeach; ?> 

<?php endforeach; ?> // ending the first for loop (foreach($filters as $filter)) 
       </ol> 

      </dd> 
    </dl> 
    <a class="all" style="float:right;" href="<?php echo $this->getClearUrl()?>">All</a> // show all products, return from current state back to category view 
</div> 
<?php endif; ?> 

これだけです。フィルタを常に見えるようにするという作業は終了しました。今では階層ナビゲーションを使用してどこに行っても、カテゴリビューに戻る必要なく、現在のカテゴリの製品を簡単に再フィルタリングすることができます。最後に、簡単な警告が表示されます。必要な機能が利用できないため、ここのコードはMagento-wayでプログラムされていません。私は十分に問題と解決策を説明しました:)。 乾杯!

関連する問題