woocommerce-paypal-payments/modules.local/ppcp-button/resources/js/modules/SingleProductConfig.js

127 lines
3.8 KiB
JavaScript
Raw Normal View History

2020-04-02 08:38:00 +03:00
import ButtonsToggleListener from "./ButtonsToggleListener";
import Product from "./Product";
2020-04-02 08:38:00 +03:00
class SingleProductConfig {
constructor(
config,
updateCart,
showButtonCallback,
hideButtonCallback,
formElement,
errorHandler
) {
this.config = config;
this.updateCart = updateCart;
this.showButtonCallback = showButtonCallback;
this.hideButtonCallback = hideButtonCallback;
this.formElement = formElement;
this.errorHandler = errorHandler;
}
2020-04-06 11:16:18 +03:00
configuration()
{
2020-04-02 08:38:00 +03:00
if ( this.hasVariations() ) {
const observer = new ButtonsToggleListener(
this.formElement.querySelector('.single_add_to_cart_button'),
this.showButtonCallback,
this.hideButtonCallback
);
observer.init();
}
const onApprove = (data, actions) => {
return actions.redirect(this.config.redirect);
}
return {
createOrder: this.createOrder(),
onApprove,
onError: (error) => {
this.errorHandler.message(error);
}
}
}
2020-04-06 11:16:18 +03:00
createOrder()
{
2020-04-08 11:23:14 +03:00
var getProducts = null;
if (! this.isGroupedProduct() ) {
2020-04-08 11:23:14 +03:00
getProducts = () => {
const id = document.querySelector('[name="add-to-cart"]').value;
const qty = document.querySelector('[name="quantity"]').value;
const variations = this.variations();
2020-04-08 11:23:14 +03:00
return [new Product(id, qty, variations)];
}
} else {
getProducts = () => {
const products = [];
this.formElement.querySelectorAll('input[type="number"]').forEach((element) => {
if (! element.value) {
return;
}
const elementName = element.getAttribute('name').match(/quantity\[([\d]*)\]/);
if (elementName.length !== 2) {
return;
}
const id = parseInt(elementName[1]);
const quantity = parseInt(element.value);
products.push(new Product(id, quantity, null));
})
return products;
}
}
const createOrder = (data, actions) => {
this.errorHandler.clear();
2020-04-02 08:38:00 +03:00
2020-04-08 11:23:14 +03:00
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;
});
};
2020-04-08 11:23:14 +03:00
const promise = this.updateCart.update(onResolve, getProducts());
return promise;
};
2020-04-02 08:38:00 +03:00
return createOrder;
}
2020-04-06 11:16:18 +03:00
variations()
{
2020-04-02 08:38:00 +03:00
if (! this.hasVariations()) {
return null;
}
const attributes = [...this.formElement.querySelectorAll("[name^='attribute_']")].map(
(element) => {
2020-04-06 11:16:18 +03:00
return {
2020-04-02 08:38:00 +03:00
value:element.value,
name:element.name
}
}
);
return attributes;
}
2020-04-06 11:16:18 +03:00
hasVariations()
{
2020-04-02 08:38:00 +03:00
return this.formElement.classList.contains('variations_form');
}
isGroupedProduct()
{
2020-04-08 11:23:14 +03:00
return this.formElement.classList.contains('grouped_form');
}
2020-04-02 08:38:00 +03:00
}
export default SingleProductConfig;