How to change Resource type of all resources within a catalog?

It turned out that some catalogs contain goods with resource type - Document, but I need msProduct type to manipulate them

I searched, but I could not find anywhere how to change the resource type with snippet or any other method

Please tell me if it is realizable and if so, how?

I’m not sure this will work or if it’s safe, but as far as I can tell, MODX doesn’t use that field for anything. I could be wrong!.

By ‘catalog’ do you mean children of a certain parent?

This would change the value of that field for all existing children of resource 12:

$docs = $modx->getCollection('modResource', array('parent' => 12));

foreach ($docs as $doc) {
    $doc->set('type', 'newValue');
    $doc->save();
}

For future resources, you could create a plugin attached to OnDocFormSave like this:

if ($resource->get('parent') == 12) {
     $resource->set('type', 'newValue');
     $resource->save();
}

Use at your own risk! I’d recommend backing up the database before trying this.

1 Like

Works. This is really helpful.

1 Like

Parent

Yeah, I’m just talking about how ‘resource type’ should look
I’ve seen it assigned as a class_key, but I’ve tried and failed to change it.

1 Like
$resource->set('class_key', 'msProduct'); <-- this doesn't work
1 Like

Here’s the full code I tried.

$array_ids = $modx->getChildIds(12);

foreach($array_ids as $id){
  $product = $modx->getObject('modResource', $id);
  $product->set('class_key', 'msProduct');
  $product->save();
}

But it just ends immediately.
I can’t find where I made a mistake

1 Like

A member of the forum helped me
Here’s the solution.

$child = $modx->getChildIds(12, 10, array(‘context’ => ‘web’));
$q = $modx->newQuery(‘modResource’);
$q->where(array(
‘id:IN’ => $child,
‘published’ => true,
‘Deleted’ => false,
‘searchable’ => true,
));

$products = $modx->getIterator(‘modResource’, $q);

foreach ($products as $k => $product) {
$product->set(‘class_key’, ‘msProduct’);
$product->save();
}

1 Like

Thanks for the working code, it will help people down the road

1 Like

Unless you (or some extra you’ve installed) have created a custom resource class that extends modResource and used that for the class_key, you might run into serious trouble by modifying the class_key field. MODX and xPDO use it extensively, that’s why the type field would be more appropriate.