mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
prepare javascript and endpoint for group products
This commit is contained in:
parent
fa137d49b8
commit
71f6622941
6 changed files with 100 additions and 494 deletions
18
modules.local/ppcp-button/resources/js/modules/Product.js
Normal file
18
modules.local/ppcp-button/resources/js/modules/Product.js
Normal 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;
|
|
@ -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;
|
|
@ -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(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue