| 1 |
import { |
| 2 |
ComboConfiguratorDialog |
| 3 |
} from '@sale/js/combo_configurator_dialog/combo_configurator_dialog'; |
| 4 |
import { ProductCombo } from '@sale/js/models/product_combo'; |
| 5 |
import { |
| 6 |
ProductConfiguratorDialog |
| 7 |
} from '@sale/js/product_configurator_dialog/product_configurator_dialog'; |
| 8 |
import { getSelectedCustomPtav, serializeComboItem } from '@sale/js/sale_utils'; |
| 9 |
import { browser } from '@web/core/browser/browser'; |
| 10 |
import { serializeDateTime } from '@web/core/l10n/dates'; |
| 11 |
import { rpc } from '@web/core/network/rpc'; |
| 12 |
import { registry } from '@web/core/registry'; |
| 13 |
import { redirect } from '@web/core/utils/urls'; |
| 14 |
import { session } from '@web/session'; |
| 15 |
|
| 16 |
const { DateTime } = luxon; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
export class CartService { |
| 38 |
static dependencies = ['cartNotificationService', 'dialog']; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
constructor() { |
| 50 |
return this.setup(...arguments); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
setup(_env, services) { |
| 64 |
this.cartNotificationService = services.cartNotificationService; |
| 65 |
this.dialog = services.dialog; |
| 66 |
this.rpc = rpc; |
| 67 |
|
| 68 |
|
| 69 |
return { |
| 70 |
add: (...args) => this.add(...args) |
| 71 |
}; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
async add({ |
| 113 |
productTemplateId, |
| 114 |
productId = undefined, |
| 115 |
quantity = 1, |
| 116 |
uomId = undefined, |
| 117 |
ptavs = [], |
| 118 |
productCustomAttributeValues = [], |
| 119 |
noVariantAttributeValues = [], |
| 120 |
isCombo = false, |
| 121 |
...rest |
| 122 |
}, |
| 123 |
{ |
| 124 |
isBuyNow=false, |
| 125 |
redirectToCart=true, |
| 126 |
isConfigured=false, |
| 127 |
showQuantity=true, |
| 128 |
} = {}, |
| 129 |
) { |
| 130 |
if (!productId && ptavs.length) { |
| 131 |
productId = await this.rpc('/sale/create_product_variant', { |
| 132 |
product_template_id: productTemplateId, |
| 133 |
product_template_attribute_value_ids: ptavs.concat(noVariantAttributeValues), |
| 134 |
}) |
| 135 |
} |
| 136 |
|
| 137 |
if(isCombo) { |
| 138 |
const { combos, ...remainingData } = await this.rpc( |
| 139 |
'/website_sale/combo_configurator/get_data', |
| 140 |
{ |
| 141 |
product_tmpl_id: productTemplateId, |
| 142 |
quantity: quantity, |
| 143 |
|
| 144 |
date: serializeDateTime(DateTime.now()), |
| 145 |
...rest |
| 146 |
} |
| 147 |
); |
| 148 |
const preselectedComboItems = combos |
| 149 |
.map(combo => new ProductCombo(combo)) |
| 150 |
.map(combo => combo.preselectedComboItem) |
| 151 |
.filter(Boolean); |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
if (preselectedComboItems.length === combos.length) { |
| 156 |
return this._makeRequest({ |
| 157 |
productTemplateId: productTemplateId, |
| 158 |
productId: productId, |
| 159 |
quantity: remainingData.quantity, |
| 160 |
uomId: uomId, |
| 161 |
linked_products: preselectedComboItems.map( |
| 162 |
(comboItem) => this._serializeComboItem( |
| 163 |
comboItem, productTemplateId, remainingData.quantity |
| 164 |
) |
| 165 |
), |
| 166 |
shouldRedirectToCart: isBuyNow && redirectToCart, |
| 167 |
...rest |
| 168 |
}); |
| 169 |
} |
| 170 |
|
| 171 |
return this._openComboConfigurator( |
| 172 |
productTemplateId, |
| 173 |
productId, |
| 174 |
combos.map(combo => new ProductCombo(combo)), |
| 175 |
remainingData, |
| 176 |
{ |
| 177 |
isBuyNow: isBuyNow, |
| 178 |
showQuantity: showQuantity, |
| 179 |
}, |
| 180 |
rest |
| 181 |
); |
| 182 |
} |
| 183 |
|
| 184 |
if (isBuyNow) { |
| 185 |
return this._makeRequest({ |
| 186 |
productTemplateId, |
| 187 |
productId, |
| 188 |
quantity, |
| 189 |
uomId, |
| 190 |
productCustomAttributeValues, |
| 191 |
noVariantAttributeValues, |
| 192 |
shouldRedirectToCart: isBuyNow && redirectToCart, |
| 193 |
...rest |
| 194 |
}); |
| 195 |
} |
| 196 |
|
| 197 |
const shouldShowProductConfigurator = await this.rpc( |
| 198 |
'/website_sale/should_show_product_configurator', |
| 199 |
{ |
| 200 |
product_template_id: productTemplateId, |
| 201 |
ptav_ids: ptavs, |
| 202 |
is_product_configured: isConfigured, |
| 203 |
} |
| 204 |
); |
| 205 |
if (shouldShowProductConfigurator) { |
| 206 |
return this._openProductConfigurator( |
| 207 |
productTemplateId, |
| 208 |
quantity, |
| 209 |
uomId, |
| 210 |
ptavs.concat(noVariantAttributeValues), |
| 211 |
productCustomAttributeValues, |
| 212 |
{ |
| 213 |
isBuyNow: isBuyNow, |
| 214 |
isMainProductConfigurable: !isConfigured, |
| 215 |
showQuantity: showQuantity, |
| 216 |
}, |
| 217 |
rest |
| 218 |
); |
| 219 |
} |
| 220 |
|
| 221 |
return this._makeRequest({ |
| 222 |
productTemplateId, |
| 223 |
productId, |
| 224 |
quantity, |
| 225 |
uomId, |
| 226 |
productCustomAttributeValues, |
| 227 |
noVariantAttributeValues, |
| 228 |
shouldRedirectToCart: isBuyNow && redirectToCart, |
| 229 |
...rest |
| 230 |
}); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
async _openComboConfigurator( |
| 259 |
productTemplateId, |
| 260 |
productId, |
| 261 |
combos, |
| 262 |
remainingData, |
| 263 |
options, |
| 264 |
additionalData |
| 265 |
) { |
| 266 |
return await new Promise((resolve) => { |
| 267 |
this.dialog.add(ComboConfiguratorDialog, { |
| 268 |
combos: combos, |
| 269 |
...remainingData, |
| 270 |
date: serializeDateTime(DateTime.now()), |
| 271 |
edit: false, |
| 272 |
isFrontend: true, |
| 273 |
options, |
| 274 |
...additionalData, |
| 275 |
save: async (comboProductData, selectedComboItems, options) => { |
| 276 |
resolve(this._makeRequest({ |
| 277 |
productTemplateId: productTemplateId, |
| 278 |
productId: productId, |
| 279 |
quantity: comboProductData.quantity, |
| 280 |
|
| 281 |
linked_products: selectedComboItems.map( |
| 282 |
(comboItem) => this._serializeComboItem( |
| 283 |
comboItem, productTemplateId, comboProductData.quantity |
| 284 |
) |
| 285 |
), |
| 286 |
shouldRedirectToCart: options.goToCart, |
| 287 |
...additionalData, |
| 288 |
})); |
| 289 |
}, |
| 290 |
discard: () => resolve(0), |
| 291 |
}); |
| 292 |
}); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
async _openProductConfigurator( |
| 315 |
productTemplateId, |
| 316 |
quantity, |
| 317 |
uomId, |
| 318 |
combination, |
| 319 |
productCustomAttributeValues, |
| 320 |
options, |
| 321 |
additionalData |
| 322 |
) { |
| 323 |
return await new Promise((resolve) => { |
| 324 |
this.dialog.add(ProductConfiguratorDialog, { |
| 325 |
productTemplateId: productTemplateId, |
| 326 |
ptavIds: combination, |
| 327 |
customPtavs: productCustomAttributeValues.map(customPtav => ({ |
| 328 |
id: customPtav.custom_product_template_attribute_value_id, |
| 329 |
value: customPtav.custom_value, |
| 330 |
})), |
| 331 |
quantity: quantity, |
| 332 |
productUOMId: uomId, |
| 333 |
soDate: serializeDateTime(DateTime.now()), |
| 334 |
edit: false, |
| 335 |
isFrontend: true, |
| 336 |
selectedComboItems: [], |
| 337 |
options, |
| 338 |
...additionalData, |
| 339 |
save: async (mainProduct, optionalProducts, options) => { |
| 340 |
const product = this._serializeProduct(mainProduct); |
| 341 |
resolve(this._makeRequest({ |
| 342 |
productTemplateId: product.product_template_id, |
| 343 |
productId: product.product_id, |
| 344 |
quantity: product.quantity, |
| 345 |
uom_id: product.uom_id, |
| 346 |
productCustomAttributeValues: product.product_custom_attribute_values, |
| 347 |
noVariantAttributeValues: product.no_variant_attribute_value_ids, |
| 348 |
linked_products: optionalProducts.map(this._serializeProduct), |
| 349 |
shouldRedirectToCart: options.goToCart, |
| 350 |
...additionalData, |
| 351 |
})); |
| 352 |
}, |
| 353 |
discard: () => resolve(0), |
| 354 |
}); |
| 355 |
}); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
_serializeProduct(product) { |
| 367 |
let serializedProduct = { |
| 368 |
product_id: product.id, |
| 369 |
product_template_id: product.product_tmpl_id, |
| 370 |
parent_product_template_id: product.parent_product_tmpl_id, |
| 371 |
quantity: product.quantity, |
| 372 |
uom_id: product.uom.id, |
| 373 |
} |
| 374 |
|
| 375 |
if (!product.attribute_lines) { |
| 376 |
return serializedProduct; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
serializedProduct.product_custom_attribute_values = []; |
| 381 |
for (const ptal of product.attribute_lines) { |
| 382 |
const selectedCustomPtav = getSelectedCustomPtav(ptal); |
| 383 |
if (selectedCustomPtav) { |
| 384 |
serializedProduct.product_custom_attribute_values.push({ |
| 385 |
custom_product_template_attribute_value_id: selectedCustomPtav.id, |
| 386 |
custom_value: ptal.customValue ?? '', |
| 387 |
}); |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
serializedProduct.no_variant_attribute_value_ids = product.attribute_lines |
| 393 |
.filter(ptal => ptal.create_variant === 'no_variant') |
| 394 |
.flatMap(ptal => ptal.selected_attribute_value_ids); |
| 395 |
|
| 396 |
return serializedProduct; |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
_serializeComboItem(comboItem, parentProductTemplateId, quantity) { |
| 411 |
return { |
| 412 |
product_template_id: comboItem.product.product_tmpl_id, |
| 413 |
parent_product_template_id: parentProductTemplateId, |
| 414 |
quantity: quantity, |
| 415 |
...serializeComboItem(comboItem), |
| 416 |
}; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
async _makeRequest({ |
| 446 |
productTemplateId, |
| 447 |
productId, |
| 448 |
quantity, |
| 449 |
uomId=undefined, |
| 450 |
productCustomAttributeValues=[], |
| 451 |
noVariantAttributeValues=[], |
| 452 |
shouldRedirectToCart=false, |
| 453 |
...rest |
| 454 |
}) { |
| 455 |
const data = await this.rpc('/shop/cart/add', { |
| 456 |
product_template_id: productTemplateId, |
| 457 |
product_id: productId, |
| 458 |
quantity: quantity, |
| 459 |
uom_id: uomId, |
| 460 |
product_custom_attribute_values: productCustomAttributeValues, |
| 461 |
no_variant_attribute_value_ids: noVariantAttributeValues, |
| 462 |
...rest |
| 463 |
}); |
| 464 |
|
| 465 |
if (shouldRedirectToCart || session.add_to_cart_action === 'go_to_cart') { |
| 466 |
redirect('/shop/cart'); |
| 467 |
return data.quantity; |
| 468 |
} |
| 469 |
if (data.cart_quantity && ( |
| 470 |
data.cart_quantity !== browser.sessionStorage.getItem('website_sale_cart_quantity') |
| 471 |
)) { |
| 472 |
this._updateCartIcon(data.cart_quantity); |
| 473 |
}; |
| 474 |
this._showCartNotification(data.notification_info); |
| 475 |
if (data.quantity) { |
| 476 |
this._trackProducts(data.tracking_info); |
| 477 |
} |
| 478 |
return data.quantity; |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
_updateCartIcon(cartQuantity) { |
| 490 |
browser.sessionStorage.setItem('website_sale_cart_quantity', cartQuantity); |
| 491 |
|
| 492 |
const cartQuantityElements = document.querySelectorAll('.my_cart_quantity'); |
| 493 |
for(const cartQuantityElement of cartQuantityElements) { |
| 494 |
if (cartQuantity === 0) { |
| 495 |
cartQuantityElement.classList.add('d-none'); |
| 496 |
} else { |
| 497 |
const cartIconElement = document.querySelector('li.o_wsale_my_cart'); |
| 498 |
cartIconElement.classList.remove('d-none'); |
| 499 |
cartQuantityElement.classList.remove('d-none'); |
| 500 |
cartQuantityElement.classList.add('o_mycart_zoom_animation'); |
| 501 |
setTimeout(() => { |
| 502 |
cartQuantityElement.textContent = cartQuantity; |
| 503 |
cartQuantityElement.classList.remove('o_mycart_zoom_animation'); |
| 504 |
}, 300); |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
_showCartNotification(props, options = {}) { |
| 519 |
if (props.lines) { |
| 520 |
this.cartNotificationService.add('', { |
| 521 |
lines: props.lines, |
| 522 |
currency_id: props.currency_id, |
| 523 |
...options, |
| 524 |
}); |
| 525 |
} |
| 526 |
if (props.warning) { |
| 527 |
this.cartNotificationService.add('', { |
| 528 |
warning: props.warning, |
| 529 |
...options, |
| 530 |
}); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
_trackProducts(trackingInfo) { |
| 543 |
document.querySelector('.oe_website_sale').dispatchEvent( |
| 544 |
new CustomEvent('add_to_cart_event', {'detail': trackingInfo}) |
| 545 |
); |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
export const cartService = { |
| 550 |
dependencies: CartService.dependencies, |
| 551 |
async: ['add'], |
| 552 |
start(env, dependencies) { |
| 553 |
return new CartService(env, dependencies); |
| 554 |
}, |
| 555 |
} |
| 556 |
|
| 557 |
registry.category('services').add('cart', cartService); |