Jump to content

Get the id of the selected carrier in the product page


Recommended Posts

Hi folks,

 

For some products I have free shipping, so I made 2 carriers, one with free shipping and one weight based.

 

For every product, I chose the carrier in the product edit page, so the ones with free shipping will have only free shipping carrier selected.

 

How do I get the id of the selected carrier in product.tpl to compare it with the id of free shipping carrier so I can display a message or image in the product page? ..or any other suggestion to make this possible.

 

Thank you!

Link to comment
Share on other sites

  • 11 months later...
If you want get the carrier details in cart page

 

1. Open the controllers/front/Cartcontroller.php file

2. And add this coded $carrier_list = $carrier->getCarriers(1); inside the initContent() function

3. Then assign the variable in smarty.

 

See e.g.-

 public function initContent()

    {

        if (Configuration::isCatalogMode() && Tools::getValue('action') === 'show') {

            Tools::redirect('index.php');

        }

 

        $presenter = new CartPresenter();

        $presented_cart = $presenter->present($this->context->cart, $shouldSeparateGifts = true);

$carrier_list = $carrier->getCarriers(1); // Your variable

        $this->context->smarty->assign([

            'cart' => $presented_cart,

            'static_token' => Tools::getToken(false),

'libros'=> $carrier,

'test'=> $carrier_list, // assign your variable

        ]);

       

    }

 

4. Then go to cart tpl page

And get the all value of carrier.

 

As. {foreach from=$test item=carrier key=carrier_id name=count}                

 <p>{$carrier.name}</p>                

{/foreach}

 

output:- My carrier

Link to comment
Share on other sites

  • 7 months later...

Everytime you edit a carrier, prestashop duplicates it to create a new one. Older versions are hidden in the database (to keep information for older orders).

So everytime you edit a carrier, it gets a new id. The id of the first technical carrier which corresponds to this new one is stored in the database. So if you want to make some tests on carrier id, you should use the reference id instead of the carrier id.

Unfortunately, $cart->id_carrier does not contain this reference id. I was not able to find a simple way to get it on product.tpl.

Link to comment
Share on other sites

  • 2 years later...
  • 8 months later...
  • 4 months later...
On 5/12/2020 at 2:33 AM, ChineseNorris said:

No changes in php files needed... simply use this in any prestashop .tpl smarty file:

{$carrier_id = context::getContext()->cart->id_carrier.id_reference}

I tried to implement your code in my cart.tpl but it does not seem to work :-(

I would like to hide some info on the "cart" page according to the available carrrier for the products added in my cart.

In ex. I would like to hide the delay of delivery info if there are only one or more articles with "pick up on site" delivery 

Unfortunately with this code $carrier_id always contains value 0 (the available carrier(s) defined for my articles has no impact on this info).

Hope you can help me.
Regards
joel

 

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