仅在电子邮件通知中按订单号显示产品标题

Display the product title by order number only in email notifications

我现在正在自定义我的 Woocommerce 电子邮件模板,现在我只能访问订单号,我想获得我的订单的价格和数量,但我还没有弄清楚如何。

<?php 

    // Getting the order object "$order"

$order = wc_get_order( $order_id );

// Getting the items in the order

$order_items = $order->get_items();

// Iterating through each item in the order

foreach ($order_items as $item_id => $item_data) {

    // Get the item quantity, something is wrong here..

    $item_quantity = $order->get_item_meta($item_id, '_qty', true);
    echo $item_quantity;
    // Get the price, doesn't work either..
    $item_total = $order->get_item_meta($item_id, '_line_total', true)


}

?>

问题是我无法获得我正在定制的订单确认电子邮件中可以显示的数量和价格,我目前 运行 woocommerce 3.2.5

因为你有 $order object,你可以这样获得产品标题:

<?php 
    // Loop through order items
    foreach($order->get_items() as $items){
        $product = $items->get_product(); // The product object
        $product_name = $product->get_name(); // The product Name
        $quantity = $items->get_quantity(); // The product Quantity
        $line_total = $items->get_total(); // The line item total

        // Display the product name
        echo '<p>'.$product_name.'</p>';

        // Display the product quantity
        echo '<p>'.$quantity.'</p>';

        // Display the product name
        echo '<p>'.$line_total.'</p>';

        // Get the raw output to check
        echo '<pre>'; print_r(echo $items->get_data() ); '</pre>';
    }
?>

请记住,一个订单可以有很多商品(所以不同的产品名称)。


相关trhread:Get Order items and WC_Order_Item_Product in Woocommerce 3