Jump to content

In Prestashop 8, How do I access object fields in a smarty template that are not exposed in the LazyArray?


Recommended Posts

Posted (edited)

Since objects are now wrapped in LazyArrays, how do I properly access the field secure_key for instance in the order-detail.tpl template?

$order.secure_key or $order.details.secure_key don't work since they are not exposed in the LazyArray and return an empty array.

 

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

  • zunxunz changed the title to In Prestashop 8, How do I access object fields in a smarty template that are not exposed in the LazyArray?

In PrestaShop, Smarty templates primarily interact with data passed from controllers. If the data you need isn't directly exposed in the LazyArray, you may need to modify the controller to include that data or use hooks to inject the data into the template.

Here's a general approach:

Modify Controller: If possible, modify the controller responsible for passing data to the template. You can include additional fields in the array that's assigned to the Smarty template.

Use Hooks: If modifying the controller isn't an option or if the data is not readily available there, you can use hooks to inject data into the template. You'd create a module with a hook that executes when the template is rendered. This hook can fetch the additional data and add it to the template's variables.

Direct Access: If you absolutely must access data that's not directly exposed, you might need to access the object directly in the template. However, this isn't recommended as it bypasses PrestaShop's architecture and could lead to unexpected behavior or compatibility issues.

Here's an example of how you might access an object directly in a Smarty template:

{php}
    // Fetch the object directly from its class
    $myObject = new MyObject($id);
    // Access the desired field
    $desiredField = $myObject->desiredField;
{/php}

{$desiredField}

Replace MyObject with the appropriate class name and $id with the object's identifier. However, remember that this approach is not recommended unless absolutely necessary, as it can lead to maintenance issues and might not work as expected in future versions of PrestaShop. It's better to go through controllers or hooks whenever possible.

  • Thanks 1
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...