5

I am trying to set category Is Anchor default to yes for all categories I tried these database queries

UPDATE catalog_category_entity_int set value = 1 where attribute_id = 51;

query run successfully but no effect in categories

try this

UPDATE `catalog_category_int` set value = 1 where attribute_id = 51 and parent_id <> 0

return error

error:#1146 - Table 'magento1.catalog_category_int' doesn't exist

and try with this create new file in magento root directory with this code

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

// Load Up Magento Core
define('MAGENTO', realpath(''));

require_once(MAGENTO . '/app/Mage.php');

$app = Mage::app();

$categories = Mage::getModel('catalog/category')
 ->getCollection()
 ->addAttributeToSelect('*')
 ->addAttributeToFilter('is_anchor', 0)
 ->addAttributeToFilter('entity_id', array("gt" => 1))
 ->setOrder('entity_id')
 ;

foreach($categories as $category) {
 echo $category->getId() . "\t" . $category->getName() . "\n";
 $category->setIsAnchor(1);
 $category->save();
}

but no result Thanks in advance for help

4 Answers 4

9

You can do this as a single sql statement:

UPDATE catalog_category_entity_int 
SET    value = 1 
WHERE  attribute_id = (SELECT attribute_id 
                       FROM   eav_attribute 
                       WHERE  attribute_code = 'is_anchor' 
                       LIMIT  1); 
1
5

First identify the attribute id of the is_anchor attribute:

SELECT * FROM eav_attribute where attribute_code = ‘is_anchor’;

Get the attribute id (for me is 43). Now run the following query

UPDATE catalog_category_entity_int set value = 1 where attribute_id = 43;

replace 43 with your own attribute id. After this, just rebuild these indexes: category flat data, product category association, url rewrites. taken from CodersKick

1
  • The link to blog.coderskick.com is broken, but looking at Google Cache, the answer is an exact copy of what used to be on that website.
    – 7ochem
    Commented Oct 14, 2016 at 6:46
3

Fire this query from database . you get attribute_code.

SELECT * FROM eav_attribute where attribute_code = 'is_anchor';

we Get attribute id 51 in my database. Now Fire this query

UPDATE catalog_category_entity_int set value = 1 where attribute_id = 51;

replace 51 with your own attribute id. And just rebuild these indexes

0

The previous answers are wrong. For a new category there is no is_anchor attribute set, so you cant update it. You have to find the non existing categories and insert is_anchor attribute values for them.

Solution (quick shot):

<?php
require_once 'abstract.php';

class Cartware_Import_Shell_Compiler extends Mage_Shell_Abstract
{

    /**
     * Run script
     * @return void
     */
    public function run()
    {
        $resource = Mage::getSingleton('core/resource');
        $writeConnection = $resource->getConnection('core_write');

        $categoryTable = $resource->getTableName('catalog/category');
        $eavAttributeTable = $resource->getTableName('eav/attribute');
        $eavAttributeIntCategoryTable = $resource->getTableName('catalog_category_entity_int');

        //fetch is anchor attribute_id
        $query = "SELECT attribute_id 
                    FROM   {$eavAttributeTable}
                    WHERE  attribute_code = 'is_anchor'
                    LIMIT  1";

        $id = $writeConnection->fetchOne($query);

        //update all categories to is_anchor = 1
        $query = "UPDATE {$eavAttributeIntCategoryTable} SET value = 1 WHERE attribute_id = {$id}";
        $writeConnection->query($query);

        //fetch all catgegory ids that are is_anchor = 1
        $query = "SELECT entity_id FROM {$eavAttributeIntCategoryTable} WHERE attribute_id = {$id} AND value = 1";
        $results = $writeConnection->fetchAll($query);
        $idsWithAnchor = array();

        foreach ($results as $key => $value){

            $idsWithAnchor[] = $value['entity_id'];

        }

        //fetch all category ids that have no is_anchor attribute set (default)
        $idsWithAnchorFormatted = implode(",", $idsWithAnchor);
        $query = "SELECT entity_id FROM {$categoryTable} WHERE entity_id NOT IN ($idsWithAnchorFormatted)";
        $results = $writeConnection->fetchAll($query);

        //insert all is_anchor attributes that are not existing
        foreach ($results as $key => $value){

            $entity_id = $value['entity_id'];
            $query = "INSERT INTO {$eavAttributeIntCategoryTable}(entity_type_id, attribute_id, store_id, entity_id, value) VALUES(3, {$id}, 0, {$entity_id}, 1)";

            echo $query . PHP_EOL;

            //$writeConnection->query($query);
        }
    }
}

$shell = new Cartware_Import_Shell_Compiler();
$shell->run();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.