Jump to content

Add 'reference' field in Order creation


Recommended Posts

Hi,

 

I would like to know how I can add a Reference field in the form for Order Creation, like this :

 

5a5e1c8ecaa75_Image6.thumb.png.dbe127024366c4786193877131eb6740.png

(I edited the HTML source, there's no php code behind)

 

What's the most "prestashop way" to add this form field ? I would to it via a file in /override (or /modules/my_module/override). I would preferably avoid to copy the whole controller/template, paste it to /override and edit it!!

 

I'm coming from symfony, so I was looking for some kind of $form->add('reference') but I can't find anything like this for the order creation on PS's github.

 

Thanks !

Florian

 

PS: it's for prestashop 1.7

Edited by Florian
more details (see edit history)

Share this post


Link to post
Share on other sites

Hi, not yet unfortunately..

 

I'm using prestashop as a B2B ERP to replace my old excel files, so I can track every payment, invoice, customers, and so on. So I need to have control on every field (at creation and modification). I successfully added my own module with CRUD for every field of payment/invoice/order state/order, e.g :

 

5a621068a3281_Image2.thumb.png.a2c4f8d1be82a5373242fbf90b079643.png

 

5a62106a16f26_Image3.thumb.png.f8e7e18c1e5d3a7387174d173e18b6ca.png

 

I would love to be able to edit the order creation flow form, also for the date_add field

Share this post


Link to post
Share on other sites

ok, I find, override AdminOrdersController.php 

the function ajaxProcessSendMailValidateOrder for update the cart with my new value get form the form.tpl.

For you if you want save your field on "create order" look postProcess function

  • Like 1

Share this post


Link to post
Share on other sites

Well done,

 

I could add my reference field using code below:

 

admin-folder/themes/default/template/controllers/orders/form.tpl
add the following around l. 1524:
<div class="form-group">
  <label class="control-label col-lg-3" for="order_reference">{l s='Order reference' d='Admin.Orderscustomers.Feature'}</label>
  <div class="col-lg-6">
    <input name="order_reference" id="order_reference" type="text"/>
  </div>
</div>
/override/controllers/admin/AdminOrdersController.php

class AdminOrdersController extends AdminOrdersControllerCore {
  public function __construct() {
    parent::__construct();
  }

public function postProcess(){
    // taken from https://github.com/PrestaShop/PrestaShop/blob/develop/controllers/admin/AdminOrdersController.php#L1204
    if (Tools::isSubmit('submitAddOrder') && ($id_cart = Tools::getValue('id_cart')) &&
            ($module_name = Tools::getValue('payment_module_name')) &&
            ($id_order_state = Tools::getValue('id_order_state')) && Validate::isModuleName($module_name)) {
        if ($this->access('edit')) {
          if(empty(Tools::getValue('order_reference'))){
            // the following is not working but would be great...
            // $this->errors[] = $this->trans('The Reference field is invalid.', [], 'Admin.Notifications.Error');
            // return parent::renderForm();
          }
            if (!Configuration::get('PS_CATALOG_MODE')) {
                $payment_module = Module::getInstanceByName($module_name);
            } else {
                $payment_module = new BoOrder();
            }
            $cart = new Cart((int)$id_cart);
            Context::getContext()->currency = new Currency((int)$cart->id_currency);
            Context::getContext()->customer = new Customer((int)$cart->id_customer);
            $bad_delivery = false;
            if (($bad_delivery = (bool)!Address::isCountryActiveById((int)$cart->id_address_delivery))
                || !Address::isCountryActiveById((int)$cart->id_address_invoice)) {
                if ($bad_delivery) {
                    $this->errors[] = $this->trans('This delivery address country is not active.', array(), 'Admin.Orderscustomers.Notification');
                } else {
                    $this->errors[] = $this->trans('This invoice address country is not active.', array(), 'Admin.Orderscustomers.Notification');
                }
            } else {
                $employee = new Employee((int)Context::getContext()->cookie->id_employee);
                $result = $payment_module->validateOrder(
                    (int)$cart->id, (int)$id_order_state,
                    $cart->getOrderTotal(true, Cart::BOTH), $payment_module->displayName, $this->trans('Manual order -- Employee:', array(), 'Admin.Orderscustomers.Feature').' '.
                    substr($employee->firstname, 0, 1).'. '.$employee->lastname, array(), null, false, $cart->secure_key
                );
                if ($payment_module->currentOrder) {
                    $reference = Tools::getValue('order_reference'); // <--- added these 5 lines
                    if(!empty($reference)){
                        $order = new Order($payment_module->currentOrder);
                        $order->reference = $reference;
                        $order->update();
                    }

                    Tools::redirectAdmin(self::$currentIndex.'&id_order='.$payment_module->currentOrder.'&vieworder'.'&token='.$this->token);
                }
            }
        }
    }
		parent::postProcess(); // use default behaviour
	}

It's a shame  there's no hook in the controller and we have to copy/paste code from the source...

It's also surprising that form.tpl is not ussing any "block" so if we want to change it, we have to change the whole file.

What'll happen when Ps will be upgraded ? ...

 

Thanks @a17000

 

Edited by Florian
more details (see edit history)

Share this post


Link to post
Share on other sites

14 hours ago, Florian said:

Well done,

 

I could add my reference field using code below:

 


admin-folder/themes/default/template/controllers/orders/form.tpl
add the following around l. 1524:
<div class="form-group">
  <label class="control-label col-lg-3" for="order_reference">{l s='Order reference' d='Admin.Orderscustomers.Feature'}</label>
  <div class="col-lg-6">
    <input name="order_reference" id="order_reference" type="text"/>
  </div>
</div>

/override/controllers/admin/AdminOrdersController.php

class AdminOrdersController extends AdminOrdersControllerCore {
  public function __construct() {
    parent::__construct();
  }

public function postProcess(){
    // taken from https://github.com/PrestaShop/PrestaShop/blob/develop/controllers/admin/AdminOrdersController.php#L1204
    if (Tools::isSubmit('submitAddOrder') && ($id_cart = Tools::getValue('id_cart')) &&
            ($module_name = Tools::getValue('payment_module_name')) &&
            ($id_order_state = Tools::getValue('id_order_state')) && Validate::isModuleName($module_name)) {
        if ($this->access('edit')) {
          if(empty(Tools::getValue('order_reference'))){
            // the following is not working but would be great...
            // $this->errors[] = $this->trans('The Reference field is invalid.', [], 'Admin.Notifications.Error');
            // return parent::renderForm();
          }
            if (!Configuration::get('PS_CATALOG_MODE')) {
                $payment_module = Module::getInstanceByName($module_name);
            } else {
                $payment_module = new BoOrder();
            }
            $cart = new Cart((int)$id_cart);
            Context::getContext()->currency = new Currency((int)$cart->id_currency);
            Context::getContext()->customer = new Customer((int)$cart->id_customer);
            $bad_delivery = false;
            if (($bad_delivery = (bool)!Address::isCountryActiveById((int)$cart->id_address_delivery))
                || !Address::isCountryActiveById((int)$cart->id_address_invoice)) {
                if ($bad_delivery) {
                    $this->errors[] = $this->trans('This delivery address country is not active.', array(), 'Admin.Orderscustomers.Notification');
                } else {
                    $this->errors[] = $this->trans('This invoice address country is not active.', array(), 'Admin.Orderscustomers.Notification');
                }
            } else {
                $employee = new Employee((int)Context::getContext()->cookie->id_employee);
                $result = $payment_module->validateOrder(
                    (int)$cart->id, (int)$id_order_state,
                    $cart->getOrderTotal(true, Cart::BOTH), $payment_module->displayName, $this->trans('Manual order -- Employee:', array(), 'Admin.Orderscustomers.Feature').' '.
                    substr($employee->firstname, 0, 1).'. '.$employee->lastname, array(), null, false, $cart->secure_key
                );
                if ($payment_module->currentOrder) {
                    $reference = Tools::getValue('order_reference'); // <--- added these 5 lines
                    if(!empty($reference)){
                        $order = new Order($payment_module->currentOrder);
                        $order->reference = $reference;
                        $order->update();
                    }

                    Tools::redirectAdmin(self::$currentIndex.'&id_order='.$payment_module->currentOrder.'&vieworder'.'&token='.$this->token);
                }
            }
        }
    }
		parent::postProcess(); // use default behaviour
	}

 

What'll happen when Ps will be upgraded ? ...

 

 

 

good question... I think presta need a good project manager

Share this post


Link to post
Share on other sites

  • 2 weeks later...

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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More