desde tintalimon: no fallar si el producto no está en storage
This commit is contained in:
parent
922433319a
commit
fd5763c0cd
1 changed files with 25 additions and 7 deletions
|
@ -34,11 +34,14 @@ export default class extends CartBaseController {
|
||||||
if (quantity < 1) return;
|
if (quantity < 1) return;
|
||||||
|
|
||||||
const orderToken = await this.tokenGetOrCreate()
|
const orderToken = await this.tokenGetOrCreate()
|
||||||
|
const product = this.product
|
||||||
|
|
||||||
|
if (!product) return
|
||||||
|
|
||||||
event.target.disabled = true
|
event.target.disabled = true
|
||||||
|
|
||||||
const response = await this.spree.cart.setQuantity({ orderToken }, {
|
const response = await this.spree.cart.setQuantity({ orderToken }, {
|
||||||
line_item_id: this.product.line_item.id,
|
line_item_id: product.line_item.id,
|
||||||
quantity,
|
quantity,
|
||||||
include: 'line_items'
|
include: 'line_items'
|
||||||
})
|
})
|
||||||
|
@ -60,7 +63,7 @@ export default class extends CartBaseController {
|
||||||
|
|
||||||
if (!this.hasSubtotalTarget) return
|
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 () {
|
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.
|
* item is removed, it removes itself from the page and the storage.
|
||||||
*/
|
*/
|
||||||
async remove () {
|
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 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()) {
|
if (response.isFail()) {
|
||||||
this.handleFailure(response)
|
this.handleFailure(response)
|
||||||
|
@ -225,14 +237,18 @@ export default class extends CartBaseController {
|
||||||
* Recovers the order if something failed
|
* Recovers the order if something failed
|
||||||
*/
|
*/
|
||||||
async recover () {
|
async recover () {
|
||||||
|
console.error('Recuperando pedido', this.token)
|
||||||
|
|
||||||
// Removes the failing token
|
// Removes the failing token
|
||||||
this.storage.removeItem('token')
|
this.storage.removeItem('token')
|
||||||
|
|
||||||
|
// Get a new token and cart
|
||||||
|
await this.tokenGetOrCreate()
|
||||||
|
|
||||||
// Stores the previous cart
|
// Stores the previous cart
|
||||||
const cart = this.cart
|
const cart = this.cart
|
||||||
|
|
||||||
// Get a new token and cart
|
if (!cart) return
|
||||||
await this.tokenGetOrCreate()
|
|
||||||
|
|
||||||
// Add previous items and their quantities to the new cart by
|
// Add previous items and their quantities to the new cart by
|
||||||
// mimicking user's actions
|
// mimicking user's actions
|
||||||
|
@ -243,6 +259,8 @@ export default class extends CartBaseController {
|
||||||
|
|
||||||
const product = this.product
|
const product = this.product
|
||||||
|
|
||||||
|
if (!product) continue
|
||||||
|
|
||||||
this.data.set('image', product.image)
|
this.data.set('image', product.image)
|
||||||
this.data.set('title', product.title)
|
this.data.set('title', product.title)
|
||||||
this.data.set('extra', product.extra.join('|'))
|
this.data.set('extra', product.extra.join('|'))
|
||||||
|
|
Loading…
Reference in a new issue