Integrating With Product Samples App: Event Hooks & Advanced Settings
The Product Samples App fires custom JavaScript events at key stages, making it easy to integrate with your existing Shopify theme or other third-party apps. You can listen for these events to trigger custom behaviors such as updating a mini cart, displaying notifications, or running your own code.
Event Reference
Event Name | Trigger | Description |
---|---|---|
ready.productSamples |
document.body |
Fired when the app is fully loaded and ready. |
beforeAddToSample.productSamples |
document.body |
Fired right after the “Get a Sample” button is clicked. |
addedToCart.productSamples |
document.body |
Fired after a sample is successfully added to the cart. Useful for triggering Ajax/slideout/mini cart. |
addToCartError.productSamples |
document.body |
Fired if adding a sample to the cart fails. Use to display error messages or alerts. |
sampleAdded.productSamples |
document.body |
Fired after a sample is added to the sample box. |
beforeCheckout.productSamples |
document.body |
Fired after the Checkout button in the sample box is clicked. |
afterCheckout.productSamples |
document.body |
Fired before redirecting to the Shopify checkout from the sample box. |
Example: Listening to an Event
document.body.addEventListener("ready.productSamples", function (result) {
var data = result.detail;
console.log(data);
console.log('Product Samples App is ready!');
});
Advanced Integration Options
You can further control and customize the app’s behavior using these advanced settings:
1. Re-initialize Sample Buttons (e.g., after Ajax page loads)
window.ProductSamplesApp.reInit();
2. Disable Page Reload After Adding to Cart
Enable Ajax mode and listen for successful cart adds:
ProductSample.type = 'ajax';
document.body.addEventListener("addedToCart.productSamples", function (result) {
// Your code to trigger minicart or slideout here
});
3. Custom Product ID Field
If your product form uses a custom input field name for the product ID:
ProductSample.CustomIDFieldName = 'yourCustomFieldName';
4. Exclude Properties from Line Item Properties
To exclude certain product properties from being added to cart line item properties:
ProductSample.excluded_properties = ["Vendor", "SKU", "OtherProperty"];
5. Force Load Sample Button on Collection Pages
To show the sample button on collection or other custom pages, add this snippet to each product item:
{%- form 'product', product, novalidate: 'novalidate' -%}
<input type="hidden" name="id" value="{{ product.selected_or_first_available_variant.id }}">
<div class="js-product-samples" data-sample="{{ product | json | url_encode }}"></div>
{%- endform -%}
Then enable:
ProductSample.loadSamplesButton = true;
6. Disable Redirect After Login
To prevent redirecting after login:
ProductSample.disableRedirectionAfterLogin = true;
Tip:
These hooks and options let you deeply customize the app’s integration and customer experience, whether you’re working on AJAX carts, custom notifications, or bespoke storefronts.
Was this article helpful?
Help us improve our documentation
Still need help?
Can't find what you're looking for? Our support team is here to help.
Contact SupportRelated Help Articles
Creating a Separated Sample Box
With this option enabled, NO ADDITIONAL PRODUCT is added into your store. App creates Draft…
Controlling App Visibility
You can control where the app appears – or limit it to specific themes. How…
How to prevent user from viewing Sample Product?
All Samples products will be redirected to Homepage. Follow these bellow guide to secure your…