mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 14:57:26 +08:00
show button on grouped products
This commit is contained in:
parent
5843809e2b
commit
cc829f9756
4 changed files with 48 additions and 29 deletions
|
@ -1,2 +1,2 @@
|
|||
!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.")})}]);
|
||||
!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(){var e=null;e=this.isGroupedProduct()?()=>{const e=[];return this.formElement.querySelectorAll('input[type="number"]').forEach(t=>{if(!t.value)return;const r=t.getAttribute("name").match(/quantity\[([\d]*)\]/);if(2!==r.length)return;const n=parseInt(r[1]),o=parseInt(t.value);e.push(new a(n,o,null))}),e}:()=>{const e=document.querySelector('[name="add-to-cart"]').value,t=document.querySelector('[name="quantity"]').value,r=this.variations();return[new a(e,t,r)]};return(t,r)=>{this.errorHandler.clear();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})),e())}}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 this.formElement.classList.contains("grouped_form")}};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
|
@ -43,36 +43,56 @@ class SingleProductConfig {
|
|||
|
||||
createOrder()
|
||||
{
|
||||
var getProducts = null;
|
||||
if (! this.isGroupedProduct() ) {
|
||||
return (data, actions) => {
|
||||
this.errorHandler.clear();
|
||||
getProducts = () => {
|
||||
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 promise = this.updateCart.update(onResolve, [product]);
|
||||
return promise;
|
||||
};
|
||||
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();
|
||||
|
||||
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, getProducts());
|
||||
return promise;
|
||||
};
|
||||
return createOrder;
|
||||
}
|
||||
|
||||
|
@ -100,7 +120,7 @@ class SingleProductConfig {
|
|||
|
||||
isGroupedProduct()
|
||||
{
|
||||
return this.formElement.querySelector('.woocommerce-grouped-product-list') !== null;
|
||||
return this.formElement.classList.contains('grouped_form');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -79,7 +79,6 @@ class ChangeCartEndpoint implements EndpointInterface
|
|||
'quantity' => (int) $product['quantity'],
|
||||
'variations' => isset($product['variations']) ? $product['variations'] : null,
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
$this->shipping->reset_shipping();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue