Agregue la condición Meta Keyword a IF en Magento - Edit01

The intention of this code (1) is to display the subcategories in a Magento store in grid form. The size of the category images is defined in the code itself while the separation and width is in styles.css.

If I use the code normally I get all the sub-categories that are enabled (IsActive is true). See this "work in progress" link: http://www.aldetal.biz/index.php/vabaro/fitness/nutricion.html

I'd prefer to display the sub-categories that are enabled AND are flagged elsewhere as "display worthy". That way I separate how many subcategories appear in grid form in the landing page from how many appear in the menu. To avoid touching the database I'll be using the Meta Keywords field adding the string showgrid as a "keyword" flag.

Código

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

For some reason the excellent IF suggested by Tim

(1) Originally written by Jake Rutter here: http://www.onerutter.com/web/magento-custom-category-images-listing-block-tutorial.html and tweaked by me


I have code with the following construct to display sub-categories in Magento front end. The sub-categories display when they are enabled ("IsActive" is true).

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

I want to add another condition without messing with the database so I'm using the Meta Keywords field (because it is officially ignored by Google and it doesn't do any harm to have a quirky keyword anyhow).

If the category is enabled (IsActive) AND the first string in that field is "showgrid" the IF is true and the whole bunch of stuff happens.

BUT careful: the Meta Keywords field could be empty so I need to test it as well as determine its content. If the category is not enabled (IsActive is false) OR Meta Keyword is empty OR Meta Keyword does not begin with showgrid then the other stuff happens

preguntado el 04 de julio de 12 a las 01:07

1 Respuestas

A better solution than the meta tag field would be to add a new attribute to the category.

Create a directory in app/code/local/[YourCompany]/Category

Create your config.xml file in app/code/local/[YourCompany]/Category/etc/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>

Next create your module config in app/etc/modules/YourCompany_Category.xml

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

Next we create the installer in 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();

You should then be able to call it in your code

if ($_category->getIsActive() && $_category->getCategoryShowGrid()) {
}

Respondido 04 Jul 12, 22:07

Paul, thank you very much for your proposal. I'm not an expert so I accept you're probably right it is "a better solution" but I need to ask why. I keep reading that Meta Keywords are officially ignored by Google and probably so by the other search engines. Why not take advantage of an available (and otherwise useless) field? You see, I'm concerned that after creating the new attribute as you suggest, my code will still have the original problem and not work with the your IF. - miguelr

Paul, hi. How / when do I run these? I wantt some sub-catehories to show and other not and still I want them all to show on the menu (that's why I filter by "enabled" AND "showGrid". How do I define showGrid for each category? Does the attibute show in the backend or do I have to run the installer for each category? - miguelr

You should use the installer from the module which manages the entity, in this case Mage_Catalog_Model_Resource_Setup - Benmarks

I'd say it's better to have a specific setting to manage it. This will run when the module is first detected by Magento. It will add the category attribute to every category and each will have a default value of 'yes' for show grid. The attribute will show in the General Information tab on the category page. - Paul

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.