Jump to content

Prestashop 1.6 Ajouter un nouvelle colonne Order State


Recommended Posts

Bonjour

je souhaite ajouter une nouvelle colonne à la table XX_order_state.

mon champs se nomme send_mail_accouting. Ce champs me permet d'ajouter un paramètre au statut de commande afin de réaliser une action supplémentaire.

 

J'ai donc déjà :

1- ajouter le champs à la table xxx_order_state

2- overide de AdminStatuseController

<?php

class AdminStatusesController extends AdminStatusesControllerCore
{
	/**
	 * init all variables to render the order status list
	 */
	protected function initOrderStatutsList()
	{
		$this->addRowAction('edit');
		$this->addRowAction('delete');

		$this->bulk_actions = array(
			'delete' => array(
				'text' => $this->l('Delete selected'),
				'confirm' => $this->l('Delete selected items?'),
				'icon' => 'icon-trash',
			)
		);

		$this->fields_list = array(
			'id_order_state' => array(
				'title' => $this->l('ID'),
				'align' => 'text-center',
				'class' => 'fixed-width-xs'
			),
			'name' => array(
				'title' => $this->l('Name'),
				'width' => 'auto',
				'color' => 'color'
			),
			'logo' => array(
				'title' => $this->l('Icon'),
				'align' => 'text-center',
				'image' => 'os',
				'orderby' => false,
				'search' => false,
				'class' => 'fixed-width-xs'
			),
			'send_email' => array(
				'title' => $this->l('Send email to customer'),
				'align' => 'text-center',
				'active' => 'sendEmail',
				'type' => 'bool',
				'ajax' => true,
				'orderby' => false,
				'class' => 'fixed-width-sm'
			),
			'delivery' => array(
				'title' => $this->l('Delivery'),
				'align' => 'text-center',
				'active' => 'delivery',
				'type' => 'bool',
				'ajax' => true,
				'orderby' => false,
				'class' => 'fixed-width-sm'
			)
			,
			'invoice' => array(
				'title' => $this->l('Invoice'),
				'align' => 'text-center',
				'active' => 'invoice',
				'type' => 'bool',
				'ajax' => true,
				'orderby' => false,
				'class' => 'fixed-width-sm'
			),
			'template' => array(
				'title' => $this->l('Email template')
			),
				
				'send_mail_accounting' => array(
						'title' => $this->l('Send email to accounting'),
						'align' => 'text-center',
						'active' => 'sendMailAccounting',
						'type' => 'bool',
						'ajax' => true,
						'orderby' => false,
						'class' => 'fixed-width-sm'
				)
				
		);
	}

	

	

