英文:
How to send a woocommerce cart items in CF7 form email
问题
我需要通过Contact Form 7插件将Woocommerce购物车中的物品发送到电子邮件中。
我想在我的结账页面上只包含电话字段的快速订单表单,但也要将购物车详情发送到电子邮件中。
英文:
I need to send Woocommerce cart items in the email by Contact Form 7 plugin.
I want to make a quick order form on my checkout page only with phone field
but send the cart details in email as well.
答案1
得分: 1
以下是翻译好的部分:
/* 将此代码放入您的 functions.php 文件中 */
/* 作为文本获取购物车详情 */
function get_cart_content_as_text() {
$cart_contents = WC()->cart->get_cart();
$cart_text = '';
$cart_total = 0;
foreach ($cart_contents as $cart_item_key => $cart_item) {
$product = $cart_item['data'];
$quantity = $cart_item['quantity'];
$sum = $cart_item['line_total'];
$cart_total += $sum;
$product_name = $product->get_name();
$cart_text .= "Product: $product_name x $quantity Subtotal: $sum \n";
}
$cart_text .= "---\n";
$cart_text .= "Total: $cart_total";
return $cart_text;
}
/* 为 CF7 创建一个新标签 */
function cf7_add_cart_content_tag() {
wpcf7_add_form_tag('cart_content', 'cf7_cart_content_handler');
}
function cf7_cart_content_handler($tag) {
$cart_content_text = get_cart_content_as_text();
return '<textarea name="cart_content" readonly>' . esc_textarea($cart_content_text) .
'</textarea>';
}
add_action('wpcf7_init', 'cf7_add_cart_content_tag');
然后在您的表单中使用新标签 [cart_content]
英文:
Here is a solution:
Put this code into your functions.php file
/* Get a cart details as a text */
function get_cart_content_as_text() {
$cart_contents = WC()->cart->get_cart();
$cart_text = '';
$cart_total = 0;
foreach ($cart_contents as $cart_item_key => $cart_item) {
$product = $cart_item['data'];
$quantity = $cart_item['quantity'];
$sum = $cart_item['line_total'];
$cart_total += $sum;
$product_name = $product->get_name();
$cart_text .= "Product: $product_name x $quantity Subtotal: $sum \n";
}
$cart_text .= "---\n";
$cart_text .= "Total: $cart_total";
return $cart_text;
}
/* create a new tag for CF7 */
function cf7_add_cart_content_tag() {
wpcf7_add_form_tag('cart_content', 'cf7_cart_content_handler');
}
function cf7_cart_content_handler($tag) {
$cart_content_text = get_cart_content_as_text();
return '<textarea name="cart_content" readonly>' . esc_textarea($cart_content_text) .
'</textarea>';
}
add_action('wpcf7_init', 'cf7_add_cart_content_tag');
Then use new tag [cart_content] in your form
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论