prepare javascript and endpoint for group products

This commit is contained in:
David Remer 2020-04-07 13:44:34 +03:00
parent fa137d49b8
commit 71f6622941
6 changed files with 100 additions and 494 deletions

View file

@ -0,0 +1,18 @@
class Product {
constructor(id, quantity, variations) {
this.id = id;
this.quantity = quantity;
this.variations = variations;
}
data() {
return {
id:this.id,
quantity:this.quantity,
variations:this.variations
}
}
}
export default Product;

View file

@ -1,5 +1,5 @@
import ButtonsToggleListener from "./ButtonsToggleListener";
import Product from "./Product";
class SingleProductConfig {
constructor(
@ -43,33 +43,36 @@ class SingleProductConfig {
createOrder()
{
const createOrder = (data, actions) => {
this.errorHandler.clear();
const product = document.querySelector('[name="add-to-cart"]').value;
const qty = document.querySelector('[name="quantity"]').value;
const variations = this.variations();
if (! this.isGroupedProduct() ) {
return (data, actions) => {
this.errorHandler.clear();
const id = document.querySelector('[name="add-to-cart"]').value;
const qty = document.querySelector('[name="quantity"]').value;
const variations = this.variations();
const product = new Product(id, qty, variations);
const onResolve = (purchase_units) => {
return fetch(this.config.ajax.create_order.endpoint, {
method: 'POST',
body: JSON.stringify({
nonce:this.config.ajax.create_order.nonce,
purchase_units
})
}).then(function (res) {
return res.json();
}).then(function (data) {
if (! data.success) {
//Todo: Error handling
return;
}
return data.data.id;
});
const onResolve = (purchase_units) => {
return fetch(this.config.ajax.create_order.endpoint, {
method: 'POST',
body: JSON.stringify({
nonce: this.config.ajax.create_order.nonce,
purchase_units
})
}).then(function (res) {
return res.json();
}).then(function (data) {
if (!data.success) {
//Todo: Error handling
return;
}
return data.data.id;
});
};
const promise = this.updateCart.update(onResolve, [product]);
return promise;
};
const promise = this.updateCart.update(onResolve, product, qty, variations);
return promise;
};
}
return createOrder;
}
@ -94,6 +97,11 @@ class SingleProductConfig {
{
return this.formElement.classList.contains('variations_form');
}
isGroupedProduct()
{
return this.formElement.querySelector('.woocommerce-grouped-product-list') !== null;
}
}
export default SingleProductConfig;

View file

@ -1,3 +1,4 @@
import Product from "./Product";
class UpdateCart {
constructor(endpoint, nonce)
@ -6,7 +7,13 @@ class UpdateCart {
this.nonce = nonce;
}
update(onResolve, product, qty, variations)
/**
*
* @param onResolve
* @param {Product[]} products
* @returns {Promise<unknown>}
*/
update(onResolve, products)
{
return new Promise((resolve, reject) => {
fetch(
@ -15,9 +22,7 @@ class UpdateCart {
method: 'POST',
body: JSON.stringify({
nonce: this.nonce,
product,
qty,
variations
products,
})
}
).then(