More of a general flow question than a specific su...
# support
g
More of a general flow question than a specific support question that I guess might turn into a support question at the end 😛 With promotions, if I want to have a promotion that is a percent off an order and free shipping, and the customer applies the code in the cart, it presumably doesn't calculate the free shipping until there's a shipping method selected so that it knows how much of a discount to add, yeah? What I'm running into is that we want to offer free shipping for only some people (in our case it's only for shipping addresses in the US), but what I'm running into looks like when I apply the discount in the cart it gives the order discount but never applies the free shipping. If I clear my cart and wait until after I've selected a shipping method to apply the code, I get the expected result. Is there something else I need to make the order recalculate the discount that it's missing? I've pulled the code from the existing free shipping action and added
Copy code
# If shipping country isn't in the US, we skip it
                        next unless order.ship_address.country.iso.eql?('US')
before it creates the shipment adjustment. I'm sure there's something simple I'm missing, I think I've been staring at this for too long to see what it is.
n
are the promotions two different ones or a single one? if they are two different promotions you could have one being applied after the shipping step, and the other at its appropriate point
g
They're set up as a single promotion so we can generate a single code that applies both actions
Super weird... I confirmed that with the stock "free shipping" action it will apply the shipping adjustment after the shipping method is selected, but even if I pull that code out and just drop it in my action (without any country checks) is still only applies the order discount and then doesn't do anything with the shipping.
🤔 1
n
would have to do a deep dive into the source code to try to understand why it does that, but it's a bit late for me rn
g
No worries, that's the step that I'm on right now 😛 The only difference I can see between the stock action and mine is: stock:
Copy code
module Spree
  class Promotion < Spree::Base
    module Actions
      class FreeShipping < Spree::PromotionAction
mine:
Copy code
module Embold
    module Promotion
        module Actions
            class FreeShippingUsOnly < ::Spree::PromotionAction
Stock
Promotion
is a class that extends
Spree::Base
and mine is a module, but that's how it shows you to do it in the guide (https://guides.solidus.io/advanced-solidus/promotions-system/#implementing-a-new-action)
I've tried to change it to a class and inherit that base class, but if I do that rails won't start, saying that
Promotion
isn't a class
c
I think I’ve seen this behaviour before. I vaguely recall something around the shipment needing to exist at the time the promotion is applied, which tracks with your experience of it working after selecting a shipping method.
One way to test this is to try and add a
binding.pry
somewhere in your checkout and see if the promotion is still associated with the order and try to manually re-apply it / recreate the adjustments on the shipment.
g
It's weird, though, because the stock version of the action seems like it will wait until there's a shipment and then apply it after it's been created, but my version (with the same action code) doesn't. Good call, I'll
pry
it and see if I can see anything that stands out.
👍🏼 1
c
Hmm, it could be due to a difference in your checkout controller, I believe if the promotions is still associated with the order there still needs to be a call to recalculate the adjustments after the shipment is created for the discount to be applied.
g
Thanks Chris, I'll take a look at that, too. I've got another site (build still in progress) that's a lot closer to a vanilla install that I can try it on, too, just to see if there's something that we added somewhere along the way that's messing with it.
👍🏼 1