我想在客户电子邮件和客户查看订单页面中显示项目单价(包括销售价格和常规价格)。
在woocommerce订单行项目对象中,您只能看到项目行小计,小计,数量等。但是它没有在数据中显示原始价格单价(包括正常价格和销售价格)。最多我可以通过将折扣金额和行小计相加得出单价。但是,我绝对无法知道该商品是否在销售以及响应价格。
现在,我可以使用以下代码获取订单商品的单价:
$product = $item->get_product();
$product->get_price_html();
此get_price_html()
的问题在于显示实时产品价格。这意味着,如果我在某个时间之后更改了产品价格,那么先前订单明细中的价格也会随之更改。当有人查看旧订单时,这会引起很多混乱。
那么如何获取订单明细中订单项的原始单价?
答案 0 :(得分:0)
woocommerce_checkout_create_order_line_item
有4个可用参数
$item
是WC_Order_Item_Product
$cart_item_key
是购物车商品的唯一哈希键$values
是购物车商品$order
WC_Order
对象的实例 $values
是购物车项目,您可以改用它
function my_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
$product = $values['data'];
$item->add_meta_data( 'My text', $product->get_price_html(), $item->get_id() );
}
add_action('woocommerce_checkout_create_order_line_item', 'my_checkout_create_order_line_item', 10, 4 );
答案 1 :(得分: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;
更新:
https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/
轻松选择订单中的所有内容。