Jump to content

Get carriers list o product.tpl


NextOne
 Share

Recommended Posts

Hello,

I will try add list of carriers available to current product. I search for resolution about and I saw this:

 

so, in ../controllers/front/ProductController.php in line 279 I added this code:

'carriers' => $this->product->getCarriers(),

 

and in my product.tpl:

{$carriers|@print_r}

 

results it's:

Array ( ) 1

 

also tried:

{foreach from=$carriers item=carrier} 
  {$carrier|@print_r}
{/foreach} 

 

then nothing shows up.

 

Could you help me? Thanks.

 

My prestashop version: 1.7.6.9

Share this post


Link to post
Share on other sites

  • 2 months later...

We want to tell you that your code will return the result empty when no specific carrier is selected for the respective product.

Image for your reference: https://nimb.ws/Zgkb5j

To resolve the issue, you need to use an if condition to check the result and if the result is empty then you can use the Carrier class of the Prestahsop to get all the available carriers.
You need to add the below code in the ProductController.php before assigning the product data to the tpl.

File path: YOUR_PRESTASHOP_DIRECTORY/controllers/front/ProductController.php

    $carriers = $this->product->getCarriers();
     if(count($carriers) == 0) {
            $carriers = Carrier::getCarriers($this->context->language->id, true, false, false, null, Carrier::ALL_CARRIERS);
      }

Also, you need to assign the data to the product.tpl which is available in the $carriers variable.

  'carriers' => $carriers,

Share this post


Link to post
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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More