Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Commerce Product Resource Sync
- *
- * Syncs all the attached Commerce TV product's target field to point
- * to the resource that it was saved from for quick linking to the resource.
- *
- * NOTE: Using this sync, products can only belong to one resource to link back to.
- */
- switch ($modx->event->name) {
- case 'OnDocFormSave':
- // Replace products with the TV name
- $productsTV = $resource->getTVValue('products');
- if (!$productsTV) {
- return;
- }
- // Load Commerce, required to get the model for comProduct
- $path = $modx->getOption('commerce.core_path', null, MODX_CORE_PATH . 'components/commerce/') . 'model/commerce/';
- $params = ['mode' => $modx->getOption('commerce.mode')];
- $commerce = $modx->getService('commerce', 'Commerce', $path, $params);
- // Since product ids are stored as a comma seperated list in the TV, we can
- // extract it into an array to easily manipulate each product
- $productIds = explode(',', $productsTV);
- foreach ($productIds as $productId) {
- $product = $modx->getObject('comProduct', $productId);
- // Make sure product hasn't been removed externally
- if (!$product) {
- continue;
- }
- // Set the target referring back to the modResource id.
- $product->set('target', $id);
- $product->save();
- }
- break;
- }
Add Comment
Please, Sign In to add comment