1

我正在开发一个模块,当管理员将订单状态更改为“已发货”时,它会获取订单详细信息,以使用 API 将它们发布到第三方应用程序。

我正在使用Prestashop V 1.7.7.0。

我使用的钩子是hookActionOrderStatusPostUpdate

该模块已成功安装在后台,没有语法错误,但它没有做任何事情。

无法弄清楚我的代码有什么问题。 请需要帮助。谢谢

<?php
/**
* 2007-2021 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author    PrestaShop SA <contact@prestashop.com>
*  @copyright 2007-2021 PrestaShop SA
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/

if (!defined('_PS_VERSION_')) {
    exit;
}

class Cargo extends Module
{
    protected $config_form = false;

    public function __construct()
    {
        $this->name = 'cargo';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'tekwave';
        $this->need_instance = 1;

        /**
         * Set $this->bootstrap to true if your module is compliant with bootstrap (PrestaShop 1.6)
         */
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('cargo');
        $this->description = $this->l('Ce module permet la synchronisation entre Prestashop et Cargo');

        $this->confirmUninstall = $this->l('');

        $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
    }

    /**
     * Don't forget to create update methods if needed:
     * http://doc.prestashop.com/display/PS16/Enabling+the+Auto-Update
     */
    public function install()
    {
        Configuration::updateValue('CARGO_LIVE_MODE', false);

        return parent::install() &&
            $this->registerHook('header') &&
            $this->registerHook('backOfficeHeader') &&
            $this->registerHook('actionOrderStatusPostUpdate');
    }

    public function uninstall()
    {
        Configuration::deleteByName('CARGO_LIVE_MODE');

        return parent::uninstall();
    }

    /**
     * Load the configuration form
     */
    public function getContent()
    {
        /**
         * If values have been submitted in the form, process.
         */
        if (((bool)Tools::isSubmit('submitCargoModule')) == true) {
            $this->postProcess();
        }

        $this->context->smarty->assign('module_dir', $this->_path);

        $output = $this->context->smarty->fetch($this->local_path.'views/templates/admin/configure.tpl');

        return $output.$this->renderForm();
    }

    /**
     * Create the form that will be displayed in the configuration of your module.
     */
    protected function renderForm()
    {
        $helper = new HelperForm();

        $helper->show_toolbar = false;
        $helper->table = $this->table;
        $helper->module = $this;
        $helper->default_form_language = $this->context->language->id;
        $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);

        $helper->identifier = $this->identifier;
        $helper->submit_action = 'submitCargoModule';
        $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
            .'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
        $helper->token = Tools::getAdminTokenLite('AdminModules');

        $helper->tpl_vars = array(
            'fields_value' => $this->getConfigFormValues(), /* Add values for your inputs */
            'languages' => $this->context->controller->getLanguages(),
            'id_language' => $this->context->language->id,
        );

        return $helper->generateForm(array($this->getConfigForm()));
    }

    /**
     * Create the structure of your form.
     */
    protected function getConfigForm()
    {
        return array(
            'form' => array(
                'legend' => array(
                'title' => $this->l('Settings'),
                'icon' => 'icon-cogs',
                ),
                'input' => array(
                    array(
                        'type' => 'switch',
                        'label' => $this->l('Live mode'),
                        'name' => 'CARGO_LIVE_MODE',
                        'is_bool' => true,
                        'desc' => $this->l('Use this module in live mode'),
                        'values' => array(
                            array(
                                'id' => 'active_on',
                                'value' => true,
                                'label' => $this->l('Enabled')
                            ),
                            array(
                                'id' => 'active_off',
                                'value' => false,
                                'label' => $this->l('Disabled')
                            )
                        ),
                    ),
                    array(
                        'col' => 3,
                        'type' => 'text',
                        'prefix' => '<i class="icon icon-envelope"></i>',
                        'desc' => $this->l('Enter a valid email address'),
                        'name' => 'CARGO_ACCOUNT_EMAIL',
                        'label' => $this->l('Email'),
                    ),
                    array(
                        'type' => 'password',
                        'name' => 'CARGO_ACCOUNT_PASSWORD',
                        'label' => $this->l('Password'),
                    ),
                ),
                'submit' => array(
                    'title' => $this->l('Save'),
                ),
            ),
        );
    }

    /**
     * Set values for the inputs.
     */
    protected function getConfigFormValues()
    {
        return array(
            'CARGO_LIVE_MODE' => Configuration::get('CARGO_LIVE_MODE', true),
            'CARGO_ACCOUNT_EMAIL' => Configuration::get('CARGO_ACCOUNT_EMAIL', 'contact@prestashop.com'),
            'CARGO_ACCOUNT_PASSWORD' => Configuration::get('CARGO_ACCOUNT_PASSWORD', null),
        );
    }

    /**
     * Save form data.
     */
    protected function postProcess()
    {
        $form_values = $this->getConfigFormValues();

        foreach (array_keys($form_values) as $key) {
            Configuration::updateValue($key, Tools::getValue($key));
        }
    }

    /**
    * Add the CSS & JavaScript files you want to be loaded in the BO.
    */
    public function hookBackOfficeHeader()
    {
        if (Tools::getValue('module_name') == $this->name) {
            $this->context->controller->addJS($this->_path.'views/js/back.js');
            $this->context->controller->addCSS($this->_path.'views/css/back.css');
        }
    }

    /**
     * Add the CSS & JavaScript files you want to be added on the FO.
     */
    public function hookHeader()
    {
        $this->context->controller->addJS($this->_path.'/views/js/front.js');
        $this->context->controller->addCSS($this->_path.'/views/css/front.css');
    }

