从终端将订单导出到 X-Cart 5 中的 .xslx 文件

问题描述 投票:0回答:2

我想设置一个 cron 作业并按计划将订单导出到 .xlsx 文件。

我想知道是否有一种简单的方法可以使用linux控制台命令在X-Cart 5中设置订单导出?

php terminal cron export x-cart
2个回答
1
投票
  • 您可以设置REST API模块来按条件获取订单

https://devs.x-cart.com/rest-api/

https://devs.x-cart.com/rest-api/cookbook.html

https://devs.x-cart.com/rest-api/v5.4.0.8/Core/Order.html

https://market.x-cart.com/addons/rest-api.html

  • 然后使用一些控制台 HTTP 代理 例如 wget

    wget --header="Content-Type: application/json" --method=GET --no-check-certificate -q -O /tmp/somefile.txt 'https://HOST/src/admin.php?target=RESTAPI&_key=h1cvNg34654ZCSVWpMokqUqI&_path=order'
    
  • (可选)全部转换文件后 /tmp/somefile.txt 到 .xlsx 文件。 通过一些离线或在线工具


0
投票

我对X-Cart 5机制有了一些了解,并编写了以下解决方案。如果这项研究对其他人有帮助,我会很高兴。


开始导出文件的 Bash 脚本:
/path_to_project/script/export.sh

包含代码:

#!/usr/bin/env bash
cd /path_to_project/

iteration=0

is_event_setted=$(php "console.php" --target=export --action=set_event_task $i);

if [ $is_event_setted == 'OK' ] ; then
  while [ 1 = 1 ] ; do
    (( iteration++ ))
    echo $iteration

    is_export_not_finished=$(php "console.php" --target=export --action=sh_is_export_not_finished $i);

    if [ $is_export_not_finished == 'export_not_finished' ] ; then
      echo "Export continues..."
      php "console.php" --target=export --action=run_event_task
    else
      echo "Export was finished."
      break
    fi
  done
fi



导出管理文件中进程的控制台控制器:
/path_to_project/classes/XLite/Controller/Console/Export.php

包含代码:

<?php
// vim: set ts=4 sw=4 sts=4 et:

/**
 * Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
 * See https://www.x-cart.com/license-agreement.html for license details.
 */

namespace XLite\Controller\Console;

class Export extends \XLite\Controller\Console\AConsole
{
    /**
     * Generator
     *
     * @var \XLite\Logic\Export\Generator
     */
    protected $generator;

    private $section = [
//         'XLite\Logic\Export\Step\Products',
//         'XLite\Logic\Export\Step\Attributes',
//         'XLite\Logic\Export\Step\AttributeValues\AttributeValueCheckbox',
           'XLite\Logic\Export\Step\Orders',
//         'XLite\Logic\Export\Step\Categories',
//         'XLite\Logic\Export\Step\ProductsCustom',
//         'XLite\Logic\Export\Step\Users',
//         'XLite\Module\XC\Reviews\Logic\Export\Step\Reviews',
//         'XLite\Module\XC\NewsletterSubscriptions\Logic\Export\Step\NewsletterSubscribers',
//         'XLite\Module\XC\CustomProductTabs\Logic\Export\Step\CustomTabs',
//         'XLite\Module\XC\CustomProductTabs\Logic\Export\Step\GlobalTabs',
    ];

    private $options = [
        'files'     => 'local', // 'local', 'url'
        'attrs'     => 'global', // 'global', 'global_n_classes', 'all', 'none'
        'delimiter' => ',',
        'charset'   => 'UTF-8',
        'filter'    => '',
        'selection' => [],
        'type'      => 'xlsx', // 'csv', 'xls', 'xlsx'
    ];

    public function getGenerator()
    {
        if (!isset($this->generator)) {
            $state = \XLite\Core\Database::getRepo('XLite\Model\TmpVar')->getEventState($this->getEventName());
            $this->generator = $state && isset($state['options']) ? new \XLite\Logic\Export\Generator($state['options']) : false;
        }

        return $this->generator;
    }

    protected function doActionTest()
    {
        exit('Console-export test phrase');
    }

    /**
     * Delete all files
     *
     * @return void
     */
    protected function doActionDeleteFiles()
    {
        $generator = new \XLite\Logic\Export\Generator();
        $generator->deleteAllFiles();

        echo "Files were deleted successfully!\n";
    }

