Настройка Woocommerce электронной почты - PullRequest
0 голосов
/ 29 марта 2020

Я экспериментирую с электронными письмами Woocommerce. Вот почему я задаю свой вопрос здесь, чтобы поучиться у него.

Я пытаюсь добавить таблицу в электронное письмо Woocommerce. Добавление таблицы выполнено успешно, только я хочу отобразить заказанные товары с указанием названия, количества и цены. Когда я пробую фрагмент кода ниже, я получаю пустую таблицу, как показано на рисунке. Таким образом, вопрос заключается в том, есть ли возможность, что я могу добавить в электронное письмо таблицу с приобретенными продуктами?

Изображение (вывод): enter image description here

Код в функциях. php:

 add_action('woocommerce_email_order_details', 'action_after_email_order_details', 25, 4 );
    function action_after_email_order_details( $order, $sent_to_admin, $plain_text, $email )
    {
    $event = get_post_meta( $order->get_id(), '_billing_company', true );

        // The HTML Structure
        $html_output = '<h2>' . __('Attendee Info') . '</h2>
        <div class="discount-info">
            <table cellspacing="0" cellpadding="6"><tbody>';


     $order = new WC_Order( $order_id );
    foreach ($order->get_items() as $item_key => $item ):
    $item_data    = $item->get_data();

        $product_name = $item_data['name'];
        $product_id   = $item_data['product_id'];
        $variation_id = $item_data['variation_id'];
        $quantity     = $item_data['quantity'];
        $tax_class    = $item_data['tax_class'];
        $line_subtotal     = $item_data['subtotal'];
        $line_subtotal_tax = $item_data['subtotal_tax'];
        $line_total        = $item_data['total'];
        $line_total_tax    = $item_data['total_tax'];

        $html_output .= '<tr>
            <th>' . $product_name . '</th>
            <td>' . $quantity . '</td>
            <td>' . $line_total . '</td>
        </tr>';


        endforeach;
        $html_output .= '</tbody></table>
        </div><br>'; // HTML (end)

        // The CSS styling
        $styles = '<style>
            .discount-info table{width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;
                color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}
            .discount-info table th, table.tracking-info td{text-align: left; border-top-width: 4px;
                color: #737373; border: 1px solid #e4e4e4; padding: 12px; width:58%;}
            .discount-info table td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;}
        </style>';

        // The Output CSS + HTML
        echo $styles . $html_output;
    }

1 Ответ

0 голосов
/ 29 марта 2020

$order является одним из параметров, поэтому у вас уже есть доступ к нему!

Оттуда у вас есть доступ к:

WC_Product_Simple Object

$product, содержит такие свойства, как (имя, вес, пуля, ...)

https://docs.woocommerce.com/wc-apidocs/class-WC_Product.html


WC_Order_Item_Product Объект

$item, содержит такие свойства, как (количество, всего, ...)

https://docs.woocommerce.com/wc-apidocs/class-WC_Order_Item_Product.html

Некоторые свойства дают тот же результат, там вы можете выбрать.

Итак, вы получите ... и тогда это вопрос добавления html метки в нужном месте

function action_after_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
    foreach ($order->get_items() as $item_key => $item ) {
        // WC_Product_Simple Object
        // https://docs.woocommerce.com/wc-apidocs/class-WC_Product.html

        // WC_Order_Item_Product Object
        // https://docs.woocommerce.com/wc-apidocs/class-WC_Order_Item_Product.html

        // The WC_Product object
        $product = $item->get_product();    

        // Get product name
        $product_name = $product->get_name();

        // Get product id
        $product_id1 = $product->get_id();

        // Get product id
        $product_id2 = $item->get_product_id();

        // Get variation id
        $product_id3 = $item->get_variation_id(); 

        // The quantity
        $quantity = $item->get_quantity();

        echo 'name = ' . $product_name . '<br>';
        echo 'product id 1 = ' . $product_id1 . '<br>';
        echo 'product id 2 = ' . $product_id2 . '<br>';
        echo 'product id 3 = ' . $product_id3 . '<br>';
        echo 'quantity = ' . $quantity . '<br>';
    }
}
add_action('woocommerce_email_order_details', 'action_after_email_order_details', 10, 4 );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...