Jump to content

[Solved] how to get delivery Address state id in invoice tpl


Recommended Posts

How to get delivery address state id variable in pdf/ invoice.tpl file, I have tried

State::getNameById({$addresses.invoice->id_state})  but it is not working ...

I want to check the customer's delivery state  

 

I am using PrestaShop version: 1.6.0.5

 

--------------------------------------------------------------------------------------------

 

Solution:-

delivery & billing address is same you need to assign this  $invoice_address->id_state; in HTMLTemplateInvoice.php

 

delivery & billing address differ you need to assign $delivery_address->id_state; in HTMLTemplateInvoice.php .  

 

and catch the variable in invoice.tpl

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

try to use id only in a first step. if this works, go further with getting the state name.

 

but in ny opinion, state should be available as per default. don't have source available here to check the field name.

State::getNameById working fine if I manually put any id, but I want to get the customers delivery state id ($addresses.invoice->id_state as this is not giving any value), I want this for some tax calculation based on state

Link to comment
Share on other sites

But tax calculations shouldn't be done in the invoice tpl. It's just the wrong place.

 

 

We  have no choice because of GST we need to show the two taxes for one product and it is has to be printed on invoice...

 

I have figured out this with newest version but few stores are on old version .... are there any way to get variable in invoice.tpl

  • Like 1
Link to comment
Share on other sites

But if you insist: look at the according php which fills the variables. It should be in /classes/pdf

 

Our you can try to dump arrays from within tpl.

 

 

I have checked the HTMLTemplateInvoice.php , it is $formatted_delivery_address = AddressFormat::generateAddress($delivery_address, array(), '<br />', ' '); 

 

I have tried to print $delivery_address in tpl but it is not printing ....

 

I have attached  HTMLTemplateInvoice.php for you reference , can you tell me how to print the $delivery_address in tpl file 

<?php

/*

* 2007-2014 PrestaShop

*

* NOTICE OF LICENSE

*

* This source file is subject to the Open Software License (OSL 3.0)

* that is bundled with this package in the file LICENSE.txt.

* It is also available through the world-wide-web at this URL:

* http://opensource.org/licenses/osl-3.0.php

* If you did not receive a copy of the license and are unable to

* obtain it through the world-wide-web, please send an email

* to [email protected] so we can send you a copy immediately.

*

* DISCLAIMER

*

* Do not edit or add to this file if you wish to upgrade PrestaShop to newer

* versions in the future. If you wish to customize PrestaShop for your

* needs please refer to http://www.prestashop.com for more information.

*

*  @author PrestaShop SA <[email protected]>

*  @copyright  2007-2014 PrestaShop SA

*  @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)

*  International Registered Trademark & Property of PrestaShop SA

*/



/**

 * @since 1.5

 */

class HTMLTemplateInvoiceCore extends HTMLTemplate

{

	public $order;

	public $available_in_your_account = false;



	public function __construct(OrderInvoice $order_invoice, $smarty)

	{

		$this->order_invoice = $order_invoice;

		$this->order = new Order((int)$this->order_invoice->id_order);

		$this->smarty = $smarty;



		// header informations

		$this->date = Tools::displayDate($order_invoice->date_add);



		$id_lang = Context::getContext()->language->id;

		$this->title = HTMLTemplateInvoice::l('Invoice ').' #'.Configuration::get('PS_INVOICE_PREFIX', $id_lang, null, (int)$this->order->id_shop).sprintf('%06d', $order_invoice->number);

		// footer informations

		$this->shop = new Shop((int)$this->order->id_shop);

	}



	/**

	 * Returns the template's HTML content

	 * @return string HTML content

	 */

	public function getContent()

	{

		$country = new Country((int)$this->order->id_address_invoice);

		$invoice_address = new Address((int)$this->order->id_address_invoice);

		$formatted_invoice_address = AddressFormat::generateAddress($invoice_address, array(), '<br />', ' ');

		$formatted_delivery_address = '';



		if ($this->order->id_address_delivery != $this->order->id_address_invoice)

		{

			$delivery_address = new Address((int)$this->order->id_address_delivery);

			$formatted_delivery_address = AddressFormat::generateAddress($delivery_address, array(), '<br />', ' ');

		}



		$customer = new Customer((int)$this->order->id_customer);



		$this->smarty->assign(array(

			'order' => $this->order,

			'order_details' => $this->order_invoice->getProducts(),

			'cart_rules' => $this->order->getCartRules($this->order_invoice->id),

			'delivery_address' => $formatted_delivery_address,

			'invoice_address' => $formatted_invoice_address,

			'tax_excluded_display' => Group::getPriceDisplayMethod($customer->id_default_group),

			'tax_tab' => $this->getTaxTabContent(),

			'customer' => $customer

		));



		return $this->smarty->fetch($this->getTemplateByCountry($country->iso_code));

	}