    protected function doActionShIsExportNotFinished()
    {
        if ($this->isExportNotFinished()) {
            exit('export_not_finished');
        } else {
            exit('export_finished');
        }
    }

    protected function doActionSetEventTask()
    {
        foreach ($this->options as $key => $value) {
            if (
                !\XLite\Core\Config::getInstance()->Export
                || \XLite\Core\Config::getInstance()->Export->$key != $value
            ) {
                \XLite\Core\Database::getRepo('XLite\Model\Config')->createOption([
                    'category' => 'Export',
                    'name'     => $key,
                    'value'    => $value,
                ]);
            }
        }

        if (in_array('XLite\Logic\Export\Step\AttributeValues\AttributeValueCheckbox', $this->section)) {
            $addSections = [
                'XLite\Logic\Export\Step\AttributeValues\AttributeValueSelect',
                'XLite\Logic\Export\Step\AttributeValues\AttributeValueText',
                'XLite\Logic\Export\Step\AttributeValues\AttributeValueHidden',
            ];

            $this->section = array_merge(
                $this->section,
                $addSections
            );
        }

        \XLite\Logic\Export\Generator::run($this->assembleExportOptions());

        exit('OK');
    }

    protected function doActionRunEventTask()
    {
        $event = $this->getEventName();
        $result = false;
        $errors = [];

        $task = \XLite\Core\Database::getRepo('XLite\Model\EventTask')->findOneBy(['name' => $event]);
        if ($task) {
            \XLite\Core\Database::getRepo('XLite\Model\EventTask')->cleanTasks($event, $task->getId());
            if (\XLite\Core\EventListener::getInstance()->handle($task->getName(), $task->getArguments())) {
                $task = \XLite\Core\Database::getEM()->merge($task);
                \XLite\Core\Database::getEM()->remove($task);

                $result = true;
            }

            $errors = \XLite\Core\EventListener::getInstance()->getErrors();

        } else {
            \XLite\Core\Database::getRepo('XLite\Model\TmpVar')->removeEventState($event);
        }

        \XLite\Core\Database::getEM()->flush();

        $state = \XLite\Core\Database::getRepo('XLite\Model\TmpVar')->getEventState($event);

        $this->setPureAction(true);

        if ($result && $state) {
            $data = [
                'percent'  => \XLite\Core\Database::getRepo('XLite\Model\TmpVar')->getEventStatePercent($event),
                'error'    => !empty($errors),
                'messages' => $errors
            ];

            if (!empty($state['touchData'])) {
                $data += $state['touchData'];
            }

            \XLite\Core\Event::eventTaskRun($data);

        } else {
            \XLite\Core\Event::eventTaskRun([
                'percent'  => 100,
                'error'    => true,
                'messages' => $errors
            ]);

            $result = false;
        }

        if ($errors) {
            foreach ($errors as $message) {
                \XLite\Core\TopMessage::addError($message);
            }

            $result = false;
        }
    }

    /**
     * Assemble export options
     *
     * @return array
     */
    protected function assembleExportOptions()
    {
        return [
            'include'       => $this->section,
            'copyResources' => 'local' == $this->options['files'],
            'attrs'         => $this->options['attrs'],
            'delimiter'     => $this->options['delimiter'],
            'charset'       => $this->options['charset'],
            'filter'        => $this->options['filter'],
            'selection'     => $this->options['selection'],
            'type'          => $this->options['type'],
        ];
    }

    /**
     * Get event name
     *
     * @return string
     */
    protected function getEventName()
    {
        return \XLite\Logic\Export\Generator::getEventName();
    }

    /**
     * Get export cancel flag name
     *
     * @return string
     */
    protected function getExportCancelFlagVarName()
    {
        return \XLite\Logic\Export\Generator::getCancelFlagVarName();
    }

    /**
     * Check - export process is not-finished or not
     *
     * @return boolean
     */
    protected function isExportNotFinished()
    {
        $state = \XLite\Core\Database::getRepo('XLite\Model\TmpVar')->getEventState($this->getEventName());

        return $state
            && in_array($state['state'], [\XLite\Core\EventTask::STATE_STANDBY, \XLite\Core\EventTask::STATE_IN_PROGRESS])
            && !\XLite\Core\Database::getRepo('XLite\Model\TmpVar')->getVar($this->getExportCancelFlagVarName());
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.