mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 09:08:09 +08:00
Add support for custom single product page fields
This commit is contained in:
parent
02de8ada7f
commit
7baee26194
5 changed files with 50 additions and 12 deletions
|
@ -73,7 +73,7 @@ class SingleProductActionHandler {
|
|||
getSubscriptionProducts()
|
||||
{
|
||||
const id = document.querySelector('[name="add-to-cart"]').value;
|
||||
return [new Product(id, 1, null)];
|
||||
return [new Product(id, 1, null, this.extraFields())];
|
||||
}
|
||||
|
||||
configuration()
|
||||
|
@ -107,7 +107,7 @@ class SingleProductActionHandler {
|
|||
{
|
||||
if ( this.isBookingProduct() ) {
|
||||
const id = document.querySelector('[name="add-to-cart"]').value;
|
||||
return [new BookingProduct(id, 1, FormHelper.getPrefixedFields(this.formElement, "wc_bookings_field"))];
|
||||
return [new BookingProduct(id, 1, FormHelper.getPrefixedFields(this.formElement, "wc_bookings_field"), this.extraFields())];
|
||||
} else if ( this.isGroupedProduct() ) {
|
||||
const products = [];
|
||||
this.formElement.querySelectorAll('input[type="number"]').forEach((element) => {
|
||||
|
@ -120,17 +120,25 @@ class SingleProductActionHandler {
|
|||
}
|
||||
const id = parseInt(elementName[1]);
|
||||
const quantity = parseInt(element.value);
|
||||
products.push(new Product(id, quantity, null));
|
||||
products.push(new Product(id, quantity, null, this.extraFields()));
|
||||
})
|
||||
return products;
|
||||
} else {
|
||||
const id = document.querySelector('[name="add-to-cart"]').value;
|
||||
const qty = document.querySelector('[name="quantity"]').value;
|
||||
const variations = this.variations();
|
||||
return [new Product(id, qty, variations)];
|
||||
return [new Product(id, qty, variations, this.extraFields())];
|
||||
}
|
||||
}
|
||||
|
||||
extraFields() {
|
||||
return FormHelper.getFilteredFields(
|
||||
this.formElement,
|
||||
['add-to-cart', 'quantity', 'product_id', 'variation_id'],
|
||||
['attribute_', 'wc_bookings_field']
|
||||
);
|
||||
}
|
||||
|
||||
createOrder()
|
||||
{
|
||||
this.cartHelper = null;
|
||||
|
|
|
@ -2,15 +2,17 @@ import Product from "./Product";
|
|||
|
||||
class BookingProduct extends Product {
|
||||
|
||||
constructor(id, quantity, booking) {
|
||||
constructor(id, quantity, booking, extra) {
|
||||
super(id, quantity, null);
|
||||
this.booking = booking;
|
||||
this.extra = extra;
|
||||
}
|
||||
|
||||
data() {
|
||||
return {
|
||||
...super.data(),
|
||||
booking: this.booking
|
||||
booking: this.booking,
|
||||
extra: this.extra
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,18 +1,19 @@
|
|||
class Product {
|
||||
|
||||
constructor(id, quantity, variations) {
|
||||
constructor(id, quantity, variations, extra) {
|
||||
this.id = id;
|
||||
this.quantity = quantity;
|
||||
this.variations = variations;
|
||||
this.extra = extra;
|
||||
}
|
||||
|
||||
data() {
|
||||
return {
|
||||
id:this.id,
|
||||
quantity:this.quantity,
|
||||
variations:this.variations
|
||||
quantity: this.quantity,
|
||||
variations: this.variations,
|
||||
extra: this.extra,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default Product;
|
||||
export default Product;
|
||||
|
|
|
@ -7,11 +7,29 @@ export default class FormHelper {
|
|||
static getPrefixedFields(formElement, prefix) {
|
||||
let fields = {};
|
||||
for(const element of formElement.elements) {
|
||||
if( element.name.startsWith(prefix) ) {
|
||||
if( ( ! prefix ) || element.name.startsWith(prefix) ) {
|
||||
fields[element.name] = element.value;
|
||||
}
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
|
||||
static getFilteredFields(formElement, exactFilters, prefixFilters) {
|
||||
let fields = {};
|
||||
|
||||
for(const element of formElement.elements) {
|
||||
if (!element.name) {
|
||||
continue;
|
||||
}
|
||||
if (exactFilters && (exactFilters.indexOf(element.name) !== -1)) {
|
||||
continue;
|
||||
}
|
||||
if (prefixFilters && prefixFilters.some(prefixFilter => element.name.startsWith(prefixFilter))) {
|
||||
continue;
|
||||
}
|
||||
fields[element.name] = element.value;
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue