Jump to content

ADD ORDER NOTE FOR ALL ORDERS


seok

Recommended Posts

I am looking for edit Order Notes to add a smarty code and the file of PS 8.0.3 src/PrestaShopBundle/Resources/views/Admin/Sell/Order/Order/Blocks/View/internal_note.html.twig show the next code:

 

    <div class="col-md-12 mt-3 js-order-notes-block{% if not isNoteOpen %} d-none{% endif %}">
      {{ form_start(internalNoteForm, {
        'action': path('admin_orders_set_internal_note', {'orderId': orderForViewing.getId()})
      }) }}
      {{ form_widget(internalNoteForm.note) }}

      <div class="d-none">
        {{ form_rest(internalNoteForm) }}
      </div>
 

I remarked in bold that i am trying to modify, in back office from firebug the form_start(internalNoteForm not show <textarea id="internal_note_note" name="internal_note[note]" like firebug that is where I want add my smarty code.

 

In what file is the code "textarea" of form_start path('admin_orders_set_internal_note ?

Edited by seok (see edit history)
Link to comment
Share on other sites

thank you for repply, 

internal_note is not in OrderController.php

Now I saw order_note is the first box of the left that you see in order detail and it is showned in order list. Internal note is other different note

I am looking for the .tpl file of order note

regards

Edited by seok (see edit history)
Link to comment
Share on other sites

thanks, then should be more easy add a custom colum but internal note is not the note showned in order list of backoffice, the right is order note, i think is OrderMessageType.php

I had in prestashop order list backoffice a code showning the estimated date of delivery. It worked in PS 1.7.0 but in Prestashop 8 AdminOrdersControllers.php was moved from classes and now i dont know how add my code, I resume the code:

AdminOrderController.php (old PS 1.7.0)

In:

public function __construct()
    {
        $this->bootstrap = true;
        $this->table = 'order';
        $this->className = 'Order';
        $this->lang = false;
        $this->addRowAction('view');
        $this->explicitSelect = true;
        $this->allow_export = true;
        $this->deleted = false;

        parent::__construct();

        $this->_select = '

 

I added then:

(SELECT (DATE_FORMAT(DATE_ADD(op.`date_add`,INTERVAL 7 + IF((WEEKDAY(DATE_ADD(op.`date_add`, INTERVAL 7 DAY)) IN (5)), 2, 0) + IF((WEEKDAY(DATE_ADD(op.`date_add`, INTERVAL 7 DAY)) IN (6)), 1, 0) DAY), "%W, %e of %M")) FROM `'._DB_PREFIX_.'order_payment` op WHERE op.`order_reference` = a.`reference` limit 1) as DayOfDelivery,

 

then after:

$this->fields_list = array(
            'id_order' => array(
                'title' => $this->trans('ID', array(), 'Admin.Global'),
                'align' => 'text-center'
            ),

I added:

            'DayOfDelivery' => array(
                'title' => $this->l('Day Of Delivery'),
                'align' => 'center',
                'orderby' => false,
                'search' => false,
                'class' => 'fixed-width-xs',
                'callback' => 'getIsDayOfDelivery'
            ),

then I added a function:

    public function getIsDayOfDelivery ($a, $order)
    {
        if ($order['note']) {
            return '<span title="" data-toggle="tooltip" class="label-tooltip" data-original-title="'.$order['note'].'" data-html="true" data-placement="top"><i class="icon-truck"></i></span>';
        } 
        else {
            return '<span title="" data-toggle="tooltip" class="label-tooltip" data-original-title="'.$order['DayOfDelivery'].'" data-html="true" data-placement="top"><i class="icon-truck"></i></span>';
        }
    }

 

Do you know how add this code in src/Core/Grid/Definition/Factory/OrderGridDefinitionFactory.php or in src/PrestaShopBundle/Controller/Admin/Sell/Order/OrderController.php ?

Link to comment
Share on other sites

In the list of orders, you can display your own column and values by embedding four hooks.

actionOrderGridDefinitionModifier, actionOrderGridQueryBuilderModifier, actionOrderGridDataModifier, actionAdminOrdersListingFieldsModifier.

You can define your changes in the hookActionAdminOrdersListingFieldsModifier.

E.g:

public function hookActionAdminOrdersListingFieldsModifier(array $params)
{

    if (isset($params['select'])) {
        $params['select'] .= ', DATE_FORMAT(DATE_ADD(op.`date_add`,INTERVAL 7 + IF((WEEKDAY(DATE_ADD(op.`date_add`, INTERVAL 7 DAY)) IN (5)), 2, 0) + IF((WEEKDAY(DATE_ADD(op.`date_add`, INTERVAL 7 DAY)) IN (6)), 1, 0) DAY), "%W, %e of %M") as `DayOfDelivery`';
    }

    if (isset($params['join'])) {
        $params['join'] .= 'LEFT JOIN ' . _DB_PREFIX_ . 'order_payment AS op ON (a.reference = op.order_reference)';
    }

    $params['fields']['DayOfDelivery']['callback'] = 'getIsDayOfDelivery';
    $params['fields']['DayOfDelivery']['callback_object'] = $this;

}

public function getIsDayOfDelivery($a, $order)
{
    if ($order['note']) {
        return '<span title="" data-toggle="tooltip" class="label-tooltip" data-original-title="'.$order['note'].'" data-html="true" data-placement="top"><i class="icon-truck"></i></span>';
    } else {
        return '<span title="" data-toggle="tooltip" class="label-tooltip" data-original-title="'.$order['DayOfDelivery'].'" data-html="true" data-placement="top"><i class="icon-truck"></i></span>';
    }
}

 

Or another way.
Capture the change in order status when it is written into the ps_order_payment table and fill your date into ps_orders and the DayOfDelivery column. Alternatively, override ./classes/order/Order.php and the addOrderPayment function.
Then you can see the data straight away in your new column.

 

Edited by ps8moduly.cz (see edit history)
Link to comment
Share on other sites

Therefore, in order to help you, I need accurate information.
1. When do you enter the expected delivery date into the ps_orders table and the DayOfDelivery column?
2. What columns can you see in the list of orders? (insert a screenshot of your table and an arrow where the columns will be)
3. Should the columns in the list of orders be filterable?

It should be noted that this is a developer forum, not a "Do the work for me for free" forum.

Edited by ps8moduly.cz (see edit history)
Link to comment
Share on other sites

Hello

1) When order is ordered from customer "date_add"

2) I attach screenshot, the columns are the standard plus a module of carrier what was installed as test

3) No, is not required to be filterable and searchable

Yes, I understand it, I thought the same code working before PS 1.7.7 should be work in PS 1.8, the carrier column working in backoffice screenshot was a backoffice module of PS 1.6 and it works in PS 8. Understand what is very hard add columns with data in PS 8 for someone with little knowledge

BACKOFFICE.jpg

Link to comment
Share on other sites

Order notes display on an order's details screen, and they're useful for attaching additional information about an order. If a customer gives you special instructions, or there's anything about a specific order that you or your staff need to remember, then you can attach a note to the order during checkout.

Link to comment
Share on other sites

yes, is this module https://github.com/Matt75/displayordercarrier/releases/tag/v1.1.0

I installed to try modify adding my code but I have not knowledge to do it, before PS 1.7.7 was easy, simply I copied the LEFTJOIN lines and modified them by order_notes.

Will be more easy for me add directly the code in files src\Core\Grid\Definition\Factory\OrderGridDefinitionFactory.php or src\Core\Grid\Query\OrderQueryBuilder.php

Link to comment
Share on other sites

in top of your module:

use PrestaShop\PrestaShop\Core\Grid\Column\Type\DataColumn;
use PrestaShop\PrestaShop\Core\Grid\Filter\Filter;
use Symfony\Component\Form\Extension\Core\Type\TextType; 
use PrestaShop\PrestaShop\Core\Grid\Data\GridData;
// class your_module extends Module ......

 

and install function add hooks:

$this->registerHook('actionOrderGridDefinitionModifier');
$this->registerHook('actionOrderGridQueryBuilderModifier');
$this->registerHook('actionOrderGridDataModifier');

 

and hooks and functions:

    public function hookActionOrderGridDefinitionModifier(array $params)
    {
        $definition = $params['definition'];

        if (empty($params['definition'])) {
            return;
        }
        

        $definition
            ->getColumns()
            ->addAfter(
                'osname',
                (new DataColumn('DayOfDelivery'))
                ->setName($this->l('Day Of Delivery'))
                ->setOptions([
                    'field' => 'DayOfDelivery',
                ])
		);

        $definition->getFilters()->add(
            (new Filter('DayOfDelivery', TextType::class))
            ->setAssociatedColumn('DayOfDelivery')
            ->setTypeOptions([
                'required' => false,
            ])
        );

    }

    public function hookActionOrderGridQueryBuilderModifier( array $params ) 
    {
        if (empty($params['search_query_builder']) || empty($params['search_criteria'])){
            return;
        } 

        $searchQueryBuilder = $params['search_query_builder'];

        $searchCriteria = $params['search_criteria'];
            
        $searchQueryBuilder->addSelect('o.`DayOfDelivery` as `day_of_delivery`, opa.`date_add` AS `opa_date_add`');
        $searchQueryBuilder->leftJoin(
            'o',
            '`' . _DB_PREFIX_ . 'order_payment`',
            'opa',
            'opa.`order_reference` = o.`reference`'
        );

        if ('DayOfDelivery' === $searchCriteria->getOrderBy()) {
            $searchQueryBuilder->orderBy('opa.`date_add`', $searchCriteria->getOrderWay());
        }

        foreach ($searchCriteria->getFilters() as $filterName => $filterValue) {
            if ('DayOfDelivery' === $filterName) {
                $searchQueryBuilder->andWhere('opa.`date_add` = :opa_date_add');
                $searchQueryBuilder->setParameter('opa_date_add', $filterValue);
            }
        }

    }

    public function hookActionOrderGridDataModifier(array $params)
    {
        if (empty($params['data'])) {
            return;
        }

        $gridData = $params['data'];
        $modifiedRecords = $gridData->getRecords()->all();


        foreach ($modifiedRecords as $key => $data) {
            if (empty($data['DayOfDelivery'])) {
                $modifiedRecords[$key]['DayOfDelivery'] = $this->getIsDayOfDelivery($modifiedRecords[$key]['opa_date_add']);
            }
        }

        $params['data'] = new PrestaShop\PrestaShop\Core\Grid\Data\GridData(
            new PrestaShop\PrestaShop\Core\Grid\Record\RecordCollection($modifiedRecords),
            $gridData->getRecordsTotal(),
            $gridData->getQuery()
        );
    }

    public function getIsDayOfDelivery($data)
    {
        $getDateNumber = date('N', strtotime($data));

        $newDate = Tools::displayDate($data);

        if (in_array($getDateNumber, array(5, 6)) && $data) {
            $newDate = Tools::displayDate(date('Y-m-d', strtotime($data.' +2 days')));
        } 

        if ($getDateNumber < 5 && $data) {
            $newDate = Tools::displayDate(date('Y-m-d', strtotime($data.' +7 days')));
        } 

        if (!$data) {
            $newDate = '--';
        } 

        return $newDate;
    }

 

You won't learn anything by copying codes. If you want to be a developer, you need to study the documentation.

Edited by ps8moduly.cz (see edit history)
  • Like 1
Link to comment
Share on other sites

The module create table but delivery day is not showned.

Can you say me what is wrong in file?

regards

use PrestaShop\PrestaShop\Core\Grid\Column\Type\DataColumn;
use PrestaShop\PrestaShop\Core\Grid\Filter\Filter;
use Symfony\Component\Form\Extension\Core\Type\TextType; 
use PrestaShop\PrestaShop\Core\Grid\Data\GridData;
if (!defined('_PS_VERSION_')) {
    exit;
}

class DayOfDelivery extends Module
{
    protected $config_form = false;

    public function __construct()
    {
        $this->name = 'dayofdelivery';
        $this->tab = 'administration';
        $this->version = '1.0.0';
        $this->author = 'DayOfDelivery';
        $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('DayOfDelivery');
        $this->description = $this->l('Day Of Delivery prestashop module');

        $this->ps_versions_compliancy = array('min' => '1.7', '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('DAYOFDELIVERY_LIVE_MODE', false);

        return parent::install() &&
$this->registerHook('actionOrderGridDefinitionModifier');
$this->registerHook('actionOrderGridQueryBuilderModifier');
$this->registerHook('actionOrderGridDataModifier');
    }

    public function uninstall()
    {
        Configuration::deleteByName('DAYOFDELIVERY_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('submitDayOfDeliveryModule')) == 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 = 'submitDayOfDeliveryModule';
        $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' => 'DAYOFDELIVERY_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' => 'DAYOFDELIVERY_ACCOUNT_EMAIL',
                        'label' => $this->l('Email'),
                    ),
                    array(
                        'type' => 'password',
                        'name' => 'DAYOFDELIVERY_ACCOUNT_PASSWORD',
                        'label' => $this->l('Password'),
                    ),
                ),
                'submit' => array(
                    'title' => $this->l('Save'),
                ),
            ),
        );
    }

    /**
     * Set values for the inputs.
     */
    protected function getConfigFormValues()
    {
        return array(
            'DAYOFDELIVERY_LIVE_MODE' => Configuration::get('DAYOFDELIVERY_LIVE_MODE', true),
            'DAYOFDELIVERY_ACCOUNT_EMAIL' => Configuration::get('DAYOFDELIVERY_ACCOUNT_EMAIL', '[email protected]'),
            'DAYOFDELIVERY_ACCOUNT_PASSWORD' => Configuration::get('DAYOFDELIVERY_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 hookDisplayBackOfficeHeader()
    {
        if (Tools::getValue('configure') == $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');
    }

public function hookActionOrderGridDefinitionModifier(array $params)
    {
        $definition = $params['definition'];

        if (empty($params['definition'])) {
            return;
        }
        

        $definition
            ->getColumns()
            ->addAfter(
                'osname',
                (new DataColumn('DayOfDelivery'))
                ->setName($this->l('Day Of Delivery'))
                ->setOptions([
                    'field' => 'DayOfDelivery',
                ])
		);

        $definition->getFilters()->add(
            (new Filter('DayOfDelivery', TextType::class))
            ->setAssociatedColumn('DayOfDelivery')
            ->setTypeOptions([
                'required' => false,
            ])
        );

    }

    public function hookActionOrderGridQueryBuilderModifier( array $params ) 
    {
        if (empty($params['search_query_builder']) || empty($params['search_criteria'])){
            return;
        } 

        $searchQueryBuilder = $params['search_query_builder'];

        $searchCriteria = $params['search_criteria'];
            
        $searchQueryBuilder->addSelect('o.`DayOfDelivery` as `day_of_delivery`, opa.`date_add` AS `opa_date_add`');
        $searchQueryBuilder->leftJoin(
            'o',
            '`' . _DB_PREFIX_ . 'order_payment`',
            'opa',
            'opa.`order_reference` = o.`reference`'
        );

        if ('DayOfDelivery' === $searchCriteria->getOrderBy()) {
            $searchQueryBuilder->orderBy('opa.`date_add`', $searchCriteria->getOrderWay());
        }

        foreach ($searchCriteria->getFilters() as $filterName => $filterValue) {
            if ('DayOfDelivery' === $filterName) {
                $searchQueryBuilder->andWhere('opa.`date_add` = :opa_date_add');
                $searchQueryBuilder->setParameter('opa_date_add', $filterValue);
            }
        }

    }

    public function hookActionOrderGridDataModifier(array $params)
    {
        if (empty($params['data'])) {
            return;
        }

        $gridData = $params['data'];
        $modifiedRecords = $gridData->getRecords()->all();


        foreach ($modifiedRecords as $key => $data) {
            if (empty($data['DayOfDelivery'])) {
                $modifiedRecords[$key]['DayOfDelivery'] = $this->getIsDayOfDelivery($modifiedRecords[$key]['opa_date_add']);
            }
        }

        $params['data'] = new PrestaShop\PrestaShop\Core\Grid\Data\GridData(
            new PrestaShop\PrestaShop\Core\Grid\Record\RecordCollection($modifiedRecords),
            $gridData->getRecordsTotal(),
            $gridData->getQuery()
        );
    }

    public function getIsDayOfDelivery($data)
    {
        $getDateNumber = date('N', strtotime($data));

        $newDate = Tools::displayDate($data);

        if (in_array($getDateNumber, array(5, 6)) && $data) {
            $newDate = Tools::displayDate(date('Y-m-d', strtotime($data.' +2 days')));
        } 

        if ($getDateNumber < 5 && $data) {
            $newDate = Tools::displayDate(date('Y-m-d', strtotime($data.' +7 days')));
        } 

        if (!$data) {
            $newDate = '--';
        } 

        return $newDate;
    }
}

 

backoffice.jpg

Link to comment
Share on other sites

what column name is? I copied the data of your code for column

$definition
            ->getColumns()
            ->addAfter(
                'osname',
                (new DataColumn('DayOfDelivery'))
                ->setName($this->l('Day Of Delivery'))
                ->setOptions([
                    'field' => 'DayOfDelivery',
                ])

 

Link to comment
Share on other sites

DayOfDelivery = column name in ps_orders table.

If not exists, create IT.

ALTER TABLE ps_orders ADD DayOfDelivery date DEFAULT NULL;

 

https://devdocs.prestashop-project.org/8/development/components/grid/tutorials/modify-grid-in-module/

You obviously don't read the documentation and expect to be able to just copy the codes. That way you won't understand and you won't learn anything. Here is the documentation. I have already given more advice than is healthy and without a single like post. I have no more advice.

Edited by ps8moduly.cz (see edit history)
Link to comment
Share on other sites

Now is working but only in Card payments, in Bankwire or cash on delivery any date is showned

The error was in:


$this->registerHook('actionOrderGridDefinitionModifier');
$this->registerHook('actionOrderGridQueryBuilderModifier');
$this->registerHook('actionOrderGridDataModifier');

It was fixed by:

$this->registerHook('actionOrderGridDefinitionModifier') &&
$this->registerHook('actionOrderGridQueryBuilderModifier') &&
$this->registerHook('actionOrderGridDataModifier');

If somebody want the module i can share here but he should be create manually in database the table in ps_orders "DayOfDelivery", change the bold text by the desired days

if ($getDateNumber < 5 && $data) { $newDate = Tools::displayDate(date('Y-m-d', strtotime($data.' +7 days'))); }

Then it will work for card payments

regards

 

backoffice.jpg

Link to comment
Share on other sites

Order notes display on an order's details screen, and they're useful for attaching additional information about an order. If a customer gives you special instructions, or there's anything about a specific order that you or your staff need to remember, then you can attach a note to the order during checkout.

jitter speed test

Link to comment
Share on other sites

@seok

The date is written to the ps_order_payment table in the event that the order is marked as paid.
Look in Shop parameters => Order settings => Statuses.
After opening some order status, you will see the Set the order as paid checkbox.
If checked, a record is written to the ps_order_payment table.

If there is an entry in this table, the delivery date will be displayed.

  • Like 1
Link to comment
Share on other sites

11 hours ago, jishaq4 said:

Order notes display on an order's details screen, and they're useful for attaching additional information about an order. If a customer gives you special instructions, or there's anything about a specific order that you or your staff need to remember, then you can attach a note to the order during checkout.

jitter speed test

The Order note is not being dealt with here at the moment, but something completely different.

Please read the whole thread.

Thank you

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...