Tanikum
September 6, 2019, 9:30pm
1
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?
bobray
September 7, 2019, 3:33am
2
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
aiden88
September 7, 2019, 7:22am
3
Works. This is really helpful.
1 Like
Tanikum
September 7, 2019, 9:37am
4
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
Tanikum
September 7, 2019, 9:38am
5
$resource->set('class_key', 'msProduct'); <-- this doesn't work
1 Like
Tanikum
September 7, 2019, 10:09am
6
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
Tanikum
September 7, 2019, 12:06pm
7
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
nuan88
September 7, 2019, 2:11pm
8
Thanks for the working code, it will help people down the road
1 Like
bobray
September 7, 2019, 8:47pm
9
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.