Guest Checkout for Certain Product Codes?

newbienewbie Member
in Help edited September 5
Hi, can someone help me with code to allow a guest checkout for certain product codes?

There are only certain products that we want to disable "save my address & payment info for next time."

Thanks!
Comments
  • fc_adamfc_adam FoxyCart Team
    @newbie,

    We can certainly help with that. Could I confirm with you how you want it to behave with any other product in your store? Would it require they create an account? Or would they just have the option of creating accounts with the rest of the products, and with these specific codes it would not let them create an account at all?

    Also, just to confirm, these aren't for subscriptions are they?
  • Thanks @fc_adam. Good questions. It’s not for a subscription. It’s just for the case when this product was purchased alone, that password creation would not be requested. If this product was purchased in combination with other things then it would trigger the password creation request. Thanks!
  • fc_adamfc_adam FoxyCart Team
    @newbie,

    Thanks for the additional details. Just to confirm further - when you say that a combination of products would trigger the password creation request - do you mean that you would require the customer create an account? Or just provide the option to create an account?

    Also, one further question - if a customer already has an account, you would be ok with them logging in to their existing account if they just purchase the product on its own? Or would you require that all purchases, even for returning customers, would be guest purchases?
  • Thanks @fc_adam... yes, if this product was purchased with anything else the customer should be required to create an account. (Which should be our default setting right now. We're just looking to create an exception when this gift product is purchased alone).

    Yes, if a customer already has an account, we would be ok with them logging in to their existing account even if they just purchase the product on its own.

    Thanks!
  • fc_adamfc_adam FoxyCart Team
    @newbie,

    Thanks for confirming those last parts for me. I believe I've got a solution that should work for what you're after. You'll need to make two changes to your store to get it to work.

    Firstly, in your store's configuration - you'll want to uncheck the "Customize the checkout user type (account vs. guest)" option if it's checked. This will revert it to the default of allowing both, but defaulting to creating an account. This ensures that the server-side validation will be ok getting a guest checkout.

    Next, jump over to the "checkout" template page, and set it to a "Custom" template if it's not already. Then, in the template, in between the lines {% embed 'checkout.inc.twig' %} and {% endembed %} paste the following code:
    {% block additional_fields %}
    {% set only_guest_products = true %}
    {% for item in items %}
    {% if item.code not in ["guest-product", "guest-code-2"] %}
    {% set only_guest_products = false %}
    {% endif %}
    {% endfor %}
    {% if only_guest_products and not email_is_found %}
    {% set checkout_type = "guest_only" %}
    {% else %}
    {% set checkout_type = "account_only" %}
    {% endif %}

    {{ parent() }}
    {% endblock %}
    That code assumes you'll check for the products that should force a guest checkout by their code - which you can alter on this line:
    {% if item.code not in ["guest-product", "guest-code-2"] %}
    You can replace those two codes with whatever codes you will detect, and however many you need to detect.

    If you need to detect products using a different condition, let us know.
  • @fc_adam Thank you!
Sign In or Register to comment.