Entretoize Posted November 21, 2022 Share Posted November 21, 2022 Hello, I'm selling courses in my prestashop store, that have a specific duration from 1 hour to 7 hours. I need users to buy by multiple of 14 hours, for exemple a course of 6 and two of 4 hours (6+4*2=14). I think what I need is to hook on the cart page but I don't understand how. In a second time I think it will be good to display a bubble that say something like "You need to choose 3 more hours of courses" that stays visible while customers are filling their carts. I don't think it will be doable with a hook... Thanks in advance for any help. Link to comment Share on other sites More sharing options...
Entretoize Posted November 29, 2022 Author Share Posted November 29, 2022 Nobody ? Link to comment Share on other sites More sharing options...
ps8modules Posted November 30, 2022 Share Posted November 30, 2022 Hi. We would like to help, but it is not clear to us what you need from us. You can't do it by hook. Such things are done via module and javascript. Link to comment Share on other sites More sharing options...
ps8modules Posted November 30, 2022 Share Posted November 30, 2022 But you can create a custom.js file in the theme folder (if it doesn't exist): ./themes/your-theme/assests/js/custom.js And for example insert this javascript code: prestashop.on('updateCart', function(event) { if(event.resp != undefined && event.resp.cart != undefined) { var productsCountInCart = Number(event.resp.cart.products_count); var divisor = Number(14); var result = (productsCountInCart / divisor); if (Number.isInteger(result)){ alert('The result is an integer divisible by 14'); } else { alert('The result is not an integer'); } } }); It is enough to add to this code the calculation of how many products you have left to add, the quantity to a whole number. Link to comment Share on other sites More sharing options...
ps8modules Posted November 30, 2022 Share Posted November 30, 2022 Sample for update cart quantity: prestashop.on('updateCart', function(event) { if(event.resp != undefined && event.resp.cart != undefined) { var productsCountInCart = Number(event.resp.cart.products_count); var divisor = Number(14); var result = (productsCountInCart / divisor); var n = Math.ceil(result); var quotient = Number(0); if (productsCountInCart < divisor){ /* < 14 */ quotient = (divisor - productsCountInCart); } else { /* > 14 */ quotient = ((divisor * n) - productsCountInCart); } /* custom error mesage block */ var customErrorMessage = document.getElementById('my-custom-error-message'); /* add error to header */ var elementHeaderPage = document.getElementById('header'); if (typeof(elementHeaderPage) != 'undefined' && elementHeaderPage != null) { if (typeof(customErrorMessage) != 'undefined' && customErrorMessage != null) { customErrorMessage.remove(); } if (!Number.isInteger(result)){ elementHeaderPage.insertAdjacentHTML( 'afterbegin', '<div style="display:block;width:100%;background-color:#f5c8c8;padding: 15px;text-align: center;" id="my-custom-error-message">'+ '<span style="color:red;font-weight: bold;">You still need to add '+quotient+' hours of the course to your cart</span>'+ '</div>'); } } } }); result: Link to comment Share on other sites More sharing options...
ps8modules Posted November 30, 2022 Share Posted November 30, 2022 And you still need to create a hookDisplayHeader in the module and find out the number of products in the cart through the context, make a mathematical calculation similar to javascript and create a variable definition for javascript. Add a check after page load in javascript. Link to comment Share on other sites More sharing options...
Entretoize Posted November 30, 2022 Author Share Posted November 30, 2022 Thank you for your codes I didn't ask as much, that's cool. Javascript is ok for me, I just need to find how to add the js from a module because I would like to avoid modifying the theme... Link to comment Share on other sites More sharing options...
Entretoize Posted November 30, 2022 Author Share Posted November 30, 2022 I found how to add a javascript from a module, that's perfect, thank you again ! 1 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now