We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Override product items in collections page best practises

Solved

Override product items in collections page best practises

HashimovH
Shopify Partner
4 0 0

Hello dear community, 

I have been developing the apps, and I would like to learn about your views and best practices.

 

When I want to override or make product items more visually appealing, the main challenge is detecting the product item CSS selector and using pure JavaScript on the DOM. How do you ensure that your storefront updates are compatible with all themes? Currently, I am doing a lot of manual work updates, overrides for the store, and I would love to optimize this.

 

Example: I want to show a new button on the product grid for each item. 

 

Thank you!

Accepted Solution (1)

MandasaTech
Shopify Partner
816 156 168

This is an accepted solution.

Hi @HashimovH 


Great question — working with different Shopify themes can definitely be a challenge due to variations in markup and class naming conventions.

Here are a few best practices that might help you optimize your approach:

1. Use data-* Attributes When Possible

Shopify themes often include data-product-id, data-product-title, or similar attributes in product grid elements. These are much more reliable for targeting elements than CSS class names, which can vary from theme to theme.

document.querySelectorAll('[data-product-id]').forEach((productEl) => {
  // Append your custom button here
});

2. Defer Until DOM Is Ready

Always ensure your script runs after the DOM is fully loaded (especially important if you're working with sections or apps that lazy-load content):

document.addEventListener("DOMContentLoaded", function () {
  // your code
});

3. Theme-Agnostic Design

Avoid targeting classes like .product-card or .grid__item unless you're building for a specific theme. Instead, write logic that detects structural patterns (like child elements inside a container with a known role or data-* attribute).

4. Use Mutation Observers

If you're injecting content into dynamic areas (e.g., quick view modals, filters that re-render grids), MutationObserver can help:

const observer = new MutationObserver(() => {
  // re-apply your changes after DOM updates
});

observer.observe(document.body, { childList: true, subtree: true });

5. Fallback Logic

If you're building an app to be used across multiple themes, consider maintaining a small compatibility layer for the most popular themes where your selectors differ slightly.


Hope this helps! Keep building — the Shopify ecosystem definitely benefits from innovative developers like you.

Let me know if you have any issues with this!

☞ Helpful or Question answered? Please Click Like & Mark it Accepted Solution
☞ Want to modify or custom changes on store for affordable price? Click on Contact button here
☞ Email at experts@mandasa.in
☞ Whatsapp at +918989609120 | Hire us at: Website Support Page
☞ Selling Shopify Fundamentals: Verified Skill Badge

View solution in original post

Replies 3 (3)

Dotsquares
Shopify Partner
460 29 61

HI @HashimovH 

Great question—this is definitely one of the common challenges when working with storefront customizations across multiple Shopify themes.

Here are a few best practices that might help optimize your approach:

 

Use Shopify’s data-* attributes: Many Shopify themes follow certain conventions, and you can often target elements using data-product-id, data-product-handle, or similar attributes rather than relying solely on class names that vary by theme.

 

Build a fallback selector system: Create a small utility that checks for multiple possible selectors depending on the theme—this adds some flexibility without hardcoding for each theme.

 

Use Mutation Observers: If your app depends on elements loading dynamically (e.g., via infinite scroll or dynamic filtering), Mutation Observers can help you detect and inject your button at the right time.

 

Offer manual targeting as a backup: You could add a simple UI in your app that lets merchants select or confirm the selector for product grid items. This way, you avoid doing all the guesswork and give control to the merchant if your auto-detection fails.

 

Leverage App Blocks or App Embed blocks (Online Store 2.0): If your app supports Online Store 2.0, try moving as much logic as possible into theme app extensions. This gives merchants a drag-and-drop interface and eliminates a lot of manual overrides.

Dotsquares Ltd


Problem Solved? ✔ Accept and Like solution to help future merchants.


Shopify Partner Directory | Trustpilot | Portfolio

MandasaTech
Shopify Partner
816 156 168

This is an accepted solution.

Hi @HashimovH 


Great question — working with different Shopify themes can definitely be a challenge due to variations in markup and class naming conventions.

Here are a few best practices that might help you optimize your approach:

1. Use data-* Attributes When Possible

Shopify themes often include data-product-id, data-product-title, or similar attributes in product grid elements. These are much more reliable for targeting elements than CSS class names, which can vary from theme to theme.

document.querySelectorAll('[data-product-id]').forEach((productEl) => {
  // Append your custom button here
});

2. Defer Until DOM Is Ready

Always ensure your script runs after the DOM is fully loaded (especially important if you're working with sections or apps that lazy-load content):

document.addEventListener("DOMContentLoaded", function () {
  // your code
});

3. Theme-Agnostic Design

Avoid targeting classes like .product-card or .grid__item unless you're building for a specific theme. Instead, write logic that detects structural patterns (like child elements inside a container with a known role or data-* attribute).

4. Use Mutation Observers

If you're injecting content into dynamic areas (e.g., quick view modals, filters that re-render grids), MutationObserver can help:

const observer = new MutationObserver(() => {
  // re-apply your changes after DOM updates
});

observer.observe(document.body, { childList: true, subtree: true });

5. Fallback Logic

If you're building an app to be used across multiple themes, consider maintaining a small compatibility layer for the most popular themes where your selectors differ slightly.


Hope this helps! Keep building — the Shopify ecosystem definitely benefits from innovative developers like you.

Let me know if you have any issues with this!

☞ Helpful or Question answered? Please Click Like & Mark it Accepted Solution
☞ Want to modify or custom changes on store for affordable price? Click on Contact button here
☞ Email at experts@mandasa.in
☞ Whatsapp at +918989609120 | Hire us at: Website Support Page
☞ Selling Shopify Fundamentals: Verified Skill Badge
HashimovH
Shopify Partner
4 0 0

Hello, thank you so much for your reply. Points 1 and 3 are the ones I was missing. I will work on this in this way. Th