0

このコード (1) の意図は、Magento ストアのサブカテゴリをグリッド形式で表示することです。カテゴリ画像のサイズはコード自体で定義され、分離と幅は styles.css で定義されます。

コードを通常どおり使用すると、有効になっているすべてのサブカテゴリが取得されます (IsActive が true)。この「進行中の作業」リンクを参照してください: http://www.aldetal.biz/index.php/vabaro/fitness/nutricion.html

有効になっていて、他の場所で「表示に値する」というフラグが付けられているサブカテゴリを表示したいと思います。このようにして、ランディング ページのグリッド形式で表示されるサブカテゴリの数と、メニューに表示されるサブカテゴリの数を分けています。データベースに触れないようにするために、「キーワード」フラグとして文字列 showgrid を追加する Meta Keywords フィールドを使用します。

コード

<?php
/** 
* Original code by Jake Rutter
* @website: http://www.onerutter.com/web/magento-custom-category-images-listing-block-tutorial.html#idc-ctools
* Fixed by func0der
*
*/
?>

<div id="categories">
    <div class="col_full">
        <div class="listing" >
        <?php
            $_maincategorylisting = $this->getCurrentCategory();
            $_categories = $this->getCurrentChildCategories();

            if($_categories->count()):
                foreach ($_categories as $_category):

    /** Choose between the two possible IFs. The first one works (as before, incompletely. The second Should work completely but instead breaks the code */
    /**                 if($_category->getIsActive()): */ 
                        if($_category->getIsActive() && preg_match('/^showgrid/ism', $_category->getData('meta_keywords'))): 

                        $cur_category=Mage::getModel('catalog/category')->load($_category->getId());
                        $layer = Mage::getSingleton('catalog/layer');
                        $layer->setCurrentCategory($cur_category);

    /** This is just for debugging. The first shows the sub-category name under the image (as it should in final version). The second one shows the text in Meta_Keywords. */
    /** 
    /**                 $catName = $this->getCurrentCategory()->getName(); */
                        $catName = $this->getCurrentCategory()->getData('meta_keywords'); 

                        if($_imageUrl = $this->getCurrentCategory()->getImageUrl()):
            ?>

            <div class="category-box">
                <div class="category-image-box">
                    <a href="<?php echo $this->getCategoryUrl($_category)?>"><img src="<?php echo $_imageUrl?>" height="150"></a>
                </div>
                <div class="category-name">

                    <?php
                    /**  Changed to check writing the category vs variable */
                    /** <a href="<?php echo $this->getCategoryUrl($_category)?>"> <?php echo $_category->getName()?></a> */
                    ?>

                    <p>
                        <a href="<?php echo $this->getCategoryUrl($_category)?>"> <?php echo $catName ?></a>
                    </p>
                </div>
            </div>
            <?php 
                        else:
            ?>
            <div class="category-box">
                <div class="category-image-box">
                    <a href="<?php echo $this->getCategoryUrl($_category)?>"><img src="/skin/frontend/default/default/images/media/default01.jpg" height="150"); ?></a>
                </div>
                <div class="category-name">
                    <p>
                        <a href="<?php echo $this->getCategoryUrl($_category)?>"><?php echo $catName ?></a>
                    </p>
                </div>
            </div>


            <?php 
                        endif; /* END: if($_imageUrl=!$this->getCurrentCategory()->getImageUrl()) */
                    endif; /* END: $_category->getIsActive()) */
                endforeach; /* END: ($_categories as $_category) */

                /* This resets the category back to the original pages category
                *    If this is not done, subsequent calls on the same page will use the last category in the foreach loop.
                * 
                *    The next line is the one showing the problem. If I use the expanded IF I get the error here...
                */

                $layer->setCurrentCategory($_maincategorylisting);
                endif; /* END: if($_categories->count()) */
            ?>
        </div>
        <br clear=all>
    </div>
</div>

何らかの理由で、ティムによって提案された優れた IF

(1) Jake Rutter によって最初に書かれたもの: http://www.onerutter.com/web/magento-custom-category-images-listing-block-tutorial.htmlと私が微調整したもの


Magento フロント エンドでサブカテゴリを表示する次の構造を持つコードがあります。サブカテゴリは、有効になっている場合に表示されます ("IsActive" が true)。

  if($_categories->count()):
 foreach ($_categories as $_category):
        if($_category->getIsActive()):
               a whole bunch of stuff if true
            else
               other stuff if not true                 

データベースをいじらずに別の条件を追加したいので、Meta Keywords フィールドを使用しています (これは Google によって公式に無視されており、風変わりなキーワードを使用しても害がないためです)。

カテゴリが有効 (IsActive) で、かつそのフィールドの最初の文字列が「showgrid」の場合、IF は true であり、さまざまな処理が行われます。

ただし、注意してください。Meta Keywords フィールドは空である可能性があるため、内容を確認するだけでなく、テストする必要があります。カテゴリが有効になっていない (IsActive が false) またはメタ キーワードが空である、またはメタ キーワードが showgrid で始まらない場合、他のことが起こります

4

1 に答える 1

0

メタ タグ フィールドよりも優れた解決策は、カテゴリに新しい属性を追加することです。

app/code/local/[YourCompany]/Category にディレクトリを作成します

app/code/local/[YourCompany]/Category/etc/config.xml に config.xml ファイルを作成します

<?xml version="1.0"?>
<config>
    <modules>
         <YourCompany_Category>
             <version>0.1.0</version>
         </YourCompany_Category>
    </modules>
    <global>
        <resources>
            <category_setup>
                <setup>
                    <class>Mage_Eav_Model_Entity_Setup</class>
                    <module>YourCompany_Category</module>
                </setup>
                <connection>
                    <use>default_setup</use>
                </connection>
            </category_setup>
        </resources>
    </global>
</config>

次に、モジュール構成を app/etc/modules/YourCompany_Category.xml に作成します

<?xml version="1.0"?>
<config>
    <modules>
         <YourCompany_Category>
             <active>true</active>
             <codePool>local</codePool>
         </YourCompany_Category>
    </modules>
</config>

次に app/code/local/YourCompany/Category/sql/category_setup/mysql4-install-0.1.0.php にインストーラーを作成します

<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute('catalog_category','category_show_grid', array(
    'type'              => 'int',
    'group'             => 'General Information',
    'backend'           => '',
    'frontend'          => '',
    'label'             => 'Show in grid',
    'input'             => 'select',
    'class'             => '',
    'source'            => 'eav/entity_attribute_source_boolean',
    'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible'           => 1,
    'required'          => 0,
    'user_defined'      => 1,
    'default_value'     => 1,
    'searchable'        => false,
    'comparable'        => false,
    'visible_on_front'  => false,
    'unique'            => false
));
$installer->endSetup();

その後、コードでそれを呼び出すことができるはずです

if ($_category->getIsActive() && $_category->getCategoryShowGrid()) {
}
于 2012-07-04T21:34:40.930 に答える