desde tintalimon: no fallar si el producto no está en storage

This commit is contained in:
f 2021-10-27 15:32:59 -03:00
parent 922433319a
commit fd5763c0cd

View file

@ -34,11 +34,14 @@ export default class extends CartBaseController {
if (quantity < 1) return;
const orderToken = await this.tokenGetOrCreate()
const product = this.product
if (!product) return
event.target.disabled = true
const response = await this.spree.cart.setQuantity({ orderToken }, {
line_item_id: this.product.line_item.id,
line_item_id: product.line_item.id,
quantity,
include: 'line_items'
})
@ -60,7 +63,7 @@ export default class extends CartBaseController {
if (!this.hasSubtotalTarget) return
this.subtotalTarget.innerText = this.product.line_item.attributes.discounted_amount
this.subtotalTarget.innerText = product.line_item.attributes.discounted_amount
})
}
@ -110,7 +113,13 @@ export default class extends CartBaseController {
}
get product () {
return JSON.parse(this.storage.getItem(this.storageId))
const product = JSON.parse(this.storage.getItem(this.storageId))
if (!product) {
console.error("El producto es nulo!", this.storageId, this.storage.length, this.cart)
}
return product
}
/*
@ -183,10 +192,13 @@ export default class extends CartBaseController {
* item is removed, it removes itself from the page and the storage.
*/
async remove () {
if (!this.product.line_item) return
const product = this.product
if (!product) return
if (!product.line_item) return
const orderToken = this.token
const response = await this.spree.cart.removeItem({ orderToken }, this.product.line_item.id, { include: 'line_items' })
const response = await this.spree.cart.removeItem({ orderToken }, product.line_item.id, { include: 'line_items' })
if (response.isFail()) {
this.handleFailure(response)
@ -225,14 +237,18 @@ export default class extends CartBaseController {
* Recovers the order if something failed
*/
async recover () {
console.error('Recuperando pedido', this.token)
// Removes the failing token
this.storage.removeItem('token')
// Get a new token and cart
await this.tokenGetOrCreate()
// Stores the previous cart
const cart = this.cart
// Get a new token and cart
await this.tokenGetOrCreate()
if (!cart) return
// Add previous items and their quantities to the new cart by
// mimicking user's actions
@ -243,6 +259,8 @@ export default class extends CartBaseController {
const product = this.product
if (!product) continue
this.data.set('image', product.image)
this.data.set('title', product.title)
this.data.set('extra', product.extra.join('|'))