Remix

Integrating Shopify Remix App with Theme App Extensions: A Guide to Using the Metafield API

Shopify’s ecosystem has evolved significantly with the introduction of Remix apps and Theme App Extensions. This powerful combination allows developers to build robust and interactive experiences for Shopify merchants. At the core of this integration lies the Metafield API, which acts as a crucial bridge between the two.

In this guide, we’ll explore how to integrate a Shopify Remix app with Shopify theme app extensions. We’ll also cover how to use the metafield API to render dynamic content based on app-specific data. This integration allows for a more customized storefront experience and leverages Shopify’s powerful metafield API.

Steps to Integrating Shopify Remix App with Theme App Extensions Using the Metafield API:

Step 1: Setting Up the Shopify Remix App

To begin, ensure you have a Shopify Remix app set up. This involves configuring your Remix project and connecting it with Shopify’s APIs. Here’s a basic setup to get started:

npx create-remix@latest

Follow the prompts to set up your Remix app with the appropriate Shopify integration.

Step 2: Configuring Theme App Extensions

Theme app extensions allow you to add custom sections and blocks to a Shopify theme. For this example, we’ll create a theme extension that includes a block for displaying video content.

Theme Extension Configuration:

In your theme extension’s configuration file (theme-extension.config.json), you might have something like this:

{
  "name": "VDO Basic",
  "stylesheet": "video-feed.css",
  "javascript": "slider.js",
  "available_if": "{{app.metafields.app_plan.api}}",
  "target": "section"
}

This configuration specifies that the extension should be applied to a section and depends on the app_plan.api metafield.

Step 3: Utilizing the Metafield API

The metafield API allows you to store and retrieve custom data for Shopify resources. Here’s how you can use it to set and retrieve metafields for your app:

export const loader = async ({ request }) => {
    const { session, admin } = await authenticate.admin(request);
    const userauth = await prisma.session.findFirst({
        where: { shop: session.shop },
        select: {
            planId: true,
            shopify_freemium: true,
            shop: true,
        }
    });

    const sessionshop = await prisma.session.findFirst({
        where: { shop: session.shop },
    });    

    const shopify_plan = await prisma.plan.findFirst({
        where: { shop: session.shop },
        select: {
            charge_id: true,
        }
    })

    const query = `
        query {
            currentAppInstallation {
                id
            }
        }
    `;

    const metafieldMutation = `mutation CreateAppDataMetafield($metafieldsSetInput: [MetafieldsSetInput!]!) {
        metafieldsSet(metafields: $metafieldsSetInput) {
            metafields {
                id
                namespace
                key
            }
            userErrors {
                field
                message
            }
        }
    }
    `;

    const response = await admin.graphql(query);
    const result = await response.json();
    console.log(result, "Response Data");

    const appId = result?.data?.currentAppInstallation?.id;
    const shop_plan = shopify_plan ? "true" : "false";
    console.log(shop_plan, "shop plan ...");

    const chargeid_value = appId ? "true" : "false";
    console.log(chargeid_value, "chargeid value...");

    const metafieldVariables = {
        "variables": {
            "metafieldsSetInput": [{
                "namespace": "app_plan",
                "key": "api",
                "type": "boolean",
                "value": shop_plan,
                "ownerId": appId
            }]
        }
    };

    console.log(JSON.stringify(metafieldVariables), "variable");

    const metafieldResponse = await admin.graphql(metafieldMutation, { ...metafieldVariables });
    const metafieldResult = await metafieldResponse.json();

    console.log(JSON.stringify(metafieldResult), "Metafield Response");
    return json({ userauth, privateMetafieldUpsert: metafieldResult, sessionshop });
}

Explanation:

  • Authentication and Session Retrieval: The loader function authenticates the user and retrieves session details.
  • Metafield Query and Mutation: It queries the current app installation ID and sets metafields related to the app plan.
  • Metafield Update: The metafield mutation updates the app_plan.api metafield with a boolean value.

Step 4: Rendering Blocks Dynamically

With the metafields set, your theme extension can now use these metafields to conditionally render blocks. In your theme’s Liquid files, you can use:

{% if settings.app_plan.api %}
    {% section 'video-feed' %}
{% endif %}

This Liquid code checks the value of app_plan.api and conditionally renders the video feed section if the metafield is set to true.

Conclusion:

By integrating Shopify Remix with theme app extensions and utilizing the metafield API, you can create dynamic and customized storefronts that respond to your app’s data. This approach enhances the user experience and allows for greater flexibility in theme customization.

Click to rate this post!
[Total: 2 Average: 5]
Bharat Desai

Bharat Desai is a Co-Founder at MageComp. He is an Adobe Magento Certified Frontend Developer ? with having 8+ Years of experience and has developed 150+ Magento 2 Products with MageComp. He has an unquenchable thirst to learn new things. On off days you can find him playing the game of Chess ♟️ or Cricket ?.

Recent Posts

Handling Forms and Data in Shopify Remix: useSubmit vs. useFetcher

In Shopify Remix, managing form submissions and data fetching is crucial for building interactive and…

1 day ago

SEO and Digital Marketing for Magento Stores

When positioning oneself in the constantly developing field of internet sales, it is critical to…

1 day ago

Emerging Shopify Trends That Student Entrepreneurs Should Know About

One major challenge student entrepreneurs encounter is difficulty balancing academics and business. Most find themselves…

1 day ago

How to Setup Vite in Shopify Remix App?

In this article, we will learn how to set up Vite in the Shopify remix…

2 days ago

Magento 2: How to Add View Button in Admin Grid to Open a View Page in Slide Window

Hello Magento Friends, In Magento 2, customizations to the admin panel can significantly enhance the…

3 days ago

Magento 2: How to Observe the Multi-shipping Order Creation Event

Hello Magento Friends, Magento 2 provides a robust event-driven architecture that allows developers to observe…

6 days ago