I have this code to create a custom tag for display woocommerce cart items in cf7 form and send cart items in email notifications. But in Email I can't figure out how show cart items like they are displayed in the form: with image product, name product, price ect. In email I was only able to show products list in a boring and no acceptable way. As extra, I also create a custom tag to display total cart excluding tax and without shipping amount (because is a quote request). This is my working code.
// 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 = $values['data']->get_image(array(0, 30));
$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;
}
cart items list in my form
cart items list in my email
Thanks for any help
As suggested by Snuffy, I replaced this line of code:
$item_image = $values['data']->get_image(array(0, 30));
in:
$item_image = $_product->get_image();
I delated 'Thumbnail' because I don't like to have cropped product image. It's a better way to write clean code, although the result doesn't change and that doesn't answer my question.
The portion of code I would like to implement is the following (complete code above):
// 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()
];
The problem I like to fix is transform the input string in a kind of array, as in front end form appear built with this function (complete code in my question):
// 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 = $values['data']->get_image(array(0, 30));
$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>';
//...
}
Same question not solved here: Contact form 7 custom tag not send
Any help is greatly appreciated. Thanks