如何将 WooCommerce 购物车中的物品发送到 CF7 表单邮件中

huangapple go评论56阅读模式
英文:

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()-&gt;cart-&gt;get_cart();
   $cart_text = &#39;&#39;;
   $cart_total = 0;
   foreach ($cart_contents as $cart_item_key =&gt; $cart_item) {
       $product = $cart_item[&#39;data&#39;];
       $quantity = $cart_item[&#39;quantity&#39;];
       $sum = $cart_item[&#39;line_total&#39;];
       $cart_total += $sum;
       $product_name = $product-&gt;get_name();
       $cart_text .= &quot;Product: $product_name x $quantity Subtotal: $sum \n&quot;;
   }
   $cart_text .= &quot;---\n&quot;;
   $cart_text .= &quot;Total: $cart_total&quot;;
   return $cart_text;
}

 /* create a new tag for CF7 */

 function cf7_add_cart_content_tag() {
   wpcf7_add_form_tag(&#39;cart_content&#39;, &#39;cf7_cart_content_handler&#39;);
}

function cf7_cart_content_handler($tag) {
   $cart_content_text = get_cart_content_as_text();
   return &#39;&lt;textarea name=&quot;cart_content&quot; readonly&gt;&#39; . esc_textarea($cart_content_text) . 
 &#39;&lt;/textarea&gt;&#39;;
}

add_action(&#39;wpcf7_init&#39;, &#39;cf7_add_cart_content_tag&#39;);

Then use new tag [cart_content] in your form

huangapple
  • 本文由 发表于 2023年5月11日 04:15:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222251.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定