通过添加自定义 table 来自定义 WooCommerce 电子邮件

Customizing WooCommerce e-mail by adding a custom table

我正在试验 WooCommerce 电子邮件。这就是为什么我在这里问我的问题以从中学习。

我正在尝试向 WooCommerce 电子邮件添加 table。添加一个table成功,只是我要显示订购的产品名称,数量和价格。

当我尝试下面的代码时,我得到一个空 table。

代码在functions.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;
    }

所以问题是我是否可以在购买产品的电子邮件中添加 table?

$order是参数之一,所以你已经可以访问它了!

从那里您可以访问:

WC_Product_Simple Object

$product, contains properties such as (name, weight, slug, ...)

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


WC_Order_Item_Product Object

$item, contains properties such as (quantity, total, ...)

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 );