Jump to content

[SOLVED] Updating module to 1.7.7.1 : help with .twig and .tpl files


Recommended Posts

Hi,

I have to finish the update of a Prestashop module to print shipping labels (it was working on PS < 1.7.7).
I'm not the original developper of this module.
I have many years of experience as a PHP developer, but never touched a Prestashop instance... and I have to admit I'm quite lost on how to solve this issue !

The module adds two buttons :
- one on the back-office/order page (see screenshot)
- one on the back-office/orders page (see screenshot)

 

The button on the order page is added with a button.tpl file.
Main module class :

public function hookActionAdminControllerSetMedia() {
        ...
        $ajax_link  = $link->getModuleLink('unikshipping', 'FrontAjaxUnik', $parameters);
        Media::addJsDef(array(
            'ajax_controller' => $ajax_link,
            'token_key'=>$this->token_key
        ));
        ...
    }

FrontAjaxUnik.php :

public function displayAjaxUnikAction() {
        $UnikShipping = new UnikShipping();
        $order_id = Tools::getValue('order_id');
        echo $UnikShipping->shipOrderButton($order_id);
}

Main module class :

public function shipOrderButton($id) {
        ...
        return $this->context->smarty
            ->fetch(_PS_MODULE_DIR_.'/unikshipping/views/templates/admin/unik_shipping/button.tpl');
}

button.tpl :

<span class='btn-group-action'>
	<span class='btn-group'>
		<button style ='margin-right:4px; padding: 6px 2px!important;' type ='button' class='btn btn-default' onclick='$.ajax({
            url: "{$ajax_controller|escape:'htmlall':'UTF-8'}",
            data: {
                order_id: "{$id|escape:'htmlall':'UTF-8'}",
            },
            ...
        '>{$btname|escape:'htmlall':'UTF-8'}</button> 
	</span> 
</span>

This button is working perfectly !

 

But the button on the orders page is added with a mymodule_button.html.twig file.

Main module class:

public function hookActionOrderGridDefinitionModifier(array $params) {
        ...
        $definition = $params['definition'];
        $definition
                ->getColumns()
                ->addAfter(
                    'date_add',
                    (new HtmlTypeColumn('wk_button'))
                        ->setName($this->l('Unik Shipping'))
                        ->setOptions([
                            'ModuleClass' => new UnikShipping(),
                            'custom_text' => $this->ship_trans
                        ])
                );
}

class HtmlTypeColumn extends AbstractColumn :

public function getType() {
    return 'mymodule_button';
}

mymodule_button.html.twig :

<a href="https://www.google.com" class="btn btn-primary pointer">
    <i class="icon-user"></i> {{ column.options.custom_text }}
</a>

Basically, I have to replace the google.com href with an Ajax call, but I need the order id to do that and I have no clue on how the get it !

 

I tried Googling "prestashop twig get order id" and "prestashop twig get var" but couldn't sort it out.

order page.png

orders page.png

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

Well, there are to ways to do this, here are some pointers to them:

1.  Prevent that link to be executed and replace it with an Ajax call to the corresponding AdminController's action.

2. The Symfony way, replace the URL with a route name you specify in your module's route.yml, the route will call the same AdminController's action.

You'll find more about this on https://devdocs.prestashop.com/

Link to comment
Share on other sites

Hi,

Thanks for your answer and your help !

I see how to change the button behavior, to make an Ajax call (like in the button.tpl file onclick="...").
But my problem is that I need the order ID to make this Ajax call, and I don't know how to get this in the twig file ?

Link to comment
Share on other sites

Hi,

I was finally able to get the order ID to make the Ajax call work : {{ record.id_order }} in the twig file.
Sounds quite simple but I had hard times finding it...

Do you know a way of dumping all the variables available in a twig file ?
I found {{ dump() }} while Googling but it throws a fatal error...

Last thing I need to do is hiding the "ship" button if the order is already shipped.
And to do that, I have to find the "status" variable in the twig file, to add an if statement.

Link to comment
Share on other sites

  • Adrien74100 changed the title to [SOLVED] Updating module to 1.7.7.1 : help with .twig and .tpl files

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