	/**

	 * Returns the tax tab content

	 */

	public function getTaxTabContent()

	{

			$address = new Address((int)$this->order->{Configuration::get('PS_TAX_ADDRESS_TYPE')});

			$tax_exempt = Configuration::get('VATNUMBER_MANAGEMENT')

								&& !empty($address->vat_number)

								&& $address->id_country != Configuration::get('VATNUMBER_COUNTRY');

			$carrier = new Carrier($this->order->id_carrier);

			

			$this->smarty->assign(array(

				'tax_exempt' => $tax_exempt,

				'use_one_after_another_method' => $this->order_invoice->useOneAfterAnotherTaxComputationMethod(),

				'product_tax_breakdown' => $this->order_invoice->getProductTaxesBreakdown(),

				'shipping_tax_breakdown' => $this->order_invoice->getShippingTaxesBreakdown($this->order),

				'ecotax_tax_breakdown' => $this->order_invoice->getEcoTaxTaxesBreakdown(),

				'wrapping_tax_breakdown' => $this->order_invoice->getWrappingTaxesBreakdown(),

				'order' => $this->order,

				'order_invoice' => $this->order_invoice,

				'carrier' => $carrier

			));



			return $this->smarty->fetch($this->getTemplate('invoice.tax-tab'));

	}



	/**

	 * Returns the invoice template associated to the country iso_code

	 * @param string $iso_country

	 */

	protected function getTemplateByCountry($iso_country)

	{

		$file = Configuration::get('PS_INVOICE_MODEL');



		// try to fetch the iso template

		$template = $this->getTemplate($file.'.'.$iso_country);



		// else use the default one

		if (!$template)

			$template = $this->getTemplate($file);



		return $template;

	}



	/**

	 * Returns the template filename when using bulk rendering

	 * @return string filename

	 */

	public function getBulkFilename()

	{

		return 'invoices.pdf';

	}



	/**

	 * Returns the template filename

	 * @return string filename

	 */

	public function getFilename()

	{

		return Configuration::get('PS_INVOICE_PREFIX', Context::getContext()->language->id, null, $this->order->id_shop).sprintf('%06d', $this->order_invoice->number).'.pdf';

	}

}




Link to comment
Share on other sites

ok, this value

 

$this->order->id_address_invoice

 

is the ID you are looking for. Use it in the smarty assign array section to pass it to the tpl file.

 

Just add one more line and give it a meaningful name within the existing array.

 

After that you can reference it in your tpl.

Link to comment
Share on other sites

ok, this value

 

$this->order->id_address_invoice

 

is the ID you are looking for. Use it in the smarty assign array section to pass it to the tpl file.

 

Just add one more line and give it a meaningful name within the existing array.

 

After that you can reference it in your tpl.

 

 

it is giving the number but wrong number...the customer's  state id  is 327 but it is giving 94

Link to comment
Share on other sites

You have to assign this variable in the PHP first. $this->smarty->assign

 

You cannot catch the variable directly in tpl!

 

You have to assign this variable in the PHP first. $this->smarty->assign

 

You cannot catch the variable directly in tpl!

 

 

 

Thank you  Scully ,

 

with your guidance ,I have figured out if the delivery & billing address is same you need to assign this  $invoice_address->id_state; in HTMLTemplateInvoice.php

if delivery & billing address differ you need to assign $delivery_address->id_state; in HTMLTemplateInvoice.php .  

And catch the variable in invoice.tpl

 

Hence this solved   :)

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

  • 3 years later...
On 7/29/2017 at 12:28 PM, ecommercePrestaShop said:

Thank you  Scully ,

 

with your guidance ,I have figured out if the delivery & billing address is same you need to assign this  $invoice_address->id_state; in HTMLTemplateInvoice.php

if delivery & billing address differ you need to assign $delivery_address->id_state; in HTMLTemplateInvoice.php .  

And catch the variable in invoice.tpl

 

Hence this solved   :)

How do you assign this in code?

Link to comment
Share on other sites

  • 4 months later...

I don´t think that this topic should be considered as SOLVED. Because there is missing the solution....

You should at least share one line of code for the others...

 

Here is the SOLUTION for v1.6: To add name of the country based on ID_COUNTRY you should add following code into -> classes/pdf/HTMLTemplateInvoice.php

In  public function getContent ...
...
$data = array(
...

'country' => State::getNameById($invoiceAddress->id_country),  // added for Country name 

);

Don't forget to add comma before last line, where you are inserting that code.

 

 If i helped you, say thanks, or you can give me a BEER 🍻

Edited by Kaper (see edit history)
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...