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

@ -1,449 +1,2 @@
/******/ (function (modules) {
// webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId)
{
/******/
/******/ // Check if module is in cache
/******/ if (installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function (exports, name, getter) {
/******/ if (!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function (exports) {
/******/ if (typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function (value, mode) {
/******/ if (mode & 1) {
value = __webpack_require__(value);
}
/******/ if (mode & 8) {
return value;
}
/******/ if ((mode & 4) && typeof value === 'object' && value && value.__esModule) {
return value;
}
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if (mode & 2 && typeof value != 'string') {
for (var key in value) {
__webpack_require__.d(ns, key, function (key) {
return value[key]; }.bind(null, key));
}
}
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function (module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault()
{
return module['default']; } :
/******/ function getModuleExports()
{
return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function (object, property) {
return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = "./resources/js/button.js");
/******/ })({
/***/ "./resources/js/button.js":
/*!********************************!*\
!*** ./resources/js/button.js ***!
\********************************/
/*! no exports provided */
/***/ (function (module, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _modules_Renderer__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./modules/Renderer */ "./resources/js/modules/Renderer.js");
/* harmony import */ var _modules_SingleProductConfig__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./modules/SingleProductConfig */ "./resources/js/modules/SingleProductConfig.js");
/* harmony import */ var _modules_UpdateCart__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./modules/UpdateCart */ "./resources/js/modules/UpdateCart.js");
/* harmony import */ var _modules_ErrorHandler__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./modules/ErrorHandler */ "./resources/js/modules/ErrorHandler.js");
document.addEventListener('DOMContentLoaded', () => {
if (!typeof PayPalCommerceGateway) {
console.error('PayPal button could not be configured.');
return;
}
if (!document.querySelector(PayPalCommerceGateway.button.wrapper)) {
console.error('No wrapper for PayPal button found.');
return;
}
const context = PayPalCommerceGateway.context;
if (context === 'product' && !document.querySelector('form.cart')) {
return;
}
const errorHandler = new _modules_ErrorHandler__WEBPACK_IMPORTED_MODULE_3__["default"]();
const renderer = new _modules_Renderer__WEBPACK_IMPORTED_MODULE_0__["default"]({
url: PayPalCommerceGateway.button.url,
wrapper: PayPalCommerceGateway.button.wrapper
});
const updateCart = new _modules_UpdateCart__WEBPACK_IMPORTED_MODULE_2__["default"](PayPalCommerceGateway.ajax.change_cart.endpoint, PayPalCommerceGateway.ajax.change_cart.nonce);
let configurator = null;
if (context === 'product') {
configurator = new _modules_SingleProductConfig__WEBPACK_IMPORTED_MODULE_1__["default"](PayPalCommerceGateway, updateCart, renderer.showButtons.bind(renderer), renderer.hideButtons.bind(renderer), document.querySelector('form.cart'), errorHandler);
}
if (!configurator) {
console.error('No context for button found.');
return;
}
renderer.render(configurator.configuration());
});
/***/ }),
/***/ "./resources/js/modules/ButtonsToggleListener.js":
/*!*******************************************************!*\
!*** ./resources/js/modules/ButtonsToggleListener.js ***!
\*******************************************************/
/*! exports provided: default */
/***/ (function (module, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/**
* When you can't add something to the cart, the PayPal buttons should not show.
* Therefore we listen for changes on the add to cart button and show/hide the buttons accordingly.
*/
class ButtonsToggleListener {
constructor(element, showCallback, hideCallback)
{
this.element = element;
this.showCallback = showCallback;
this.hideCallback = hideCallback;
this.observer = null;
}
init()
{
const config = { attributes: true };
const callback = () => {
if (this.element.classList.contains('disabled')) {
this.hideCallback();
return;
}
this.showCallback();
};
this.observer = new MutationObserver(callback);
this.observer.observe(this.element, config);
}
disconnect()
{
this.observer.disconnect();
}
}
/* harmony default export */ __webpack_exports__["default"] = (ButtonsToggleListener);
/***/ }),
/***/ "./resources/js/modules/ErrorHandler.js":
/*!**********************************************!*\
!*** ./resources/js/modules/ErrorHandler.js ***!
\**********************************************/
/*! exports provided: default */
/***/ (function (module, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
class ErrorHandler {
constructor()
{
this.wrapper = document.querySelector('.woocommerce-notices-wrapper');
}
message(text)
{
this.wrapper.classList.add('woocommerce-error');
this.wrapper.innerText = this.sanitize(text);
}
sanitize(text)
{
const textarea = document.createElement('textarea');
textarea.innerHTML = text;
return textarea.value;
}
clear()
{
if (!this.wrapper.classList.contains('woocommerce-error')) {
return;
}
this.wrapper.classList.remove('woocommerce-error');
this.wrapper.innerText = '';
}
}
/* harmony default export */ __webpack_exports__["default"] = (ErrorHandler);
/***/ }),
/***/ "./resources/js/modules/Renderer.js":
/*!******************************************!*\
!*** ./resources/js/modules/Renderer.js ***!
\******************************************/
/*! exports provided: default */
/***/ (function (module, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
class Renderer {
constructor(config)
{
this.config = config;
}
render(buttonConfig)
{
const script = document.createElement('script');
if (typeof paypal !== 'object') {
script.setAttribute('src', this.config.url);
script.addEventListener('load', event => {
this.renderButtons(buttonConfig);
});
document.body.append(script);
return;
}
this.renderButtons(buttonConfig);
}
renderButtons(buttonConfig)
{
paypal.Buttons(buttonConfig).render(this.config.wrapper);
}
hideButtons()
{
document.querySelector(this.config.wrapper).style.display = 'none';
}
showButtons()
{
document.querySelector(this.config.wrapper).style.display = 'block';
}
}
/* harmony default export */ __webpack_exports__["default"] = (Renderer);
/***/ }),
/***/ "./resources/js/modules/SingleProductConfig.js":
/*!*****************************************************!*\
!*** ./resources/js/modules/SingleProductConfig.js ***!
\*****************************************************/
/*! exports provided: default */
/***/ (function (module, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _ButtonsToggleListener__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./ButtonsToggleListener */ "./resources/js/modules/ButtonsToggleListener.js");
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;
}
configuration()
{
if (this.hasVariations()) {
const observer = new _ButtonsToggleListener__WEBPACK_IMPORTED_MODULE_0__["default"](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);
}
};
}
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();
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, qty, variations);
return promise;
};
return createOrder;
}
variations()
{
if (!this.hasVariations()) {
return null;
}
const attributes = [...this.formElement.querySelectorAll("[name^='attribute_']")].map(element => {
return {
value: element.value,
name: element.name
};
});
return attributes;
}
hasVariations()
{
return this.formElement.classList.contains('variations_form');
}
}
/* harmony default export */ __webpack_exports__["default"] = (SingleProductConfig);
/***/ }),
/***/ "./resources/js/modules/UpdateCart.js":
/*!********************************************!*\
!*** ./resources/js/modules/UpdateCart.js ***!
\********************************************/
/*! exports provided: default */
/***/ (function (module, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
class UpdateCart {
constructor(endpoint, nonce)
{
this.endpoint = endpoint;
this.nonce = nonce;
}
update(onResolve, product, qty, variations)
{
return new Promise((resolve, reject) => {
fetch(this.endpoint, {
method: 'POST',
body: JSON.stringify({
nonce: this.nonce,
product,
qty,
variations
})
}).then(result => {
return result.json();
}).then(result => {
if (!result.success) {
reject(result.data);
return;
}
const resolved = onResolve(result.data);
resolve(resolved);
});
});
}
}
/* harmony default export */ __webpack_exports__["default"] = (UpdateCart);
/***/ })
/******/ });
!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t,r){"use strict";r.r(t);var n=class{constructor(e){this.config=e}render(e){const t=document.createElement("script");if("object"!=typeof paypal)return t.setAttribute("src",this.config.url),t.addEventListener("load",t=>{this.renderButtons(e)}),void document.body.append(t);this.renderButtons(e)}renderButtons(e){paypal.Buttons(e).render(this.config.wrapper)}hideButtons(){document.querySelector(this.config.wrapper).style.display="none"}showButtons(){document.querySelector(this.config.wrapper).style.display="block"}};var o=class{constructor(e,t,r){this.element=e,this.showCallback=t,this.hideCallback=r,this.observer=null}init(){this.observer=new MutationObserver(()=>{this.element.classList.contains("disabled")?this.hideCallback():this.showCallback()}),this.observer.observe(this.element,{attributes:!0})}disconnect(){this.observer.disconnect()}};var a=class{constructor(e,t,r){this.id=e,this.quantity=t,this.variations=r}data(){return{id:this.id,quantity:this.quantity,variations:this.variations}}};var i=class{constructor(e,t,r,n,o,a){this.config=e,this.updateCart=t,this.showButtonCallback=r,this.hideButtonCallback=n,this.formElement=o,this.errorHandler=a}configuration(){if(this.hasVariations()){new o(this.formElement.querySelector(".single_add_to_cart_button"),this.showButtonCallback,this.hideButtonCallback).init()}return{createOrder:this.createOrder(),onApprove:(e,t)=>t.redirect(this.config.redirect),onError:e=>{this.errorHandler.message(e)}}}createOrder(){return this.isGroupedProduct()?createOrder:(e,t)=>{this.errorHandler.clear();const r=document.querySelector('[name="add-to-cart"]').value,n=document.querySelector('[name="quantity"]').value,o=this.variations(),i=new a(r,n,o);return this.updateCart.update(e=>fetch(this.config.ajax.create_order.endpoint,{method:"POST",body:JSON.stringify({nonce:this.config.ajax.create_order.nonce,purchase_units:e})}).then((function(e){return e.json()})).then((function(e){if(e.success)return e.data.id})),[i])}}variations(){if(!this.hasVariations())return null;return[...this.formElement.querySelectorAll("[name^='attribute_']")].map(e=>({value:e.value,name:e.name}))}hasVariations(){return this.formElement.classList.contains("variations_form")}isGroupedProduct(){return null!==this.formElement.querySelector(".woocommerce-grouped-product-list")}};var s=class{constructor(e,t){this.endpoint=e,this.nonce=t}update(e,t){return new Promise((r,n)=>{fetch(this.endpoint,{method:"POST",body:JSON.stringify({nonce:this.nonce,products:t})}).then(e=>e.json()).then(t=>{if(!t.success)return void n(t.data);const o=e(t.data);r(o)})})}};var c=class{constructor(){this.wrapper=document.querySelector(".woocommerce-notices-wrapper")}message(e){this.wrapper.classList.add("woocommerce-error"),this.wrapper.innerText=this.sanitize(e)}sanitize(e){const t=document.createElement("textarea");return t.innerHTML=e,t.value}clear(){this.wrapper.classList.contains("woocommerce-error")&&(this.wrapper.classList.remove("woocommerce-error"),this.wrapper.innerText="")}};document.addEventListener("DOMContentLoaded",()=>{if(!document.querySelector(PayPalCommerceGateway.button.wrapper))return void console.error("No wrapper for PayPal button found.");const e=PayPalCommerceGateway.context;if("product"===e&&!document.querySelector("form.cart"))return;const t=new c,r=new n({url:PayPalCommerceGateway.button.url,wrapper:PayPalCommerceGateway.button.wrapper}),o=new s(PayPalCommerceGateway.ajax.change_cart.endpoint,PayPalCommerceGateway.ajax.change_cart.nonce);let a=null;"product"===e&&(a=new i(PayPalCommerceGateway,o,r.showButtons.bind(r),r.hideButtons.bind(r),document.querySelector("form.cart"),t)),a?r.render(a.configuration()):console.error("No context for button found.")})}]);
//# sourceMappingURL=button.js.map

