Jump to content

Create a hook to force some product to be bought together


Recommended Posts

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

  • 2 weeks later...

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

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:

obrazek.thumb.png.321a4a66ca12a3274258e223f9bac79d.png

Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...