Manage metaobjects
Metaobjects in Shopify empower merchants to define and manage custom data models on their storefronts or shops. Learn more about what metaobjects are here.
Add a new metaobject definition
You can add a new metaobject definition to your storefront or shop in Shopify:
- From your Shopify admin, go to Content > Metaobjects.
- Click Add definition.
- Fill in the fields to define your metaobject:
- Name - The name of the metaobject definition.
- Fields - Add fields to the metaobject definition which will define your object's structure.
- Access Options - check Storefronts
- Click Save.
Add a new metaobject entry
The feature to add a new metaobject directly in Pack without having to go to the Shopify admin is coming soon!
You can add a new metaobject entry to your storefront or shop in Shopify:
- From your Shopify admin, go to Content > Metaobjects.
- Click on the metaobject definition you want to add a new entry to.
- Click on Add entry.
- Fill out the fields you defined in the metaobject definition.
- Click Save.
Add a new metaobject section to your codebase
You can add a new metaobject section in your storefront or shop repository.
Here's an example of how to add a new metaobject section for a text block we created in the previous steps:
- Create a new section folder in
app/sections
with the following structure:
-
MetaobjectTextBlock.schema.ts
containing your component's schema. UsedataSource
to pull in the metaobject data from Shopify.export function Schema() { return { category: 'Text', label: 'Text Block', key: 'metaobject-text-block', previewSrc: 'https://cdn.shopify.com/s/files/1/0671/5074/1778/files/text-block-preview.jpg?v=1675730349', dataSource: { source: 'shopify', // where the data is coming from, in this case Shopify type: 'metaobject', // what type of data it is, in this case a metaobject reference: { type: 'text_block', // what the definition is called in Shopify }, }, fields: [], }; }
-
MetaobjectTextBlock.tsx
containing your component's code. You can reference thecms
prop to get the fields needed based off of what you defined in Shopify for the metaobject.export function MetaobjectTextBlock({ cms }) { const fields = cms?.dataSource?.reference?.fields?.reduce((acc, field) => { acc[field.key] = field.reference || field.value; return acc; }, {}); const { button_link, button_link_text, heading, subtext } = fields; return ( <div className="your-container-styles"> {heading && <h1>{heading}</h1>} {subtext && <div>{subtext}</div>} {button_link && <a href={button_link}>{button_link_text}</a>} </div> ); }
-
index.ts
exporting your new section component.export {MetaobjectTextBlock} from './MetaobjectTextBlock';
-
Register the new section in
app/sections/index.tsx
:import { MetaObjectTextBlock } from './MetaObjectTextBlock'; export function registerSections() { registerSection(MetaObjectTextBlock, { name: 'meta-object-text-block' }) }
-
Now, when you navigate to Pack's Customizer, you should see the new metaobject section available to add to your page. Once you add the section to your page, you will have access to all the entries you created in the Shopify admin.
Use metaobject sections in Pack's Customizer
Once you've added a new metaobject section to your codebase, you can use it in Pack's Customizer to add content to your pages.
- Navigate to Pack's Customizer.
- Click on + to add a new section to your page.
- Select Add New Section.
- Select the Metaobjects tab.
- Select the metaobject section you wish to add and press Add Selected.
- Click on the newly added section to select the metaobject entry you want to display.
- The metaobject section will now display the content from the selected entry.