这是我在hookActionOrderStatusPostUpdate函数下的代码:

    public function hookActionOrderStatusPostUpdate($params)
    {
        if ($params['newOrderStatus']->id == 6) {
            $order = new Order((int)$params['id_order']);
            $address = new Address((int)$order->id_address_delivery);
            
            $country = new Country((int)($address->id_country));
            $state = new State((int)($address->id_state));
            $message = new Message((int)($order->id_message));
            $Products = $order->getProducts();
            
            $tel_cl = $address->phone;
            $nom_prenom_cl = $address->lastname . ' ' . $address->firstname;
            $ville_cl = $country->name;
            $delegation_cl = $state->name;
            $adresse_cl = $address->address1 . ' ' . $address->address2;
            $tel_2_cl = $address->phone_mobile;
            $libelle = '';
            $nb_piece = 0;
            foreach ($Products as $product) {
                $libelle .= $product['product_name'] . '<br/>';
                $nb_piece += $product['product_quantity'];
            }
            $cod = $order->total_paid;
            $remarque = $message->message;
            $Url_str = 'http://admin.cargotunisie.com/cargo_api/set_colis_cargo_get.php?id=1&tel_cl='.$tel_cl.'&nom_prenom_cl='.$nom_prenom_cl.'&ville_cl='.$ville_cl.'&delegation_cl='.$delegation_cl.'&adresse_cl='.$adresse_cl.'&tel_2_cl='.$tel_2_cl.'&libelle='.$libelle.'&cod='.$cod.'&nb_piece='.$nb_piece.'&remarque='.$remarque;
            $json = Tools::file_get_contents($Url_str);
            $result = json_decode($json);
        }
        
    }
}
4

1 回答 1

0

这可能是由于几件事。

第一:确保钩子是钩住的。转到 Design > Positions > Then add new hook 选择你的模块名称并检查你是否可以选择钩子 actionOrderStatusPostUpdate。如果可以,则意味着它已脱钩。

这种情况比我们想的要多,有时你会发疯直到你发现它

如果这不起作用,您可以尝试这个(仅当不在生产站点上时)。如果是,只需使用 if 检查它是否来自您的 IP 来包装它。

Tools::dieObject($params);在函数开头添加。

然后更改订单并检查结果。您应该会看到存储在 $params 中的值的用户友好列表。

确保$params['newOrderStatus']存在并且$params['newOrderStatus']->id是一个对象。

此外,这应该很明显,但请确保您进行测试的订单 ID 是 6。

有了这个,您应该能够对可能发生的事情有一个大致的了解。

于 2021-04-29T12:35:14.237 回答