Кроме того, я также создаю собственный тег для отображения общей суммы корзины без учета налога и суммы доставки (потому что это запрос ценового предложения).
Это мой рабочий код.
Код: Выделить всё
// 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 = '';
$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 .= '' . $item_image;
$output .= '
' . $_product->get_title() . '
';
$output .= '
Quantity: ';
$output .= $values['quantity'] . '
';
$output .= '
Price: € ' . $price . ' exc. tax
';
// 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 .= '
Products total:[/b] € ' . $total . ' exc. tax
';
$output .= '';
$output .= '';
$output .= '';
$output .= '';
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 = '';
$total = WC()->cart->subtotal_ex_tax;
$total = number_format($total, 2, ",", ".");
$inputValue = '€ ' . $total . ' exc. tax';
$output .= '';
$output .= '';
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()
];
Код: Выделить всё
// 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 = '';
$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 .= '' . $item_image;
$output .= '
' . $_product->get_title() . '
';
$output .= '
Quantity: ';
$output .= $values['quantity'] . '
';
$output .= '
Price: € ' . $price . ' exc. tax
';
//...
}
Любая помощь приветствуется. Спасибо
Подробнее здесь: https://stackoverflow.com/questions/786 ... act-form-7