Skip to content

Instantly share code, notes, and snippets.

@grachov
Last active August 14, 2018 15:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save grachov/e317e92e5168d1ff45e6 to your computer and use it in GitHub Desktop.
Save grachov/e317e92e5168d1ff45e6 to your computer and use it in GitHub Desktop.
Снятие с публикации купленных товаров для miniShop2

Установка:

  • файл unpublish.class.php разместить в директории core/components/product_unpublish/processors/resource/
  • создать плагин, скопировав содержимое файла plugin.php. Если необходимо снимать с публикации товары сразу после покупки, отмечаем для плагина событие msOnCreateOrder. Если необходимо снимать с публикации товары при получении заказом определенного статуса, необходимо отметить событие msOnChangeOrderStatus для плагина и поменять в коде 99 на ID статуса заказа.
<?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();
}
<?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';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment