Guest User

Untitled

a guest
Sep 19th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. <?php
  2. /**
  3. * Commerce Product Resource Sync
  4. *
  5. * Syncs all the attached Commerce TV product's target field to point
  6. * to the resource that it was saved from for quick linking to the resource.
  7. *
  8. * NOTE: Using this sync, products can only belong to one resource to link back to.
  9. */
  10.  
  11. switch ($modx->event->name) {
  12. case 'OnDocFormSave':
  13. // Replace products with the TV name
  14. $productsTV = $resource->getTVValue('products');
  15. if (!$productsTV) {
  16. return;
  17. }
  18.  
  19. // Load Commerce, required to get the model for comProduct
  20. $path = $modx->getOption('commerce.core_path', null, MODX_CORE_PATH . 'components/commerce/') . 'model/commerce/';
  21. $params = ['mode' => $modx->getOption('commerce.mode')];
  22. $commerce = $modx->getService('commerce', 'Commerce', $path, $params);
  23.  
  24. // Since product ids are stored as a comma seperated list in the TV, we can
  25. // extract it into an array to easily manipulate each product
  26. $productIds = explode(',', $productsTV);
  27.  
  28. foreach ($productIds as $productId) {
  29. $product = $modx->getObject('comProduct', $productId);
  30. // Make sure product hasn't been removed externally
  31. if (!$product) {
  32. continue;
  33. }
  34. // Set the target referring back to the modResource id.
  35. $product->set('target', $id);
  36. $product->save();
  37. }
  38.  
  39. break;
  40. }
Add Comment
Please, Sign In to add comment