My client originally did not want Add to Cart on any products, now they have some that they would like to have it show. Is there a way to do that? The code below is for how we handled the suppressing the add to cart in the product-form.liquid
My client originally did not want Add to Cart on any products, now they have some that they would like to have it show. Is there a way to do that? The code below is for how we handled the suppressing the add to cart in the product-form.liquid
Hi @chels_grove ,
I would recommend creating a new product template and section for the said product. Then assign the template to that product.
Please see the video for instructions on how.
You can’t because the product form is not referenced in the product template, unless you have a string of code to add to the new template.
Interesting - what metafield would you use and what would you put in there?
@Elghorfi is right. You can use a product metafield actually and add the condition in your product section. You can use a true and false metafield. Then enclosed your button with the if statement.
@made4Uo @Elghorfi this is great - could you help me figure out where to put my metafield condition. Below is the full code on the product-form.liquid I tried putting it before and before the data add to cart class but it didn’t work (got errors when saving).
{ if product.metafields.custom.add_to_cart_button == true }
{%- form 'product', product, id: form_id, class: 'product-single__form' -%}
{%- liquid
assign enable_dynamic_buttons = false
if show_dynamic_checkout and template != 'product.preorder'
assign enable_dynamic_buttons = true
endif
-%}
{%- if enable_dynamic_buttons -%}
{%- endif -%}
{%- liquid
assign default_text = 'products.product.add_to_cart' | t
assign button_text = 'products.product.add_to_cart' | t
if template == 'product.preorder'
assign default_text = 'products.product.preorder' | t
assign button_text = 'products.product.preorder' | t
endif
unless current_variant.available
assign button_text = 'products.product.sold_out' | t
endunless
-%}
{%- if enable_dynamic_buttons -%}
{{ form | payment_button }}
{%- endif -%}
{%- if enable_dynamic_buttons -%}
{%- endif -%}
{{ form | payment_terms }}
{%- if product.options.size > 1 -%}
{%- endif -%}
{%- endform -%}
Hi @chels_grove ,
Make sure you follow you metafield settings here
I placed the code {% if product.metafields.custom.add_to_cart_button == true %} before the <button…> and make sure to close the statement with {% endif %}.
I am not sure why you have two enable_dynamic_buttons, but I enclose that also in the code.
Edited code
{%- form 'product', product, id: form_id, class: 'product-single__form' -%}
{%- liquid
assign enable_dynamic_buttons = false
if show_dynamic_checkout and template != 'product.preorder'
assign enable_dynamic_buttons = true
endif
-%}
{%- if enable_dynamic_buttons -%}
{%- endif -%}
{%- liquid
assign default_text = 'products.product.add_to_cart' | t
assign button_text = 'products.product.add_to_cart' | t
if template == 'product.preorder'
assign default_text = 'products.product.preorder' | t
assign button_text = 'products.product.preorder' | t
endif
unless current_variant.available
assign button_text = 'products.product.sold_out' | t
endunless
-%}
{% if product.metafields.custom.add_to_cart_button == true %}
{%- if enable_dynamic_buttons -%}
{{ form | payment_button }}
{%- endif -%}
{%- if enable_dynamic_buttons -%}
{%- endif -%}
{% endif %}
{{ form | payment_terms }}
{%- if product.options.size > 1 -%}
{%- endif -%}
{%- endform -%}
Thank you!! I tweaked it a little and cleaned up the code from the old developer (the double dynamic buttons) - final piece works:
{%- form 'product', product, id: form_id, class: 'product-single__form' -%}
{%- liquid
assign enable_dynamic_buttons = false
if show_dynamic_checkout and template != 'product.preorder'
assign enable_dynamic_buttons = true
endif
-%}
{%- if enable_dynamic_buttons -%}
{%- endif -%}
{%- liquid
assign default_text = 'products.product.add_to_cart' | t
assign button_text = 'products.product.add_to_cart' | t
if template == 'product.preorder'
assign default_text = 'products.product.preorder' | t
assign button_text = 'products.product.preorder' | t
endif
unless current_variant.available
assign button_text = 'products.product.sold_out' | t
endunless
-%}
{% if product.metafields.custom.add_to_cart_button == true %}
{% else %}
{% endif %}
{%- if enable_dynamic_buttons -%}
{{ form | payment_button }}
{%- endif -%}
{{ form | payment_terms }}
{%- if product.options.size > 1 -%}
{%- endif -%}
{%- endform -%}