从 WooCommerce 中的 $order->get_formatted_line_subtotal() 获取非格式化值

Get non formatted value from $order->get_formatted_line_subtotal() in WooCommerce

我需要对文件 order-details-item.php 进行某些更改,产品价格在自定义元字段中形成。所以在我的例子中,值是:$qty = $item->get_quantity();是不正确的。总是一样的。

为了解决这个问题,我可以使用最简单的算术运算。将总订单价格除以产品价格。例如,如果客户以每公斤 14.5 的成本订购 10 公斤苹果,则总成本将为 145。这意味着为了正确显示数量,我需要 145/10。

    $price_weight_array = $product_attr['_mia_cup_price_weight'];
    $price_unit_array = $product_attr['_mia_cup_price_unit'];
    $sale_price_array = $product_attr['_mia_cup_sale_price_unit'];  
    
    $price_weight = $price_weight_array[0];
    $price_unit   = $price_unit_array[0];
    $sale_price = $sale_price_array[0];
    $prod_item_total = $order->get_formatted_line_subtotal();

    custom_quantity = $prod_item_total / $price_weight

这里就是问题 $order->get_formatted_line_subtotal(); returns 给我一个带货币符号的数字。而且我不能在算术运算中使用它。我怎样才能删除这个符号?

您的代码不完整,您没有使用正确的方法……此外 get_formatted_line_subtotal() method 需要一个强制参数才能工作,并且如您所知显示 格式化的 顺序项目小计 (带货币符号)

基于 and ,您需要先获取订单商品,然后您将在 foreach 循环中使用您的代码。

要获取非格式化和非四舍五入的订单项目小计,您将使用 get_line_subtotal() 方法,如下所示:

$order = wc_get_order($order_id); // (optional - if needed) get the WC_Order object

// Loop through order items
foreach( $order->get_items() as $item_id => $item ) {
    $product = $item->get_product(); // get the WC_Product Object

    // Your other code (missing from your question) Here …

    $price_weight    = reset($product_attr['_mia_cup_price_weight']);
    $price_unit      = reset($product_attr['_mia_cup_price_unit']);
    $sale_price_unit = reset($product_attr['_mia_cup_sale_price_unit']); 

    // Get the non formatted order item subtotal (and not rounded)
    $item_subtotal   = $order->get_line_subtotal( $item, $order->get_prices_include_tax(), false );

    $custom_quantity = $item_subtotal / $price_weight;
}

它应该更好用