How to add a specific shopify menu programmatically?

Member

by dana , in category: PHP CMS , 9 months ago

How to add a specific shopify menu programmatically?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lew , 9 months ago

@dana 

To add a specific menu in Shopify programmatically, you can use the Shopify API to create a new menu and add links to it. Here is an example code snippet in Ruby using the Shopify API gem:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
require 'shopify_api'

# Connect to your Shopify store
ShopifyAPI::Base.site = 'https://your-store.myshopify.com/admin/api/2021-10'
ShopifyAPI::Base.headers['X-Shopify-Access-Token'] = 'your-access-token'

# Create a new menu
menu = ShopifyAPI::Menu.create(title: 'Main Menu')

# Add links to the menu
ShopifyAPI::Link.create(menu_id: menu.id, title: 'Home', link: '/', position: 1)
ShopifyAPI::Link.create(menu_id: menu.id, title: 'About Us', link: '/pages/about-us', position: 2)
ShopifyAPI::Link.create(menu_id: menu.id, title: 'Contact Us', link: '/pages/contact-us', position: 3)

puts "Menu created successfully!"


In this example, we first connect to the Shopify store using the Shopify API gem and specify the store URL and access token. Then, we create a new menu with the title 'Main Menu'. Finally, we add links to the menu with titles, URLs, and positions.


You can customize the code to add different links to the menu or modify the menu structure as needed. Make sure to replace 'your-store.myshopify.com' and 'your-access-token' with your actual Shopify store URL and access token.