我今天一直在把头撞在桌子上。我试图让人们直接使用mailto链接通过电子邮件(使用购物车)下订单,而不是处理表单/ API。 我的数据如下:
[
{
"image": "https://static.wixstatic.com/awefawef.jpg",
"name": "xyz",
"price": "20.00",
"id": "3",
"quantity": 30
},
{
"image": "https://static.wixstatic.com/media/aweawfefeaw.png",
"name": "abc",
"price": "20.00",
"id": "4",
"quantity": 20
}
]
尝试使电子邮件成为具有名称,价格和数量的表格,或者只是某种简单的列表/发票,例如:
所需物品:
- name: abc
quantity:30
price: $600
- name: xyz
quantity: 20
price: $400
- Total Price: $1000
customer name: [insert name]
customer email: [insert email]
customer company: [insert company]
我正在努力以某种可用形式为mailto链接中的body
解析数据。这是我到目前为止(在React中)所得到的...购物车数组是this.state.cart
:
sendEmail(){
let body;
this.state.cart.forEach(function(element) {
//console.log(element);
body = 'name:' + element.name;
body += '\nprice:' + element.price;
body += '\nquanity:' + element.quantity;
body += '\ntotal:' + element.quantity * element.price;
});
window.location.href="mailto:sales@email.com?subject= Order for [Insert Name]&body=" + encodeURIComponent(body)
}
这类作品,但我不知道如何以有序方式获得所有作品。只有最后一个有效。它还会在本地Mac邮件应用程序中打开。如您所知,我有点转身。
答案 0 :(得分:1)
这能达到预期的效果吗?
function sendEmail() {
// Format a string itemising cart by mapping elements to sub-strings and joining the result
const items = this.state.cart.map(function(element) {
return `
- name ${ element.name }
price: $${ element.price.toFixed(2) }
quantity: ${ element.quantity }
total: ${ (element.quantity * element.price).toFixed(2) }
`;
}).join('\n');
// Calculate total price via reduction, and format to a number to 2dp
const totalPrice = this.state.cart.reduce(function(sum, element) {
return sum + (element.quantity * element.price);
}, 0.0).toFixed(2);
// Format body string with itemised cart, total price, etc
const body = `
${ items }
Total Price: $${totalPrice}
customer name: [insert name]
customer email: [insert email]
customer company: [insert company]
`;
window.location.href = 'mailto:sales@email.com?subject=Order for [Insert Name]&body=' + encodeURIComponent(body);
}