ale.pixel Posted July 28, 2022 Share Posted July 28, 2022 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 More sharing options...
lordignus Posted July 29, 2022 Share Posted July 29, 2022 Where/at which point in the checkout process do you need this to be? Link to comment Share on other sites More sharing options...
ale.pixel Posted July 29, 2022 Author Share Posted July 29, 2022 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! Link to comment Share on other sites More sharing options...
lordignus Posted July 29, 2022 Share Posted July 29, 2022 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. 1 Link to comment Share on other sites More sharing options...
Krystian Podemski Posted July 29, 2022 Share Posted July 29, 2022 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. 1 Link to comment Share on other sites More sharing options...
Ali Samie Posted July 29, 2022 Share Posted July 29, 2022 I might give it a try, but I think you can do it with a JS function to show the message. As far as I know the selected carrier is stored in checkout session and not in cart. Take a look at checkout session class. Link to comment Share on other sites More sharing options...
ale.pixel Posted August 2, 2022 Author Share Posted August 2, 2022 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 More sharing options...
lordignus Posted August 2, 2022 Share Posted August 2, 2022 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. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now