Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update main product sibling sections when switching between combined listing children #3368

Conversation

lhoffbeck
Copy link
Contributor

@lhoffbeck lhoffbeck commented Mar 25, 2024

PR Summary:

All child sections of a main product may contain references to the product through metafields. Given this, we want to ensure that all sections of the main product are refreshed when switching between child products.

Demo video of error: https://screenshot.click/23-29-tjpdt-m09nb.mp4

What approach did you take?

Chatted with @tyleralsbury , the recommendation was to request a full page update concurrently with the section api call to update the product info section, then use the response to update other sections. (The full page request seems to take anywhere from a few to a few hundred MS longer than the product section request so it likely makes sense to keep this as a separate request.)

Other considerations

  • We could add a refreshable-section component that listens for an event and self-refresh.
  • product-info.js could declaratively update all sections associated with the product

Visual impact on existing themes

This will only impact combined listings products

Demo links

Checklist

@lhoffbeck lhoffbeck changed the base branch from main to add-product-wrapper-class March 25, 2024 19:30
@lhoffbeck lhoffbeck changed the title Lh prototype combined listing pdp section updates Update main product sibling sections when switching between combined listing children Mar 25, 2024
@lhoffbeck lhoffbeck force-pushed the lh-prototype-combined-listing-pdp-section-updates branch from d632f13 to c760ec5 Compare March 25, 2024 20:26
@lhoffbeck lhoffbeck force-pushed the lh-prototype-combined-listing-pdp-section-updates branch from f531545 to 2a22f8a Compare March 26, 2024 13:01
);

document.querySelectorAll('main > .shopify-section').forEach((section) => {
if (!section.id.includes('__main') && updatedSections[section.id]) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tyleralsbury / @tauthomas01 I'm not sure how section IDs are generated.... is it a safe assumption to make that the section IDs will match between the initial product and the sibling products?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Section id is generated via the JSON template so it should be fine.

In this case, __main is generated from the product.json templates. These are section identifier that you can set in the template.

Here's an example of collection page:

alt

.then((response) => response.text())
.then((responseText) => {
const html = new DOMParser().parseFromString(responseText, 'text/html');
const updatedSections = Array.from(html.querySelectorAll('main > .shopify-section')).reduce(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tyleralsbury / @tauthomas01 It looked like selecting shopify-sections under main targets all sections that could contain a product metafield (and so would need to be updated). Is that correct?

}
});
});
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is pretty naive and assumes that sibling products of a combined listing will use the same product template. If the product sections differ the UX will be pretty poor.

On the other hand, the primary use case for combined listings is to combine many products to appear as a single product. Because of this, the functionality between combined listing children should be identical in most cases, and children having different content should be a slim edge case.

Given that this isn't a one-way door (e.g. merchants can update theme code to accommodate multi-template CLs) and that it is an edge case, our designer on combined listings thought this was an acceptable tradeoff.

What do you think?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a reasonable assumption. Is there anyway to detect if the template changes between child products and print a warning or anything?

I'm assuming this would manifest by updates not working or looking wonky, and I could see it being confusing/frustrating to try and figure out what is going on if you don't realize products have different templates or know that is a limitation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah good idea, maybe we add a console.error that mentions that's what's happening here 🤔

@lhoffbeck lhoffbeck force-pushed the lh-prototype-combined-listing-pdp-section-updates branch from 20510e2 to 2a22f8a Compare March 26, 2024 17:44
Copy link

@MiniLuke MiniLuke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, tophat checks out for me. Thanks for the good explanations and annotations.

}
});
});
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a reasonable assumption. Is there anyway to detect if the template changes between child products and print a warning or anything?

I'm assuming this would manifest by updates not working or looking wonky, and I could see it being confusing/frustrating to try and figure out what is going on if you don't realize products have different templates or know that is a limitation.

#handleSwapProduct() {
return (html) => {
this.productModal?.remove();
const newProduct = html.querySelector('product-info');
this.swapProductUtility.viewTransition(this, newProduct);
this.relatedProducts?.initializeRecommendations(newProduct.dataset.productId);
this.quickOrderList?.refresh();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume these sections are covered by the overall updateProductSections now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right exactly

);

document.querySelectorAll('main > .shopify-section').forEach((section) => {
if (!section.id.includes('__main') && updatedSections[section.id]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Section id is generated via the JSON template so it should be fine.

In this case, __main is generated from the product.json templates. These are section identifier that you can set in the template.

Here's an example of collection page:

alt

});
});
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO this function should publish the section_refreshed event once complete

@lhoffbeck lhoffbeck mentioned this pull request May 17, 2024
9 tasks
@lhoffbeck lhoffbeck force-pushed the add-product-wrapper-class branch 8 times, most recently from 831e093 to 45f65d3 Compare May 23, 2024 15:58
@lhoffbeck lhoffbeck force-pushed the lh-prototype-combined-listing-pdp-section-updates branch from 2a22f8a to 7e35aa4 Compare May 23, 2024 16:06
@lhoffbeck
Copy link
Contributor Author

Closing, this will be fixed by the changes introduced here: #3289

Ultimately, the approach that combined listings takes for swapping product content is:

  • If the product is a main-product: Fetch and replace the entire content of <main>. In this case, any other sections on the page may need to be refreshed due to dependencies on the main product--recommended products based on main product ID, content powered by product metafields, etc. Additionally, combined listing siblings may have different product templates and contain different sections.
  • If the product is in an embedded context (modal, featured-product, etc): Replace just the <product-info> component.

@lhoffbeck lhoffbeck closed this May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants