0

Similar to my previous question - what SQL or PHP script can I use to remove changes to categories that were made a store view level, so that they will default back to using the "global" value?

The following code worked for removing the per-store-view settings in products, maybe it can be changed to work with categories instead?

<?php

include 'app/Mage.php'; Mage::app();

/** store id for which you would like to KEEP values */
$defaultStoreId = Mage_Catalog_Model_Product::DEFAULT_STORE_ID;

/** @var Mage_Catalog_Model_Product $model */
$model        = Mage::getModel('catalog/product');
$resource     = $model->getResource();
$entityType   = $resource->getEntityType();
$entityTypeId = $entityType->getEntityTypeId();
$adapter      = $resource->getWriteConnection();

/** gather the names of all tables that contain the attribute values to be removed */
$tables = array();

/** get all attribute value tables, making sure to leave out catalog_product_entity */
$attributes = $entityType->getAttributeCollection();
$attributes->addFieldToFilter('backend_type', array('neq' => 'static'));
foreach($attributes as $attribute) {
    /** @var Mage_Catalog_Model_Resource_Eav_Attribute $attribute */
    $tables[] = $attribute->getBackendTable();
}
$tables = array_unique($tables);

/** get product ids of all products whose SKU begins with 16- */
$collection = $model->getCollection();
$collection->addFieldToFilter('sku', array('like' => '16-%'));
$productIds = $collection->getAllIds();

$valuesDeletedCount = 0;
foreach($tables as $table) {
    $condition = array(
        'entity_type_id = ?' => $entityTypeId, // here we ensure that we are only dealing with values belonging to PRODUCTS
        'entity_id IN(?)'    => $productIds, // here we make sure that we are only dealing with SKUs beginning with 16-
        'store_id != ?'      => $defaultStoreId, // here we make sure we are deleting from ALL stores except for DEFAULT
    );
    $valuesDeletedCount += $adapter->delete($table, $condition);
}
echo $valuesDeletedCount . ' non-default attribute values deleted.';

?>
  • Try changing $model = Mage::getModel('catalog/product'); to use catalog/product, and remove the line ` 'entity_id IN(?)' => $productIds,`. Try that out. Make sure to run on a test system / database copy first! – Luke Rodgers Jan 5 '17 at 17:45
  • I don't see any change in your first comment, do you meant change it to "catalog/category" ? – Steven J Jan 5 '17 at 17:51
  • That i did ;) catalog/category – Luke Rodgers Jan 5 '17 at 17:52
  • Running this on my staging area gives an error of: Fatal error: Call to undefined method Mage_Catalog_Model_Resource_Category_Flat::getEntityType() in /home/tugasunw/staging/xyz_category_reset.php on line 11 – Steven J Jan 5 '17 at 18:37
  • Hmm, try changing to $defaultStoreId = 0; so that it pulls in from the magento admin scope, which should hopefully not use the flat tables for categories. Also change the first line to include 'app/Mage.php'; Mage::app('admin'); – Luke Rodgers Jan 5 '17 at 19:11
1

I imagine the following will work

The changes

  1. Get the category resource model directly, not the flat resource model!
  2. Removed references to products. We're resetting ALL categories to "Use default value"

Hope this works

<?php

include 'app/Mage.php'; Mage::app('admin');

/** store id for which you would like to KEEP values */
$defaultStoreId = Mage_Core_Model_App::ADMIN_STORE_ID;

$resource     = Mage::getResourceModel('catalog/category');
$entityType   = $resource->getEntityType();
$entityTypeId = $entityType->getEntityTypeId();
$adapter      = $resource->getWriteConnection();

/** gather the names of all tables that contain the attribute values to be removed */
$tables = array();

/** get all attribute value tables, making sure to leave out catalog_product_entity */
$attributes = $entityType->getAttributeCollection();
$attributes->addFieldToFilter('backend_type', array('neq' => 'static'));
foreach($attributes as $attribute) {
    /** @var Mage_Catalog_Model_Resource_Eav_Attribute $attribute */
    $tables[] = $attribute->getBackendTable();
}
$tables = array_unique($tables);

$valuesDeletedCount = 0;
foreach($tables as $table) {
    $condition = array(
        'entity_type_id = ?' => $entityTypeId, // here we ensure that we are only dealing with values belonging to PRODUCTS
        'store_id != ?'      => $defaultStoreId, // here we make sure we are deleting from ALL stores except for DEFAULT
    );
    $valuesDeletedCount += $adapter->delete($table, $condition);
}
echo $valuesDeletedCount . ' non-default attribute values deleted.';

?>
  • I'm getting this error when I run that script: Parse error: syntax error, unexpected '$entityType' (T_VARIABLE) on line 9 – Steven J Jan 5 '17 at 19:21
  • 1
    $resource line is missing a ; – Steven J Jan 5 '17 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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