在Magento - Edit01中将Meta关键字条件添加到IF

时间:2012-07-04 00:19:12

标签: php magento

此代码(1)的目的是以网格形式在Magento商店中显示子类别。类别图像的大小在代码本身中定义,而分隔和宽度在styles.css中。

如果我正常使用代码,我会获得所有已启用的子类别(IsActive为true)。请参阅此“正在进行的工作”链接:http://www.aldetal.biz/index.php/vabaro/fitness/nutricion.html

我更喜欢显示已启用的子类别,并在其他地方标记为“显示有价值”。这样我就可以在登录页面中以网格形式显示多少个子类别,从菜单中显示的子类别数量。为避免触及数据库,我将使用Meta Keywords字段将字符串showgrid添加为“关键字”标志。

代码

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

出于某种原因,Tim提出了优秀的IF。

(1)最初由Jake Rutter撰写:http://www.onerutter.com/web/magento-custom-category-images-listing-block-tutorial.html并由我调整


我有以下构造的代码来显示Magento前端的子类别。子类别在启用时显示(“IsActive”为真)。

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

我想在不弄乱数据库的情况下添加另一个条件,因此我正在使用Meta关键字字段(因为Google正式忽略它,并且无论如何都不会有任何伤害。)

如果类别已启用(IsActive)并且该字段中的第一个字符串是“showgrid”,则IF为true,并且会发生一大堆事情。

但小心:元关键字字段可能为空,因此我需要对其进行测试以及确定其内容。如果未启用该类别(IsActive为false)或Meta关键字为空或Meta关键字不以showgrid开头,则其他内容发生

1 个答案:

答案 0 :(得分: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()) {
}