woocommerce 按顺序获取原始商品单价

dev*_*jim 5 php wordpress woocommerce

我想在客户电子邮件和客户查看订单页面中显示项目单价(包括销售价格和正常价格)。

在 woocommerce 订单行项目对象中,您只能看到项目行小计、小计、数量等。但在数据中并未显示原价单价(包括正价和销售价)。我最多可以通过添加折扣金额和行小计来获得单价。但我无法知道该物品是否在销售以及响应价格。

现在我可以使用以下代码来获取订单项目的单价:

$product = $item->get_product();
$product->get_price_html();
Run Code Online (Sandbox Code Playgroud)

问题get_price_html()是显示实时产品价格。这意味着如果我在一段时间后更改了产品价格,那么之前订单详情中的价格也会发生变化。当有人查看旧订单时,这会引起很多混乱。

那么如何才能在订单明细中获取订单项的原始单价呢?

dav*_*3rn 0

这可能不是最好的答案。但眼见此事仍无人答复。我已经尝试过了$item->get_price();,它返回的是一个未定义的函数。然而,根据我的问题,我能够使用$item->get_total()$item->get_quantity()。基于此,我可以将价格除以总数。这是我的临时修复:

            foreach ($items as $item):
                $product_name = $item->get_name();
                $product_price = $item->get_total();
                $product_quantity = $item->get_quantity();
                //This gets per unit price
                $unit_price = $product_price / $product_quantity;
                ?>
            <tr>
                <td>
                    <?= $product_name ?>
                </td>
                <td>
                    <?= number_format($unit_price, 2) ?> / Unit
                </td>
                <td>
                    <?= $product_quantity ?>
                </td>
                <td>
                    <?= number_format($product_price, 2) ?>
                </td>
            </tr>
            <?php
            endforeach;

Run Code Online (Sandbox Code Playgroud)

更新:

https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/

选择订单中所有商品的简便方法。