	public function renderForm()
	{
		$this->fields_form = array(
			'tinymce' => true,
			'legend' => array(
				'title' => $this->l('Order status'),
				'icon' => 'icon-time'
			),
			'input' => array(
				array(
					'type' => 'text',
					'label' => $this->l('Status name'),
					'name' => 'name',
					'lang' => true,
					'required' => true,
					'hint' => array(
						$this->l('Order status (e.g. \'Pending\').'),
						$this->l('Invalid characters: numbers and').' !<>,;?=+()@#"{}_$%:'
					)
				),
				array(
					'type' => 'file',
					'label' => $this->l('Icon'),
					'name' => 'icon',
					'hint' => $this->l('Upload an icon from your computer (File type: .gif, suggested size: 16x16).')
				),
				array(
					'type' => 'color',
					'label' => $this->l('Color'),
					'name' => 'color',
					'hint' => $this->l('Status will be highlighted in this color. HTML colors only.').' "lightblue", "#CC6600")'
				),
				array(
					'type' => 'checkbox',
					'name' => 'logable',
					'values' => array(
						'query' => array(
							array('id' => 'on', 'name' => $this->l('Consider the associated order as validated.'), 'val' => '1'),
							),
						'id' => 'id',
						'name' => 'name'
					)
				),
				array(
					'type' => 'checkbox',
					'name' => 'invoice',
					'values' => array(
						'query' => array(
							array('id' => 'on', 'name' => $this->l('Allow a customer to download and view PDF versions of his/her invoices.'), 'val' => '1'),
							),
						'id' => 'id',
						'name' => 'name'
					)
				),
				array(
					'type' => 'checkbox',
					'name' => 'hidden',
					'values' => array(
						'query' => array(
							array('id' => 'on', 'name' => $this->l('Hide this status in all customer orders.'), 'val' => '1'),
							),
						'id' => 'id',
						'name' => 'name'
					)
				),
				array(
					'type' => 'checkbox',
					'name' => 'send_email',
					'values' => array(
						'query' => array(
							array('id' => 'on', 'name' => $this->l('Send an email to the customer when his/her order status has changed.'), 'val' => '1'),
							),
						'id' => 'id',
						'name' => 'name'
					)
				),
				array(
					'type' => 'checkbox',
					'name' => 'shipped',
					'values' => array(
						'query' => array(
							array('id' => 'on',  'name' => $this->l('Set the order as shipped.'), 'val' => '1'),
							),
						'id' => 'id',
						'name' => 'name'
					)
				),
				array(
					'type' => 'checkbox',
					'name' => 'paid',
					'values' => array(
						'query' => array(
							array('id' => 'on', 'name' => $this->l('Set the order as paid.'), 'val' => '1'),
							),
						'id' => 'id',
						'name' => 'name'
					)
				),
				array(
					'type' => 'checkbox',
					'name' => 'delivery',
					'values' => array(
						'query' => array(
							array('id' => 'on', 'name' => $this->l('Show delivery PDF.'), 'val' => '1'),
							),
						'id' => 'id',
						'name' => 'name'
					)
				),
					
				array(
					'type' => 'checkbox',
					'name' => 'send_mail_accounting',
					'values' => array(
						'query' => array(
							array('id' => 'on', 'name' => $this->l('Send an email to the accounting when his/her order status has changed.'), 'val' => '1'),
							),
						'id' => 'id',
						'name' => 'name'
					)
				),
				array(
					'type' => 'select_template',
					'label' => $this->l('Template'),
					'name' => 'template',
					'lang' => true,
					'options' => array(
						'query' => $this->getTemplates($this->context->language->iso_code),
						'id' => 'id',
						'name' => 'name'
					),
					'hint' => array(
						$this->l('Only letters, numbers and underscores ("_") are allowed.'),
						$this->l('Email template for both .html and .txt.')
					)
				)
				
			),
			'submit' => array(
				'title' => $this->l('Save'),
			)
		);

		if (Tools::isSubmit('updateorder_state') || Tools::isSubmit('addorder_state'))
			return $this->renderOrderStatusForm();
		else if (Tools::isSubmit('updateorder_return_state') || Tools::isSubmit('addorder_return_state'))
			return $this->renderOrderReturnsForm();
		else
			return AdminController::renderForm();
	}

	protected function renderOrderStatusForm()
	{
		if (!($obj = $this->loadObject(true)))
			return;

		$this->fields_value = array(
			'logable_on' => $this->getFieldValue($obj, 'logable'),
			'invoice_on' => $this->getFieldValue($obj, 'invoice'),
			'hidden_on' => $this->getFieldValue($obj, 'hidden'),
			'send_email_on' => $this->getFieldValue($obj, 'send_email'),
			'shipped_on' => $this->getFieldValue($obj, 'shipped'),
			'paid_on' => $this->getFieldValue($obj, 'paid'),
			'delivery_on' => $this->getFieldValue($obj, 'delivery'),
			'send_mail_accounting_on' => $this->getFieldValue($obj, 'send_mail_accounting'),
		);
		
		

		if ($this->getFieldValue($obj, 'color') !== false)
			$this->fields_value['color'] = $this->getFieldValue($obj, 'color');
		else
			$this->fields_value['color'] = "#ffffff";

		return AdminController::renderForm();
	}

	

