Jump to content

Get carrier id during checkout


ale.pixel

Recommended Posts

Hello everyone!

I actually need to show some text during the checkout only when a particular carrier is selected. So far, I came out with this condition that "should" work, but it seems it doesn't match the real carrier id

{if Context::getContext()->cart->id_carrier == 86}
  <p>My text</p>
</p>

Even if my carrier has this id (86) in both PrestaShop back end and in the db tables, the condition doesn't work. When I try to output the value in front end, a different id comes out and it's identical to another carrier. This is the only carrier I'm having this problem with. If I try to replicate this function with another carrier, it works. Just not on this one.

Am I missing something or approaching it the wrong way?

 

Thanks in advance!

Link to comment
Share on other sites

I would like it to appear during the step 3 - Shipping Method when the carrier are shown and you can select them (see screenshot).

The message should be on the right column, but I already know how to put it there, it's just the condition that is not working properly.

I'm using PrestaShop 1.7

 

Thanks!

Firefox_Screenshot_2022-07-29T12-42-40.633Z.png

Link to comment
Share on other sites

That's really strange, can't imagine why it would work for your other carriers but not the one with id 86.

Just something to consider - if you make any changes to the carrier in the backend, it gets assigned a new id which could break your code if you do it this way.

  • Like 1
Link to comment
Share on other sites

On 7/29/2022 at 10:11 PM, Krystian Podemski said:

You should take Lordingus tip and use the `id_reference` of the Carrier object to achieve the desired result. You might need to put your code where PrestaShop refreshes the block, to make sure it gets restored after changing the carrier.

Will this work?

{if Context::getContext()->cart->id_reference == 123}

I haven't found it in the smarty variables in the PrestaShop guide

Link to comment
Share on other sites

1 hour ago, alex.kidd said:

Will this work?

{if Context::getContext()->cart->id_reference == 123}

I haven't found it in the smarty variables in the PrestaShop guide

That wouldn't work as the carrier reference is part of the Carrier object which isn't part of the Context.

My favourite quick solution for situations like this is that I have an override of the Tools class with whatever custom functions I need.

For example you could add the following function to your Tools class override:

public static function getCartCarrierReference($id_cart) : int {
	$cart = new Cart($id_cart);
	if(!$cart->id_carrier){
	   return -1;
	}
	$carrier = new Carrier($cart->id_carrier);
	$id_carrier_reference = $carrier->id_reference;
	return $id_carrier_reference;
}

And then call it in your template like:

{if Tools::getCartCarrierReference(Context::getContext()->cart->id) == 123}
{* do your thing *}
{/if}

The php function returns an integer which is either the carrier reference, or -1 if the cart is invalid or doesn't have a carrier selected, so you can use that for debugging/obtaining the correct carrier reference if necessary.

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