Shopify / theme-extension-getting-started

A theme app extension boilerplate that highlights the basic structure and features that are available to developers who want to integrate their apps with Shopify Online Stores.
https://shopify.dev/apps/online-store/theme-app-extensions/getting-started
MIT License
105 stars 45 forks source link

What is Shopify Section object alternative? #11

Open rkaucorp opened 2 years ago

rkaucorp commented 2 years ago

I am trying to make a shopify theme extension where I have radio button and radio button form should hit form="{{ product_form_id }}"and product_form_id is coming from {%- assign product_form_id = 'product-form-' | append: section.id -%} since shopify restricted section object in theme extension how can I use this id ?

Sky-bits commented 2 years ago

@rkaucorp can you post the complete error tracer log

olso commented 1 year ago

.js workaround that will mostly work if merchant adds it to product page

  1. Create option for your block/embed where merchant can edit the selector query
{
        "label": "Product form selector",
        "id": "productFormSelector",
        "type": "textarea",
        "default": "form[id^='product-form-template']",
        "info": "Query for document.querySelector"
      },
  1. Make data available to .js via .liquid
<script type="text/json" data-CHANGE_ME>
  {
    "settings": {{ block.settings | json }}
  }
</script>
  1. In .js access that data
const scriptText = document.querySelector(
      "script[data-CHANGE_ME]"
    )?.textContent;

    if (!scriptText) return null;

    try {
      const dataRaw = JSON.parse(scriptText) as Maybe<DataRaw>;
  1. Create a function to get product form id
const getProductFormId: UseShopify["getProductFormId"] = useCallback(() => {
    const productForm = document.querySelector<HTMLFormElement>(
      data.blockSettings.productFormSelector
    );

    if (!productForm) {
      return null;
    }

    const id = productForm.getAttribute("id");

    if (!id) {
      return null;
    }

    return id;
  }, []);