	public function postProcess()
	{
		if (Tools::isSubmit($this->table.'Orderby') || Tools::isSubmit($this->table.'Orderway'))
			$this->filter = true;

		if (Tools::isSubmit('submitAddorder_return_state'))
		{
			$id_order_return_state = Tools::getValue('id_order_return_state');

			// Create Object OrderReturnState
			$order_return_state = new OrderReturnState((int)$id_order_return_state);

			$order_return_state->color = Tools::getValue('color');
			$order_return_state->name = array();
			$languages = Language::getLanguages(false);
				foreach ($languages as $language)
					$order_return_state->name[$language['id_lang']] = Tools::getValue('name_'.$language['id_lang']);

			// Update object
			if (!$order_return_state->save())
				$this->errors[] = Tools::displayError('An error has occurred: Can\'t save the current order\'s return status.');
			else
				Tools::redirectAdmin(self::$currentIndex.'&conf=4&token='.$this->token);
		}

		if (Tools::isSubmit('submitBulkdeleteorder_return_state'))
		{
			$this->className = 'OrderReturnState';
			$this->table = 'order_return_state';
			$this->boxes = Tools::getValue('order_return_stateBox');
			AdminController::processBulkDelete();
		}

		if (Tools::isSubmit('deleteorder_return_state'))
		{
			$id_order_return_state = Tools::getValue('id_order_return_state');

			// Create Object OrderReturnState
			$order_return_state = new OrderReturnState((int)$id_order_return_state);

			if (!$order_return_state->delete())
				$this->errors[] = Tools::displayError('An error has occurred: Can\'t delete the current order\'s return status.');
			else
				Tools::redirectAdmin(self::$currentIndex.'&conf=1&token='.$this->token);
		}

		if (Tools::isSubmit('submitAdd'.$this->table))
		{
			$this->deleted = false; // Disabling saving historisation
			$_POST['invoice'] = (int)Tools::getValue('invoice_on');
			$_POST['logable'] = (int)Tools::getValue('logable_on');
			$_POST['send_email'] = (int)Tools::getValue('send_email_on');
			$_POST['hidden'] = (int)Tools::getValue('hidden_on');
			$_POST['shipped'] = (int)Tools::getValue('shipped_on');
			$_POST['paid'] = (int)Tools::getValue('paid_on');
			$_POST['delivery'] = (int)Tools::getValue('delivery_on');
			$_POST['send_mail_accounting'] = (int)Tools::getValue('send_mail_accounting_on');
			
			if (!$_POST['send_email'])
			{
				$languages = Language::getLanguages(false);
				foreach ($languages as $language)
					$_POST['template_'.$language['id_lang']] = '';
			}

			return AdminController::postProcess();
		}
		else if (Tools::isSubmit('delete'.$this->table))
		{
			$order_state = new OrderState(Tools::getValue('id_order_state'), $this->context->language->id);
			if (!$order_state->isRemovable())
				$this->errors[] = $this->l('For security reasons, you cannot delete default order statuses.');
			else
				return AdminController::postProcess();
		}
		else if (Tools::isSubmit('submitBulkdelete'.$this->table))
		{
			foreach (Tools::getValue($this->table.'Box') as $selection)
			{
				$order_state = new OrderState((int)$selection, $this->context->language->id);
				if (!$order_state->isRemovable())
				{
					$this->errors[] = $this->l('For security reasons, you cannot delete default order statuses.');
					break;
				}
			}

			if (!count($this->errors))
				return AdminController::postProcess();
		}
		else
			return AdminController::postProcess();
	}

	
	
	public function ajaxProcessSendMailAccountingOrderState()
	{
		$id_order_state = (int)Tools::getValue('id_order_state');
	
		$sql = 'UPDATE '._DB_PREFIX_.'order_state SET `send_mail_accounting`= NOT `send_mail_accounting` WHERE id_order_state='.$id_order_state;
		$result = Db::getInstance()->execute($sql);
	
		if ($result)
			echo json_encode(array('success' => 1, 'text' => $this->l('The status has been updated successfully.')));
		else
			echo json_encode(array('success' => 0, 'text' => $this->l('An error occurred while updating this meta.')));
	}
	
}

Ce nouveau champs apparaît bien dans la la liste des statuts :

post-347951-0-88581600-1410170965_thumb.png

 

Mais je n'arrive pas à récupérer la valeur dans le formulaire :

post-347951-0-08688100-1410171064_thumb.png

 

 

il semble que ce soit le retour de $this->fields_value() qui ne me renvoit pas dans la méthode renderOrderStatusForm :

'send_mail_accounting_on' => $this->getFieldValue($obj, 'send_mail_accounting'),

 

Si quelqu'un a une idée ?

 

Link to comment
Share on other sites

et bien je me réponds à moi même puisque j'ai trouvé la solution, comme un grand !!

alors voila, il fallait aussi faire l'override de la classe OrderState  

class OrderState extends OrderStateCore
{
 	

	/** @var boolean Send an e-mail to accounting ? */
	public $send_mail_accounting;

	
	/**
	 * @see ObjectModel::$definition
	 */
	public static $definition = array(
		'table' => 'order_state',
		'primary' => 'id_order_state',
		'multilang' => true,
		'fields' => array(
			'send_email' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
			'module_name' => array('type' => self::TYPE_STRING, 'validate' => 'isModuleName'),
			'invoice' => 	array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
			'color' => 		array('type' => self::TYPE_STRING, 'validate' => 'isColor'),
			'logable' => 	array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
			'shipped' => 	array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
			'unremovable' =>array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
			'delivery' =>	array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
			'hidden' =>		array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
			'paid' =>		array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
			'deleted' =>	array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
				'send_mail_accounting' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),

			// Lang fields
			'name' => 		array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 64),
			'template' => 	array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isTplName', 'size' => 64),
		),
	);

	
}

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