Jump to content

Custom module not working if Cache is Enabled


aljon ngo

Recommended Posts

Hello guys, I created an Module and it is about the delivery tab . My module works fine but when i enable my cache an error occurs. Note that i already clear my cache at Back office and manually in the folder but still same error occurs

Notice: Undefined index: product in root/cache/smarty/compile/0b/95/b2/0b95b2da34be544dfa9466aa879d8aa6c52c985e.file.shippingtab.tpl.php on line 34Notice:

Im not sure but i think it is because the var $product that is generated by the ProductController.php

{if $product->additional_shipping_cost == 0}    
        code            
{else}
    {if $product->price|string_format:"%.2f" >= 10000}            
            code                            
        {else}
            code    
        {convertPrice price=$product->additional_shipping_cost}
    {/if}                                    
{/if}

any suggestion how can i solve this problem? or code?

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

does your module add the product variable to the smarty engine?  if not, then you have 2 options

 

1) change your if statement to

{if $product && $product->additional_shipping_cost == 0}    

2) adjust your module hook to detect which product is being viewed, lookup that product and place it into smarty

 

you don't describe what your module does, and what page you are on.  have you considered that a product is not in context of the page you are working on?

Link to comment
Share on other sites

does your module add the product variable to the smarty engine?  if not, then you have 2 options

 

1) change your if statement to

{if $product && $product->additional_shipping_cost == 0}    

2) adjust your module hook to detect which product is being viewed, lookup that product and place it into smarty

 

you don't describe what your module does, and what page you are on.  have you considered that a product is not in context of the page you are working on?

 

I tried #1 solution but same error occurs. In your #2 solution .. can you explain it further thanks.

 

My module is a product page tab that gives information about the shipping method per product .. this shipping info is only a plain text but it changes depending on the additional_shipping_cost and the product price.. the problem in my module i think is that when i activated the cache.. i cannot use the $product variable that is generated by the product page

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

what hook are you using?

what cache system are you using? Is it a module?

im using "productTab" and "productTabContent" hook

i just enable the cache to 'YES' and select Force compilation under smarty category

Yes it is a custom Module

Link to comment
Share on other sites

With cache enabled, your template files are compiled and stored on the file system, but the ProductController is still called with every request, and places the Product object into smarty so that the compiled template can use it.  

 

I also reviewed the ProductController, and it makes no attempt to cache the output from the template, so it should have no bearing.

 

You don't say what version of Prestashop or what theme, so I cannot test this out, but below are some code highlights for getting the product id, loading the Product object and placing it in smarty.  I don't believe you need to do this and should focus on fixing the issue you have, but you could try this.

 

 

get the product id from the page request

$id_product = Tools::getValue('id_product');

use it to lookup the product

$product = new Product($id_product);

put it into smarty engine

$this->smarty->assign(array(
          'product' => $product,
));

 

  • Like 1
Link to comment
Share on other sites

 

With cache enabled, your template files are compiled and stored on the file system, but the ProductController is still called with every request, and places the Product object into smarty so that the compiled template can use it.  

 

I also reviewed the ProductController, and it makes no attempt to cache the output from the template, so it should have no bearing.

 

You don't say what version of Prestashop or what theme, so I cannot test this out, but below are some code highlights for getting the product id, loading the Product object and placing it in smarty.  I don't believe you need to do this and should focus on fixing the issue you have, but you could try this.

 

 

get the product id from the page request

$id_product = Tools::getValue('id_product');

use it to lookup the product

$product = new Product($id_product);

put it into smarty engine

$this->smarty->assign(array(
          'product' => $product,
));

thanks i just generated the codes in my controller and it works perfectly fine =)

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