File diff suppressed because one or more lines are too long

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(

View file

@ -40,8 +40,8 @@ class ChangeCartEndpoint implements EndpointInterface
try {
$data = $this->requestData->readRequest($this->nonce());
if (! isset($data['product'])
|| ! isset($data['qty'])
if (! isset($data['products'])
|| ! is_array($data['products'])
) {
wp_send_json_error(
__(
@ -51,23 +51,45 @@ class ChangeCartEndpoint implements EndpointInterface
);
return false;
}
$product = wc_get_product((int) $data['product']);
if (! $product) {
wp_send_json_error(
__(
'No product defined. Action aborted.',
'woocommerce-paypal-commerce-gateway'
)
);
return false;
$products = [];
foreach ($data['products'] as $product) {
if (! isset($product['quantity']) || ! isset($product['id'])) {
wp_send_json_error(
__(
'Necessary fields not defined. Action aborted.',
'woocommerce-paypal-commerce-gateway'
)
);
return false;
}
$wcProduct = wc_get_product((int) $product['id']);
if (! $wcProduct) {
wp_send_json_error(
__(
'Product not found. Action aborted.',
'woocommerce-paypal-commerce-gateway'
)
);
return false;
}
$products[] = [
'product' => $wcProduct,
'quantity' => (int) $product['quantity'],
'variations' => isset($product['variations']) ? $product['variations'] : null,
];
}
$quantity = (int) $data['qty'];
$this->shipping->reset_shipping();
$this->cart->empty_cart(false);
$success = (! $product->is_type('variable')) ?
$success = $this->addProduct($product, $quantity)
: $this->addVariableProduct($product, $quantity, $data['variations']);
$success = true;
foreach ($products as $product) {
$success = $success && (! $product['product']->is_type('variable')) ?
$success = $this->addProduct($product['product'], $product['quantity'])
: $this->addVariableProduct($product['product'], $product['quantity'], $product['variations']);
}
if (! $success) {
$message = __('Something went wrong. Action aborted', 'woocommerce-paypal-commerce-gateway');
$errors = wc_get_notices('error');