如何让Contact Form 7邮件显示与表单一致的WooCommerce购物车项
优化Contact Form 7邮件中WooCommerce购物车项的展示格式
我已编写代码为Contact Form 7创建自定义标签,可在表单中展示WooCommerce购物车商品(含商品图片、名称、价格等),并将购物车项发送至邮件通知。但目前邮件仅能显示单调的文本商品列表,无法实现与表单一致的展示格式;此外我还实现了不含税及运费的购物车总价自定义标签,希望优化邮件中购物车项的展示效果,使其匹配表单样式。
现有代码
// add "cart" tag in Contact Form 7 add_action('wpcf7_init', 'cartTag'); function cartTag() { wpcf7_add_form_tag('cart', 'cartItems', true); } // loop for cart items in "cart" tag Contact Form 7 function cartItems($tag) { if (is_null(WC()->cart)) { wc_load_cart(); } $name = $tag['name']; if (empty($name)) { return ''; } $items = WC()->cart->get_cart(); $output = '<div class="my-cf7-cart">'; $inputValue = ""; $cart = [ 'total' => WC()->cart->total, 'products' => [] ]; foreach ($items as $item => $values) { $_product = wc_get_product($values['data']->get_id()); $item_image = $_product->get_image(); $price = wc_get_price_excluding_tax($_product); $price = $values['line_total']; $price = number_format($price, 2, ",", "."); $output .= '<div class="my-acf-cart-item"><a href="' . $_product->get_permalink() . '" target="_blank" rel="nofollow">' . $item_image; $output .= ' <p class="prev-product-title">' . $_product->get_title() . '</p></a>'; $output .= '<p>Quantity: '; $output .= $values['quantity'] . '</p>'; $output .= '<p>Price: € ' . $price . ' exc. tax</p></div>'; // for email (CF7) $inputValue .= 'Product: ' . $_product->get_title() . ' • Quantity: ' . $values['quantity'] . ' • Price: € ' . $price . ' exc. tax 
'; $cart['products'][] = [ 'name' => $_product->get_title(), 'qty' => $values['quantity'], 'price' => $price, 'link' => $_product->get_permalink() ]; } // set total cart without shipping amount $total = WC()->cart->subtotal_ex_tax; $shipping = WC()->cart->shipping_total; $total = number_format($total, 2, ",", "."); $output .= '<p class="prev-total"><b>Products total:</b> € ' . $total . ' exc. tax</p>'; $output .= '<input type="hidden" name="' . $name . '" value="' . $inputValue . '" />'; $output .= '<input type="hidden" name="products" value="' . htmlspecialchars(json_encode($cart)) . '" />'; $output .= '<input type="hidden" name="shipping" value="' . $_GET['shipping_method'] . '" />'; $output .= '</div>'; return $output; } // add "total" tag in Contact Form 7 add_action('wpcf7_init', 'totalTag'); function totalTag() { wpcf7_add_form_tag('total', 'totalItem', true); } // Total exc. tax in "total" tag Contact Form 7 function totalItem($tag) { if (is_null(WC()->cart)) { wc_load_cart(); } $name = $tag['name']; if (empty($name)) { return ''; } $output = '<div class="total-cart-excTax">'; $total = WC()->cart->subtotal_ex_tax; $total = number_format($total, 2, ",", "."); $inputValue = '€ ' . $total . ' exc. tax'; $output .= '<input type="hidden" name="' . $name . '" value="' . $inputValue . '" />'; $output .= '</div>'; return $output; }
已尝试的修改
我将代码中的$item_image = $values['data']->get_image(array(0, 30));替换为$item_image = $_product->get_image();,删除了缩略图设置以避免图片裁剪,代码更简洁但未解决核心问题。
需要优化的代码段
希望优化以下部分,实现邮件中购物车项的格式化展示,匹配前端表单的结构:
// for email (CF7) $inputValue .= 'Product: ' . $_product->get_title() . ' • Quantity: ' . $values['quantity'] . ' • Price: € ' . $price . ' exc. tax 
'; $cart['products'][] = [ 'name' => $_product->get_title(), 'qty' => $values['quantity'], 'price' => $price, 'link' => $_product->get_permalink() ];
内容的提问来源于stack exchange,提问作者Captain Krik
相关产品推荐
相关产品推荐

