How do I execute arbitrary JavaScript when the app applies/removes discounts? (Discounts)
The app exposes several events that you can listen to. Use the data inside the event payload as needed.
When the app starts applying/updating/removing a discount:
document.body.addEventListener('docapp-discount-applying', (e) => { console.log(JSON.stringify(e)); });
When a discount code is entered in the field by a customer:
document.body.addEventListener('docapp-discount-code-submit', (e) => { console.log(e.code); });
If you want to stop the update process at this time, use the code below. The app will update any widgets on the next update cycle, or when you call window.discountOnCartProApp.applyCode('SOME-CODE')
window.discountOnCartProApp.pauseUpdate = true;
When the app finishes a load cycle (applying/updating/removing a discount):
document.body.addEventListener('docapp-discount-applied', (e) => { console.log(JSON.stringify(e)); });
When the app's widgets' HTML is set. This is similar to docapp-discount-applied but also emitted when a new widget gets inserted in the page without the cart or discounts being changed, for example when a drawer cart is opened.
document.body.addEventListener('docapp-widgets-updated', (e) => { console.log(JSON.stringify(e.payload)); });
When a discount code is successfully applied. This is triggered the first time a discount code is applied for a customer's session.
document.body.addEventListener('docapp-discount-success', (e) => { console.log(e.code); });
When a discount code fails to be applied. This is triggered every time a discount code fails to be applied.
document.body.addEventListener('docapp-discount-failure', (e) => { console.log(e.code); });
When a customer enters a new discount to apply.
document.body.addEventListener('docapp-discount-code-submit', (e) => { console.log('New code applying!'); });
When a customer removes a discount code.
document.body.addEventListener('docapp-discount-code-remove', (e) => { console.log('Code is being removed'); });
When the app hijacks a Checkout click in order to apply custom discounts that would appear on Checkout. This can be used to integrate tracking scripts with any Shop Doctors app.
document.body.addEventListener('docapp-checkout-start', (e) => { console.log('Checkout clicked!'); });
How do I ask the app to apply a discount code programmatically?
Use the call:
window.discountOnCartProApp.applyCode('SOME_CODE');
How do I ask the app to remove a discount code programmatically?
By default you would use the method below in order to not only remove the discount code from the app's data and widgets, but also to remove the code that Shopify's Checkout has remembered in its internal data.
window.discountOnCartProApp.removeCode('SOME_CODE');
If you will be calling the "applyCode" method immediately after "removeCode", then you should skip the clearing of Shopify's Checkout discount (to prevent conflicts) and instead use the method below.
window.discountOnCartProApp.removeCodeWithoutCartDiscountReset('SOME_CODE');
When the app starts applying/updating/removing a discount:
document.body.addEventListener('docapp-discount-applying', (e) => { console.log(JSON.stringify(e)); });
When a discount code is entered in the field by a customer:
document.body.addEventListener('docapp-discount-code-submit', (e) => { console.log(e.code); });
If you want to stop the update process at this time, use the code below. The app will update any widgets on the next update cycle, or when you call window.discountOnCartProApp.applyCode('SOME-CODE')
window.discountOnCartProApp.pauseUpdate = true;
When the app finishes a load cycle (applying/updating/removing a discount):
document.body.addEventListener('docapp-discount-applied', (e) => { console.log(JSON.stringify(e)); });
When the app's widgets' HTML is set. This is similar to docapp-discount-applied but also emitted when a new widget gets inserted in the page without the cart or discounts being changed, for example when a drawer cart is opened.
document.body.addEventListener('docapp-widgets-updated', (e) => { console.log(JSON.stringify(e.payload)); });
When a discount code is successfully applied. This is triggered the first time a discount code is applied for a customer's session.
document.body.addEventListener('docapp-discount-success', (e) => { console.log(e.code); });
When a discount code fails to be applied. This is triggered every time a discount code fails to be applied.
document.body.addEventListener('docapp-discount-failure', (e) => { console.log(e.code); });
When a customer enters a new discount to apply.
document.body.addEventListener('docapp-discount-code-submit', (e) => { console.log('New code applying!'); });
When a customer removes a discount code.
document.body.addEventListener('docapp-discount-code-remove', (e) => { console.log('Code is being removed'); });
When the app hijacks a Checkout click in order to apply custom discounts that would appear on Checkout. This can be used to integrate tracking scripts with any Shop Doctors app.
document.body.addEventListener('docapp-checkout-start', (e) => { console.log('Checkout clicked!'); });
How do I ask the app to apply a discount code programmatically?
Use the call:
window.discountOnCartProApp.applyCode('SOME_CODE');
How do I ask the app to remove a discount code programmatically?
By default you would use the method below in order to not only remove the discount code from the app's data and widgets, but also to remove the code that Shopify's Checkout has remembered in its internal data.
window.discountOnCartProApp.removeCode('SOME_CODE');
If you will be calling the "applyCode" method immediately after "removeCode", then you should skip the clearing of Shopify's Checkout discount (to prevent conflicts) and instead use the method below.
window.discountOnCartProApp.removeCodeWithoutCartDiscountReset('SOME_CODE');
Updated on: 09/05/2024