PageSourceSearch

https://www.pluscolor.es/modules/doofinder/views/js/add-to-cart/doofinder-add_to_cart_ps17.js

js pluscolor.es collected 2026-10-06 09:28:48 UTC 5,782 bytes, 156 lines download raw bytes

1/**
2 * NOTICE OF LICENSE
3 *
4 * This file is licenced under the Software License Agreement.
5 * With the purchase or the installation of the software in your application
6 * you accept the licence agreement.
7 *
8 * You must not modify, adapt or create derivative works of this source code
9 *
10 * @author    Doofinder
11 * @copyright Doofinder
12 * @license   GPLv3
13 */
14
15class DoofinderAddToCartError extends Error {
16    constructor(reason, status = "") {
17      const message = "Error adding an item to the cart. Reason: " + reason + ". Status code: " + status;
18      super(message);
19      this.name = "DoofinderAddToCartError";
20    }
21}
22
23//implementation of "add to cart" functionality for prestashop 1.7.x
24
25let dfAddToCart = (cartOptions) => {
26
27    let IdProductCart;
28    let IdCustomization;
29    if (cartOptions.productID.includes('VAR-')) {
30        IdProductCart = cartOptions.group_id;
31        IdCustomization = cartOptions.productID.replace('VAR-', ''); 
32    } else {
33        IdProductCart = cartOptions.productID;
34        IdCustomization = cartOptions.customizationID;
35    }
36
37    /**
38     * 1º create the necessary form to add to cart with the required inputs.
39     * (the "name" attribute of the inputs is necessary to trigger the event that prestashop uses natively
40     * for "add to cart").
41     */
42    let form = document.createElement("form");
43    Object.assign(form, {
44        method: "post",
45        action: cartOptions.cartURL,
46        style: "display: none;"
47    });
48
49    let quantityInput = document.createElement("input");
50    Object.assign(quantityInput, {
51        type    : "number",
52        name    : "qty",
53        value   : cartOptions.quantity,
54        min     : 1
55    });
56
57    let productAttributeInput = document.createElement("input");
58    Object.assign(productAttributeInput, {
59        type    : "hidden",
60        name    : "id_product_attribute",
61        value   : IdCustomization
62    });
63
64    let productInput = document.createElement("input");
65    Object.assign(productInput, {
66        type    : "hidden",
67        name    : "id_product",
68        value   : IdProductCart
69    });
70
71    let tokenInput = document.createElement("input");
72    Object.assign(tokenInput, {
73        type    : "hidden",
74        name    : "token",
75        value   : cartOptions.cartToken
76    });
77
78    let submit = document.createElement("input");
79    submit.setAttribute("type", "submit");
80    submit.setAttribute("data-button-action", "add-to-cart");
81
82    form.appendChild(quantityInput);
83    form.appendChild(productAttributeInput);
84    form.appendChild(productInput);
85    form.appendChild(tokenInput);
86    form.appendChild(submit);
87
88    /**
89     * 2º Add to DOM the form.
90     */
91    document.body.appendChild(form);
92
93    if (typeof prestashop !== 'undefined' && typeof cartOptions.statusPromise !== 'undefined') {
94
95        // It's triggered everytime the cart is updated.
96        prestashop.on('updateCart', function (event) {
97            // This should be trigger only if the cart has been updated from the layer. Otherwise, the statusPromise will be undefined.
98            if (typeof cartOptions.statusPromise === 'undefined' || 
99            !event || 
100            !event.reason || 
101            !event.resp) {
102                return;
103            }
104            
105            if (event.resp.success) {
106                cartOptions.statusPromise.resolve("The item has been successfully added to the cart.");
107            } else {
108                // The event.resp.errors can be "" or an array
109                errorList = Array.isArray(event.resp.errors) ? event.resp.errors.join(", ") : event.resp.errors;
110                console.warn("Error adding to the cart: " + errorList);
111                cartOptions.statusPromise.reject(new DoofinderAddToCartError(errorList));
112                /* Special case: the product cannot be added because is customizable.
113                I don't know if it's the best way to detect it, but at least it's better than
114                detecting it from the error message. In this case, the user should be redirected
115                to the product detail page */
116                if (0 === event.resp.quantity && 'undefined' === typeof event.reason.cart) {
117                    window.location.href = cartOptions.itemLink;
118                }
119            }
120        });
121
122        prestashop.on('handleError', function (event) {
123             // This should be trigger only if the cart has been updated from the layer. Otherwise, the statusPromise will be undefined.
124            if (typeof cartOptions.statusPromise === 'undefined') {
125                return;
126            }
127
128            /* Possible errors variantions coming from the add to cart */
129            if (event && event.eventType && ('updateProductInCart' === event.eventType || 
130            'updateShoppingCart' === event.eventType || 
131            'updateCart' === event.eventType || 
132            'updateProductInCart' === event.eventType || 
133            'updateProductQuantityInCart' === event.eventType || 
134            'addProductToCart' === event.eventType)) {
135                let message = "Unknown error";
136                /* Empirically I've have reproduced this error after loading another page in a specific moment  */
137                if (0 === event.resp.readyState) {
138                    message = "The connection was interrupted while adding to the cart";
139                }
140                console.error(message);
141                cartOptions.statusPromise.reject(new DoofinderAddToCartError(message));
142            }
143        });
144    }
145      
146    /**
147     * 3º clicks on the submit (does not work if you invoke the submit event) 
148     * and removes the form from the DOM
149     */
150    submit.click();
151    form.remove();
152}
153
154let doofinderManageCart = (cartOptions) => {
155    dfAddToCart(cartOptions);
156}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.