Jump to content

Overriding Order class: new field doesn't save.


CrayFlatline

Recommended Posts

I'm trying to add custom field to the orders. It should only be filled when admin adds order manually from backend  form. Output for this field is need only in the orders list.

 

I have made next changes:

 

1) added int field order_source to database

 

2) admin/themes/default/template/controllers/orders/form.tpl

<div class="form-group">
<label class="control-label col-lg-3">Order Source</label>
<div class="col-lg-9">
<select name="order_source" id="order_source">
<option value="1" {if isset($smarty.post.order_source) && $smarty.post.order_source == 1}selected="selected"{/if}>Internal</option>
<option value="2" {if isset($smarty.post.order_source) && $smarty.post.order_source == 2}selected="selected"{/if}>External</option>
<option value="3" {if isset($smarty.post.order_source) && $smarty.post.order_source == 3}selected="selected"{/if}>Distribution</option>
</select>
</div>
</div>

3) override/controllers/admin/AdminOrdersController.php

class AdminOrdersController extends AdminOrdersControllerCore
{

	public function __construct()
	{

		parent::__construct();

		$pos = 3;
		$this->fields_list = array_slice($this->fields_list, 0, $pos, true) +
			array(
				'order_source' => array(
					'title' => $this->l('Order Source'),
					'align' => 'text-center',
					'callback' => 'orderSourceCallback',
					'type' => 'select',
					'list' => array(1 => 'Internal', 2 => 'External', 3 => 'Distribution'),
					'filter_key' => 'order_source',
					'filter_type' => 'int',
					'orderby' => false
				)
			) + array_slice($this->fields_list, $pos, count($this->fields_list)-$pos, true);
	}

}

4) override/classes/order/Order.php

class Order extends OrderCore
{
	/** @var integer Order Source */
	public $order_source;

	public function __construct($id = null, $id_lang = null)
	{
		self::$definition['fields']['order_source'] = array('type' => self::TYPE_INT);
		parent::__construct($id, $id_lang);
	}
}

5) I have deleted cache_index.php

 

Everything works great, It shows in order list. But when I create new order it doesn't save new field. I have tried to find any suggestions and find some but nothing help. When I manually change field value in the database - it shows in the list and I even can filter this column. But still can't save field value. 

 

Please help me :)

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

Hello CrayFlatline.

 

You did a great job. You are in right way to add new fields now only the problem in save that value. I think your solution is in "AdminOrdersController.php" file. check the action in which your data will be saved.

 

Oh, thanks! Great idea! I think I need to check postProcess() and add value to this field from Tools:getValue() 

 

Thanks again! :) I will try and post solution if it helps :) 

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

  • 4 months later...

Weill, I have solved it little bit another way. I have separate form on the admin order page just to change this value. Below is my overriden postProcess() 

	public function postProcess() {

		parent::postProcess();

		if (Tools::isSubmit('submitOrderSourceState')) {

			$orderId = (int)Tools::getValue('id_order');
			$statusId = (int)Tools::getValue('id_order_source');

			$query = "UPDATE " . _DB_PREFIX_ . "orders SET order_source = " . $statusId . " WHERE id_order=" . $orderId;
			$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($query);
		}

	}

But if you need to change value from order save - just modify corresponding if() from original postProcess().

  • Like 1
Link to comment
Share on other sites

excellent solution, but I have the problem that I can not post the variable from .tpl.
 
in tpl write by javascript and there is value in the html cd_pg
document.getElementById('contenitore_prezzo_extra').innerHTML='<strong>Pagamenti a lei riservati dalla Vicidomini srl<br><input type="checkbox" checked="checked" id="cd_pg"  name="cd_pg" value="'+pag[0]+'" {if isset($smarty.post.cd_pg)}checked="checked"{/if}> '+pag[1]+'</strong>';

in override/controllers/admin/AdminOrdersController.php

class AdminOrdersController extends AdminOrdersControllerCore
{
 
public function postProcess() {

		parent::postProcess();

		if (Tools::isSubmit('id_order')) {

			$orderId = (int)Tools::getValue('id_order');
			$cd_pg = (string)Tools::getValue('cd_pg');
/*$cd_pg="d";*/
			$query = "UPDATE " . _DB_PREFIX_ . "orders SET cd_pg = '" . $cd_pg . "' WHERE id_order=" . $orderId;
			$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($query);
		}

	}	
}

 
if you do a value manually it works and in the database field cd_pg writes.
why?????? :wacko:  :wacko:  :blush:
Edited by sbarrett (see edit history)
Link to comment
Share on other sites

  • 2 years later...
On 4/20/2015 at 3:56 PM, sbarrett said:
excellent solution, but I have the problem that I can not post the variable from .tpl.
 
in tpl write by javascript and there is value in the html cd_pg

document.getElementById('contenitore_prezzo_extra').innerHTML='<strong>Pagamenti a lei riservati dalla Vicidomini srl<br><input type="checkbox" checked="checked" id="cd_pg"  name="cd_pg" value="'+pag[0]+'" {if isset($smarty.post.cd_pg)}checked="checked"{/if}> '+pag[1]+'</strong>';

in override/controllers/admin/AdminOrdersController.php


class AdminOrdersController extends AdminOrdersControllerCore
{
 
public function postProcess() {

		parent::postProcess();

		if (Tools::isSubmit('id_order')) {

			$orderId = (int)Tools::getValue('id_order');
			$cd_pg = (string)Tools::getValue('cd_pg');
/*$cd_pg="d";*/
			$query = "UPDATE " . _DB_PREFIX_ . "orders SET cd_pg = '" . $cd_pg . "' WHERE id_order=" . $orderId;
			$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($query);
		}

	}	
}
 
if you do a value manually it works and in the database field cd_pg writes.
why?????? :wacko:  :wacko:  :blush:

 

 

put your custom logic before parent::postProcess();

Link to comment
Share on other sites

  • 2 years later...

Hey,

I read your post and it helped me a bit to do what I wanted to do but there is one difference between our project.
Let me explain.

Actually, in Admin, I go to orders and I add a new one but I do not click the submit "AddOrder" button, I click the button that send a link to customer so that they should pay this order.
I created a new field in the admin order form which is a list and the value is not sent to the link given to the customer so it is equal to 0 all time.
I modify many pages but I cannot manage to get this value.

Is there someone here who can help me please ?

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...