Skip to content

Instantly share code, notes, and snippets.

@grachov
Last active August 14, 2018 15:36

Revisions

  1. grachov revised this gist May 22, 2014. No changes.
  2. grachov created this gist Apr 30, 2014.
    3 changes: 3 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    Установка:
    - файл `unpublish.class.php` разместить в директории `core/components/product_unpublish/processors/resource/`
    - создать плагин, скопировав содержимое файла `plugin.php`. Если необходимо снимать с публикации товары сразу после покупки, отмечаем для плагина событие `msOnCreateOrder`. Если необходимо снимать с публикации товары при получении заказом определенного статуса, необходимо отметить событие `msOnChangeOrderStatus` для плагина и поменять в коде `99` на ID статуса заказа.
    51 changes: 51 additions & 0 deletions plugin.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,51 @@
    <?php
    /**
    * @var array $scriptProperties
    */
    if (!function_exists('unpublishProduct')) {
    function unpublishProduct($modx, $id)
    {
    if (empty($modx->error)) {
    $modx->getService('error', 'modError');
    } else {
    $modx->error->reset();
    }
    /**
    * @var modProcessorResponse $response
    */
    $response = $modx->runProcessor('resource/unpublish', array(
    'id' => $id,
    ), array(
    'processors_path' => $modx->getOption('product_unpublish.core_path', null, $modx->getOption('core_path', null, MODX_CORE_PATH) . 'components/product_unpublish/') . 'processors/',
    ));
    if ($response->isError()) {
    $modx->log(modX::LOG_LEVEL_ERROR, 'Error occurred while unpublishing the resource with ID ' . $id . ': ' . $response->getMessage());
    }
    }
    }
    switch ($modx->event->name) {
    case 'msOnCreateOrder':
    $order = $modx->getOption('msOrder', $scriptProperties);
    if (!is_object($order)) {
    return;
    }
    foreach ($order->getMany('Products') as $orderProduct) {
    unpublishProduct($modx, $orderProduct->get('product_id'));
    }
    break;
    case 'msOnChangeOrderStatus':
    if ($modx->getOption('status', $scriptProperties) != 99) {
    return;
    }
    $order = $modx->getOption('order', $scriptProperties);
    if (!is_object($order)) {
    return;
    }
    foreach ($order->getMany('Products') as $orderProduct) {
    unpublishProduct($modx, $orderProduct->get('product_id'));
    }
    break;
    }
    if (!empty($modx->error)) {
    $modx->error->reset();
    }
    27 changes: 27 additions & 0 deletions unpublish.class.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    <?php
    require_once MODX_CORE_PATH . 'model/modx/processors/resource/unpublish.class.php';

    class CustomResourceUnPublishProcessor extends modResourceUnPublishProcessor
    {
    public function checkPermissions()
    {
    return true;
    }

    public function initialize()
    {
    $id = $this->getProperty('id', false);
    if (empty($id)) {
    return $this->modx->lexicon('resource_err_ns');
    }
    $this->resource = $this->modx->getObject('modResource', $id);
    if (empty($this->resource)) {
    return $this->modx->lexicon('resource_err_nfs', array(
    'id' => $id,
    ));
    }
    return true;
    }
    }

    return 'CustomResourceUnPublishProcessor';