Jump to content

[SOLVED]Append new fields in xml - Prestashop 1.6


Recommended Posts

I'm updating a plugin by which I create Invoices. I added a table and two fields: 'pec' and 'codice_destinatario'. I handle all the data in registration form and checkout, but I'm not able to pass them in xml file when I'm creating it.

First step: create a new table

 $sql = array();

        $sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'fattura24` (
            `id_fattura24` int(11) NOT NULL AUTO_INCREMENT,
            `id_customer` int(11),
            `fattura24_codice_destinatario` varchar(13),
            `fattura24_pec` varchar(60),
            PRIMARY KEY  (`id_fattura24`)
        ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;';

        foreach ($sql as $query) {
            if (Db::getInstance()->execute($query) == false) {
        return false;
        }
        } 

then I register all the needed hooks: 

 if (!parent::install()
            || !$this->registerHook('displayAdminCustomers')
            || !$this->registerHook('displayCustomerAccountForm')
            || !$this->registerHook('displayCustomerIdentityForm')
            || !$this->registerHook('actionCustomerAccountAdd')
            || !$this->registerHook('actionCustomerAccountUpdate')
            || !$this->registerHook('actionValidateOrder')
            || !$this->registerHook('actionOrderStatusUpdate')
            || !$this->registerHook('actionOrderStatusPostUpdate'))
            return false;

Then I manage the new fields in all the needed hooks (CustomerAccountForm, CustomerIdentityForm, CustomerAccountAdd, CustomerAccountUpdate) by means of db queries like this:

$sql = "select id_fattura24,fattura24_codice_destinatario,fattura24_pec from `"._DB_PREFIX_."fattura24` where id_customer = ".Tools::getValue('id_customer')."";
        $result = Db::getInstance()->getRow($sql);

		$linkController = $this->context->link->getAdminLink("AdminCustomers", true) . "&id_customer=".Tools::getValue('id_customer')."&viewcustomer";
        $this->context->smarty->assign('linkController', $linkController);
        $this->context->smarty->assign('id_fattura24', $result['id_fattura24']);
        $this->context->smarty->assign('fattura24_codice_destinatario', $result['fattura24_codice_destinatario']);
        $this->context->smarty->assign('fattura24_pec', $result['fattura24_pec']);

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

Then I create my xml file:

$domtree = new DOMDocument('1.0', 'UTF-8');

$domtree->preserveWhiteSpace = false;
    	$domtree->formatOutput = true;
    	$xmlRoot = $domtree->createElement("Fattura24");
    	$xmlRoot = $domtree->appendChild($xmlRoot);
    	$xmlCustomer = $domtree->createElement("Document");
[...]
$sql = "select id_fattura24,fattura24_codice_destinatario,fattura24_pec from `"._DB_PREFIX_."fattura24` where id_customer = ".Tools::getValue('id_customer')."";
        $result = Db::getInstance()->getRow($sql);

        $pec = $result['fattura24_pec'];
        $codice_destinatario = $result['fattura24_codice_destinatario'];
        
        //if(!empty($customer->pec)){
            $this->trace("customer pec is not empty: ",$pec);
            $customer_fePec = $domtree->createElement('FeCustomerPec');
            $customer_fePec->appendChild($domtree->createCDataSection($pec));
            $xmlCustomer->appendChild($customer_fePec);
        //}
        //if(!empty($customer->codice_destinatario)){  // saves both fields if not empty Davide Iandoli 31.01.2019
            $this->trace("customer codice_destinatario is not empty: ",$codice_destinatario);
            $customer_feCodiceDestinatario = $domtree->createElement('FeDestinationCode');
            $customer_feCodiceDestinatario->appendChild($domtree->createCDataSection($codice_destinatario));
            $xmlCustomer->appendChild($customer_feCodiceDestinatario);

I'm not able to append the new fields. Any suggestion?

Thanks

Davide

Edited by d.iandoli75 (see edit history)
Link to comment
Share on other sites

Eventually I solved by means of a snippet like this:

 $sql = "select id_fattura24,fattura24_codice_destinatario,fattura24_pec from `"._DB_PREFIX_."fattura24` where id_customer = ".(int)$order->id_customer."";
        $result = Db::getInstance()->getRow($sql);

        $pec = $result['fattura24_pec'];
        $sdi_code = $result['fattura24_codice_destinatario'];

Then I pass the values to my  xml

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